public void Files_ExtensionThumbSupportedList_TiffMp4MovXMPCheck()
 {
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.tiff"));
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.mp4"));
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.mov"));
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.xmp"));
 }
Example #2
0
        public IActionResult AllowedTypesThumb(string f)
        {
            var result = ExtensionRolesHelper.IsExtensionThumbnailSupported(f);

            if (!result)
            {
                Response.StatusCode = 415;
            }
            return(Json(result));
        }
Example #3
0
        private static List <FileIndexItem> StackCollections(List <FileIndexItem> databaseSubFolderList)
        {
            // Get a list of duplicate items
            var stackItemsByFileCollectionName = databaseSubFolderList
                                                 .GroupBy(item => item.FileCollectionName)
                                                 .SelectMany(grp => grp.Skip(1).Take(1)).ToList();
            // databaseSubFolderList.ToList() > Collection was modified; enumeration operation may not execute.

            // duplicateItemsByFilePath >
            // If you have 3 item with the same name it will include 1 name
            // So we do a linq query to search simalar items
            // We keep the first item
            // And Delete duplicate items

            var querySubFolderList = new List <FileIndexItem>();

            // Do not remove it from: databaseSubFolderList otherwise it will be deleted from cache

            foreach (var stackItemByName in stackItemsByFileCollectionName)
            {
                var duplicateItems = databaseSubFolderList.Where(p =>
                                                                 p.FileCollectionName == stackItemByName.FileCollectionName).ToList();
                // The idea to pick thumbnail based images first, followed by non-thumb supported
                // when not pick alphabetaly > todo implement this

                for (int i = 0; i < duplicateItems.Count; i++)
                {
                    if (ExtensionRolesHelper.IsExtensionThumbnailSupported(duplicateItems[i].FileName))
                    {
                        querySubFolderList.Add(duplicateItems[i]);
                    }
                }
            }

            // Then add the items that are non duplicate back to the list
            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var dbItem in databaseSubFolderList.ToList())
            {
                // check if any item is duplicate
                if (stackItemsByFileCollectionName.All(p =>
                                                       p.FileCollectionName != dbItem.FileCollectionName))
                {
                    querySubFolderList.Add(dbItem);
                }
            }

            return(querySubFolderList.OrderBy(p => p.FileName).ToList());
        }
Example #4
0
        [ProducesResponseType(404)] // not found

        public IActionResult ListSizesByHash(string f)
        {
            // For serving jpeg files
            f = FilenamesHelper.GetFileNameWithoutExtension(f);

            // Restrict the fileHash to letters and digits only
            // I/O function calls should not be vulnerable to path injection attacks
            if (!Regex.IsMatch(f, "^[a-zA-Z0-9_-]+$"))
            {
                return(BadRequest());
            }

            var data = new ThumbnailSizesExistStatusModel {
                TinyMeta   = _thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, ThumbnailSize.TinyMeta)),
                Small      = _thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, ThumbnailSize.Small)),
                Large      = _thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, ThumbnailSize.Large)),
                ExtraLarge = _thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, ThumbnailSize.ExtraLarge))
            };

            // Success has all items (except tinyMeta)
            if (data.Small && data.Large && data.ExtraLarge)
            {
                return(Json(data));
            }

            var sourcePath           = _query.GetSubPathByHash(f);
            var isThumbnailSupported =
                ExtensionRolesHelper.IsExtensionThumbnailSupported(sourcePath);

            switch (isThumbnailSupported)
            {
            case true when !string.IsNullOrEmpty(sourcePath):
                Response.StatusCode = 202;
                return(Json(data));

            case false when !string.IsNullOrEmpty(sourcePath):
                Response.StatusCode = 210;                         // A conflict, that the thumb is not generated yet
                return(Json("Thumbnail is not supported; for example you try to view a raw or video file"));

            default:
                return(NotFound("not in index"));
            }
        }
Example #5
0
        [ProducesResponseType(210)] // raw
        public async Task <IActionResult> ByZoomFactor(
            string f,
            int z           = 0,
            string filePath = "")
        {
            // For serving jpeg files
            f = FilenamesHelper.GetFileNameWithoutExtension(f);

            // Restrict the fileHash to letters and digits only
            // I/O function calls should not be vulnerable to path injection attacks
            if (!Regex.IsMatch(f, "^[a-zA-Z0-9_-]+$"))
            {
                return(BadRequest());
            }

            // Cached view of item
            var sourcePath = await _query.GetSubPathByHashAsync(f);

            if (sourcePath == null)
            {
                if (await _query.GetObjectByFilePathAsync(filePath) == null)
                {
                    return(NotFound("not in index"));
                }
                sourcePath = filePath;
            }

            if (ExtensionRolesHelper.IsExtensionThumbnailSupported(sourcePath))
            {
                var fs1 = _iStorage.ReadStream(sourcePath);

                var fileExt = FilenamesHelper.GetFileExtensionWithoutDot(sourcePath);
                Response.Headers.Add("x-filename", FilenamesHelper.GetFileName(sourcePath));
                return(File(fs1, MimeHelper.GetMimeType(fileExt)));
            }

            Response.StatusCode = 210;     // A conflict, that the thumb is not generated yet
            return(Json("Thumbnail is not supported; for example you try to view a raw file"));
        }
Example #6
0
        [ResponseCache(Duration = 29030400)] // 4 weeks
        public IActionResult Thumbnail(
            string f,
            bool isSingleItem = false,
            bool json         = false,
            bool extraLarge   = true)
        {
            // f is Hash
            // isSingleItem => detailView
            // Retry thumbnail => is when you press reset thumbnail
            // json, => to don't waste the users bandwidth.

            // For serving jpeg files
            f = FilenamesHelper.GetFileNameWithoutExtension(f);

            // Get the text before at (@) so replace @2000 with nothing to match  fileHash
            var beforeAt = Regex.Match(f, ".*(?=@)", RegexOptions.None,
                                       TimeSpan.FromSeconds(1)).Value;

            if (!string.IsNullOrEmpty(beforeAt))
            {
                f = beforeAt;
            }

            // Restrict the fileHash to letters and digits only
            // I/O function calls should not be vulnerable to path injection attacks
            if (!Regex.IsMatch(f, "^[a-zA-Z0-9_-]+$"))
            {
                return(BadRequest());
            }

            var preferredSize = ThumbnailSize.ExtraLarge;
            var altSize       = ThumbnailSize.Large;

            if (!extraLarge)
            {
                preferredSize = ThumbnailSize.Large;
                altSize       = ThumbnailSize.ExtraLarge;
            }

            if (_thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, preferredSize)))
            {
                return(ReturnThumbnailResult(f, json, preferredSize));
            }

            if (_thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(f, altSize)))
            {
                return(ReturnThumbnailResult(f, json, altSize));
            }

            // Cached view of item
            var sourcePath = _query.GetSubPathByHash(f);

            if (sourcePath == null)
            {
                SetExpiresResponseHeadersToZero();
                return(NotFound("not in index"));
            }

            // Need to check again for recently moved files
            if (!_iStorage.ExistFile(sourcePath))
            {
                // remove from cache
                _query.ResetItemByHash(f);
                // query database again
                sourcePath = _query.GetSubPathByHash(f);
                SetExpiresResponseHeadersToZero();
                if (sourcePath == null)
                {
                    return(NotFound("not in index"));
                }
            }

            if (!_iStorage.ExistFile(sourcePath))
            {
                return(NotFound("There is no thumbnail image " + f + " and no source image " +
                                sourcePath));
            }

            if (!isSingleItem)
            {
                // "Photo exist in database but " + "isSingleItem flag is Missing"
                SetExpiresResponseHeadersToZero();
                Response.StatusCode = 202;         // A conflict, that the thumb is not generated yet
                return(Json("Thumbnail is not ready yet"));
            }

            if (ExtensionRolesHelper.IsExtensionThumbnailSupported(sourcePath))
            {
                var fs1 = _iStorage.ReadStream(sourcePath);

                var fileExt  = FilenamesHelper.GetFileExtensionWithoutDot(sourcePath);
                var fileName = HttpUtility.UrlEncode(FilenamesHelper.GetFileName(sourcePath));
                Response.Headers.TryAdd("x-filename", new StringValues(fileName));
                return(File(fs1, MimeHelper.GetMimeType(fileExt)));
            }

            Response.StatusCode = 210;     // A conflict, that the thumb is not generated yet
            return(Json("Thumbnail is not supported; for example you try to view a raw file"));
        }
Example #7
0
        /// <summary>
        /// Private use => CreateThumb
        /// Create a Thumbnail file to load it faster in the UI. Use FileIndexItem or database style path, Feature used by the cli tool
        /// </summary>
        /// <param name="subPath">relative path to find the file in the storage folder</param>
        /// <param name="fileHash">the base32 hash of the subPath file</param>
        /// <param name="skipExtraLarge">skip the extra large image</param>
        /// <returns>true, if successful</returns>
        private async Task <bool> CreateThumbInternal(string subPath, string fileHash, bool skipExtraLarge = false)
        {
            // FileType=supported + subPath=exit + fileHash=NOT exist
            if (!ExtensionRolesHelper.IsExtensionThumbnailSupported(subPath) ||
                !_iStorage.ExistFile(subPath))
            {
                return(false);
            }

            // File is already tested
            if (_iStorage.ExistFile(GetErrorLogItemFullPath(subPath)))
            {
                return(false);
            }

            var thumbnailToSourceSize = ThumbnailSize.ExtraLarge;

            if (skipExtraLarge)
            {
                thumbnailToSourceSize = ThumbnailSize.Large;
            }

            var largeThumbnailHash = ThumbnailNameHelper.Combine(fileHash, thumbnailToSourceSize);

            if (!_thumbnailStorage.ExistFile(ThumbnailNameHelper.Combine(
                                                 fileHash, thumbnailToSourceSize)))
            {
                // run resize sync
                var(_, resizeSuccess, resizeMessage) = (await ResizeThumbnailFromSourceImage(subPath,
                                                                                             ThumbnailNameHelper.GetSize(thumbnailToSourceSize),
                                                                                             largeThumbnailHash));

                // check if output any good
                RemoveCorruptImage(fileHash, thumbnailToSourceSize);

                if (!resizeSuccess || !_thumbnailStorage.ExistFile(
                        ThumbnailNameHelper.Combine(fileHash, thumbnailToSourceSize)))
                {
                    _logger.LogError($"[ResizeThumbnailFromSourceImage] " +
                                     $"output is null or corrupt for subPath {subPath}");
                    await WriteErrorMessageToBlockLog(subPath, resizeMessage);

                    return(false);
                }
                Console.Write(".");
            }

            var thumbnailFromThumbnailUpdateList = new List <ThumbnailSize>();

            void Add(ThumbnailSize size)
            {
                if (!_thumbnailStorage.ExistFile(
                        ThumbnailNameHelper.Combine(
                            fileHash, size))
                    )
                {
                    thumbnailFromThumbnailUpdateList.Add(size);
                }
            }

            new List <ThumbnailSize>
            {
                ThumbnailSize.Small,
                ThumbnailSize.Large                 // <- will be false when skipExtraLarge = true
            }.ForEach(Add);

            await(thumbnailFromThumbnailUpdateList).ForEachAsync(
                async(size)
                => await ResizeThumbnailFromThumbnailImage(
                    largeThumbnailHash,
                    ThumbnailNameHelper.GetSize(size),
                    ThumbnailNameHelper.Combine(fileHash, size)),
                10);

            Console.Write(".");
            return(thumbnailFromThumbnailUpdateList.Any());
        }
 public void Files_ExtensionThumbSupportedList_FolderName()
 {
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("Some Foldername"));
 }
 public void Files_ExtensionThumbSupportedList_null()
 {
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported(null));
     // equal or less then three chars
     Assert.AreEqual(false, ExtensionRolesHelper.IsExtensionThumbnailSupported("nul"));
 }
Example #10
0
 public void Files_ExtensionThumbSupportedList_JpgCheck()
 {
     Assert.AreEqual(true, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.jpg"));
     Assert.AreEqual(true, ExtensionRolesHelper.IsExtensionThumbnailSupported("file.bmp"));
 }