/// <summary>
        /// Tries to find an equal file by file name, then by comparing the binary contents of the matched files to <paramref name="source"/> content.
        /// </summary>
        /// <param name="source">The source file stream to find a duplicate for (e.g. a local or downloaded file during an import process).</param>
        /// <param name="fileName">The file name used to determine potential duplicates to check against.</param>
        /// <param name="targetFolderId">The id of the folder in which to look for duplicates.</param>
        /// <param name="deepSearch">Whether to search in subfolders too.</param>
        /// <param name="equalFile">The first file whose content is equal to the content of <paramref name="source"/>.</param>
        /// <returns><c>true</c> when a duplicate file was found, <c>false</c> otherwise.</returns>
        public static bool FindEqualFile(this IMediaService service, Stream source, string fileName, int targetFolderId, bool deepSearch, out MediaFile equalFile)
        {
            Guard.NotNull(source, nameof(source));
            Guard.NotEmpty(fileName, nameof(fileName));
            Guard.IsPositive(targetFolderId, nameof(targetFolderId));

            equalFile = null;

            var query = new MediaSearchQuery
            {
                FolderId          = targetFolderId,
                DeepSearch        = deepSearch,
                ExactMatch        = true,
                Term              = fileName,
                IncludeAltForTerm = false
            };

            var matches = service.SearchFiles(query);

            if (matches.TotalCount == 0)
            {
                return(false);
            }

            return(service.FindEqualFile(source, matches.Select(x => x.File), true, out equalFile));
        }
Esempio n. 2
0
        protected internal virtual bool CheckUniqueFileName(MediaPathData pathData)
        {
            // (perf) First make fast check
            var exists = _fileRepo.Table.Any(x => x.Name == pathData.FileName && x.FolderId == pathData.Folder.Id);

            if (!exists)
            {
                return(false);
            }

            var q = new MediaSearchQuery
            {
                FolderId = pathData.Folder.Id,
                Term     = string.Concat(pathData.FileTitle, "*.", pathData.Extension),
                Deleted  = null
            };

            var query = _searcher.PrepareQuery(q, MediaLoadFlags.AsNoTracking).Select(x => x.Name);
            var files = new HashSet <string>(query.ToList(), StringComparer.CurrentCultureIgnoreCase);

            if (_helper.CheckUniqueFileName(pathData.FileTitle, pathData.Extension, files, out var uniqueName))
            {
                pathData.FileName = uniqueName;
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        public Task <int> CountFilesAsync(MediaSearchQuery query)
        {
            Guard.NotNull(query, nameof(query));

            var q = _searcher.PrepareQuery(query, MediaLoadFlags.None);

            return(q.CountAsync());
        }
Esempio n. 4
0
        public int CountFiles(MediaSearchQuery query)
        {
            Guard.NotNull(query, nameof(query));

            var q     = _searcher.PrepareQuery(query, MediaLoadFlags.None);
            var count = q.Count();

            return(count);
        }
Esempio n. 5
0
        public async Task <MediaSearchResult> SearchFilesAsync(
            MediaSearchQuery query,
            Func <IQueryable <MediaFile>, IQueryable <MediaFile> > queryModifier,
            MediaLoadFlags flags = MediaLoadFlags.AsNoTracking)
        {
            Guard.NotNull(query, nameof(query));

            var files = _searcher.SearchFiles(query, flags);

            if (queryModifier != null)
            {
                files.AlterQuery(queryModifier);
            }

            return(new MediaSearchResult(await files.LoadAsync(), ConvertMediaFile));
        }
Esempio n. 6
0
        public IEnumerable <IFile> ListFiles(string path)
        {
            var node = _folderService.GetNodeByPath(path);

            if (node == null)
            {
                throw _exceptionFactory.FolderNotFound(path);
            }

            var query = new MediaSearchQuery
            {
                FolderId = node.Value.Id
            };

            return(_mediaService.SearchFiles(query));
        }
Esempio n. 7
0
        public IEnumerable <string> SearchFiles(string path, string pattern, bool deep = true)
        {
            var node = _folderService.GetNodeByPath(path);

            if (node == null)
            {
                throw _exceptionFactory.FolderNotFound(path);
            }

            var query = new MediaSearchQuery
            {
                FolderId   = node.Value.Id,
                DeepSearch = deep,
                Term       = pattern
            };

            return(_mediaService.SearchFiles(query).Select(x => x.Path).ToList());
        }
Esempio n. 8
0
        public long CountFiles(string path, string pattern, Func <string, bool> predicate, bool deep = true)
        {
            if (predicate == null)
            {
                var node = _folderService.GetNodeByPath(path);
                if (node == null)
                {
                    throw _exceptionFactory.FolderNotFound(path);
                }

                var query = new MediaSearchQuery
                {
                    FolderId   = node.Value.Id,
                    DeepSearch = deep,
                    Term       = pattern
                };

                return(_mediaService.CountFiles(query));
            }

            var files = SearchFiles(path, pattern, deep);

            return(files.Count(predicate));
        }
Esempio n. 9
0
        public virtual IQueryable <MediaFile> PrepareQuery(MediaSearchQuery query, MediaLoadFlags flags)
        {
            Guard.NotNull(query, nameof(query));

            var  q = _fileRepo.Table;
            bool?shouldIncludeDeleted = false;

            // Folder
            if (query.FolderId > 0)
            {
                if (query.DeepSearch)
                {
                    var folderIds = _folderService.Value.GetNodesFlattened(query.FolderId.Value, true).Select(x => x.Id).ToArray();
                    q = q.Where(x => x.FolderId != null && folderIds.Contains(x.FolderId.Value));
                }
                else
                {
                    q = q.Where(x => x.FolderId == query.FolderId);
                }
            }
            else if (query.FolderId < 0)
            {
                // Special folders
                if (query.FolderId == (int)SpecialMediaFolder.AllFiles)
                {
                    shouldIncludeDeleted = null;
                }
                else if (query.FolderId == (int)SpecialMediaFolder.Trash)
                {
                    shouldIncludeDeleted = true;
                }
                else if (query.FolderId == (int)SpecialMediaFolder.Orphans)
                {
                    // Get ids of untrackable folders, 'cause no orphan check can be made for them.
                    var untrackableFolderIds = _folderService.Value.GetRootNode()
                                               .SelectNodes(x => !x.Value.CanDetectTracks)
                                               .Select(x => x.Value.Id)
                                               .ToArray();

                    q = q.Where(x => x.FolderId > 0 && !untrackableFolderIds.Contains(x.FolderId.Value) && !x.Tracks.Any());
                }
                else if (query.FolderId == (int)SpecialMediaFolder.TransientFiles)
                {
                    q = q.Where(x => x.IsTransient);
                }
                else if (query.FolderId == (int)SpecialMediaFolder.UnassignedFiles)
                {
                    q = q.Where(x => x.FolderId == null);
                }
            }
            else
            {
                // (perf) Composite index
                q = q.Where(x => x.FolderId == null || x.FolderId.HasValue);
            }

            q = ApplyFilterQuery(query, q);

            if (query.Deleted == null && shouldIncludeDeleted.HasValue)
            {
                q = q.Where(x => x.Deleted == shouldIncludeDeleted.Value);
            }

            // Sorting
            var ordering = query.SortBy.NullEmpty() ?? "Id";

            if (query.SortDesc)
            {
                ordering += " descending";
            }
            q = q.OrderBy(ordering);

            return(ApplyLoadFlags(q, flags));
        }
Esempio n. 10
0
        public virtual IPagedList <MediaFile> SearchFiles(MediaSearchQuery query, MediaLoadFlags flags = MediaLoadFlags.AsNoTracking)
        {
            var q = PrepareQuery(query, flags);

            return(new PagedList <MediaFile>(q, query.PageIndex, query.PageSize));
        }
 public static async Task <MediaSearchResult> SearchFilesAsync(this IMediaService service, MediaSearchQuery query, MediaLoadFlags flags = MediaLoadFlags.AsNoTracking)
 {
     return(await service.SearchFilesAsync(query, null, flags));
 }
 public static MediaSearchResult SearchFiles(this IMediaService service, MediaSearchQuery query, MediaLoadFlags flags = MediaLoadFlags.AsNoTracking)
 {
     return(service.SearchFiles(query, null, flags));
 }