<commit_before>﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Dynamic;
using System.ComponentModel;
using System.Text;

using System.Web.Http;
using System.Net.Http;

using umbraco;
using Umbraco.Core;
using Umbraco.Core.Dynamics;
using Umbraco.Core.Models;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.IO;
using umbraco.BusinessLogic.Actions;
using umbraco.BusinessLogic;
using Umbraco.Web;
using Umbraco.Web.Editors;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
using Umbraco.Web.Models.ContentEditing;
using Examine;
using Examine.Providers;
using Examine.LuceneEngine;
using Examine.SearchCriteria;

using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System.Web.Hosting;
using System.IO;

using MediaTagger.Helpers;

namespace MediaTagger.Controllers.Api
{
    [PluginController("MediaTagger")]
    [IsBackOffice]
    public class MediaTaggerApiController : UmbracoAuthorizedJsonController
    {

        /// <summary>
        /// Retrieve all Medias that match the tags
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        [System.Web.Http.HttpGet]
        public IEnumerable<IMedia> GetMedias(string tags, int pageNumber = 0, int pageSize = 0, int startMediaId = 0, bool restrictPrivateFolder = false)
        {
            IEnumerable<IMedia> result = new List<IMedia>();

            // Get the media items
            List<int> mediaIds = GetMediasByTags(tags, startMediaId, restrictPrivateFolder).Select(x => x.Id).Skip(pageNumber * pageSize).Take(pageSize != 0 ? pageSize : 999).ToList<int>();
            if (mediaIds.Count > 0)
            {
                //IEnumerable<IMedia> medias = ApplicationContext.Services.MediaService.GetByIds(mediaIds);

                //// Check whether the logged in user got browsing rights on medias
                //foreach (var media in medias)
                //{
                //    var mediaChecked = PermissionHelper.CheckMediaPermissions(ApplicationContext.Current.Services.UserService, UmbracoContext.Current.Security.CurrentUser, ApplicationContext.Current.Services.MediaService, media.Id, new char[] { ActionBrowse.Instance.Letter });
                //    if (mediaChecked != null)
                //        result.Add(mediaChecked);
                //}

                result = ApplicationContext.Services.MediaService.GetByIds(mediaIds);
            }
            return result;
        }

        [System.Web.Http.HttpGet]
        public PagedResult<ContentItemBasic<ContentPropertyBasic, IMedia>> GetPagedMedias(string tags, int pageNumber = 0, int pageSize = 0, int startMediaId = 0, bool restrictPrivateFolder = false)
        {
            int totalItems;
            IMedia[] items;

            items = GetMedias(tags, pageNumber, pageSize, startMediaId, restrictPrivateFolder).ToArray();
<commit_msg>If there are no tags then sort results by the most recently updated media item<commit_after>﻿using AutoMapper;
using Examine;
using Examine.LuceneEngine;
using Examine.SearchCriteria;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;


namespace MediaTagger.Controllers.Api
{
    [PluginController("MediaTagger")]
    [IsBackOffice]
    public class MediaTaggerApiController : UmbracoAuthorizedJsonController
    {

        /// <summary>
        /// Retrieve all Medias that match the tags
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        [System.Web.Http.HttpGet]
        public IEnumerable<IMedia> GetMedias(string tags, int pageNumber = 0, int pageSize = 0, int startMediaId = 0, bool restrictPrivateFolder = false)
        {
            IEnumerable<IMedia> result = new List<IMedia>();

            // Get the media items
            var medias = GetMediasByTags(tags, startMediaId, restrictPrivateFolder);
            List<int> mediaIds = null;

            // If there are no tags then sort results by the most recently updated media item
            if (string.IsNullOrWhiteSpace(tags))
            {
                mediaIds = medias.OrderByDescending(x => x.Fields["updateDate"])
                    .Select(x => x.Id)
                    .Skip(pageNumber * pageSize)
                    .Take(pageSize != 0 ? pageSize : 999)
                    .ToList<int>();
            }
            // If there are tags then keep the lucene default sort order (Lucene provides results by the most relevant hit at the top)
            else
            {
                mediaIds = medias
                    .Select(x => x.Id)
                    .Skip(pageNumber * pageSize)
                    .Take(pageSize != 0 ? pageSize : 999)
                    .ToList<int>();
            }

            if (mediaIds.Count > 0)
            {
                //IEnumerable<IMedia> medias = ApplicationContext.Services.MediaService.GetByIds(mediaIds);

                //// Check whether the logged in user got browsing rights on medias
                //foreach (var media in medias)
                //{
                //    var mediaChecked = PermissionHelper.CheckMediaPermissions(ApplicationContext.Current.Services.UserService, UmbracoContext.Current.Security.CurrentUser, ApplicationContext.Current.Services.MediaService, media.Id, new char[] { ActionBrowse.Instance.Letter });
                //    if (mediaChecked != null)
                //        result.Add(mediaChecked);
                //}

                result = ApplicationContext.Services.MediaService.GetByIds(mediaIds);
            }
            return result;
        }

        [System.Web.Http.HttpGet]
        public PagedResult<ContentItemBasic<ContentPropertyBasic, IMedia>> GetPagedMedias(string tags, int pageNumber = 0, int pageSize = 0, int startMediaId = 0, bool restrictPrivateFolder = false)
        {
            int totalItems;
            IMedia[] items;

            items = GetMedias(tags, pageNumber, pageSize, startMediaId, restrictPrivateFolder).ToArray();

{'fraction_non_alphanumeric': 0.09741176470588235, 'fraction_numerical': 0.00407843137254902, 'mean_word_length': 2.4446245272825498, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7385799', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1712, 'n_words': 419}