/// <summary> /// Get the collections items when preflighting /// Returns as Tuple /// item1: inputFileSubPaths, toFileSubPaths /// item2: list of fileIndex Results (which contains only error cases) /// </summary> /// <param name="inputFileSubPaths">from where to copy (file or folder)</param> /// <param name="toFileSubPaths">copy to (file or folder)</param> /// <param name="fileIndexResultsList">results list</param> /// <param name="collections">enable file collections</param> /// <returns>inputFileSubPaths list, toFileSubPaths list and fileIndexResultsList</returns> private Tuple <Tuple <string[], string[]>, List <FileIndexItem> > CollectionAddPreflight( IReadOnlyList <string> inputFileSubPaths, IReadOnlyList <string> toFileSubPaths, List <FileIndexItem> fileIndexResultsList, bool collections) { if (!collections) { return(new Tuple <Tuple <string[], string[]>, List <FileIndexItem> >( new Tuple <string[], string[]>(inputFileSubPaths.ToArray(), toFileSubPaths.ToArray()), fileIndexResultsList )); } var inputCollectionFileSubPaths = new List <string>(); var toCollectionFileSubPaths = new List <string>(); for (var i = 0; i < inputFileSubPaths.Count; i++) { // When the input is a folder, just copy the array if (_iStorage.ExistFolder(inputFileSubPaths[i])) { inputCollectionFileSubPaths.Add(inputFileSubPaths[i]); toCollectionFileSubPaths.Add(toFileSubPaths[i]); continue; } // when it is a file update the 'to paths' var collectionPaths = _query.SingleItem(inputFileSubPaths[i], null, true, false).FileIndexItem.CollectionPaths; inputCollectionFileSubPaths.AddRange(collectionPaths); for (var j = 0; j < collectionPaths.Count; j++) { var collectionItem = collectionPaths[j]; // When moving to a folder if (_iStorage.ExistFolder(toFileSubPaths[i])) { toCollectionFileSubPaths.Add(toFileSubPaths[i]); continue; } var extensionWithoutDot = FilenamesHelper.GetFileExtensionWithoutDot(collectionItem); // when rename-ing the current file, but the other ones are implicit copied if (j == 0) { extensionWithoutDot = FilenamesHelper.GetFileExtensionWithoutDot(toFileSubPaths[i]); } // Rename other sidecar files // From file to Deleted var parentFolder = FilenamesHelper.GetParentPath(toFileSubPaths[i]); var baseName = FilenamesHelper.GetFileNameWithoutExtension(toFileSubPaths[i]); toCollectionFileSubPaths.Add($"{parentFolder}/{baseName}.{extensionWithoutDot}"); } } return(new Tuple <Tuple <string[], string[]>, List <FileIndexItem> >( new Tuple <string[], string[]>(inputCollectionFileSubPaths.ToArray(), toCollectionFileSubPaths.ToArray()), fileIndexResultsList )); }
internal static string AppendIndexerToFilePath(string parentDirectory, string fileName, int index) { if (index >= 1) { fileName = string.Concat( FilenamesHelper.GetFileNameWithoutExtension(fileName), $"_{index}.", FilenamesHelper.GetFileExtensionWithoutDot(fileName) ); } return(PathHelper.AddSlash(parentDirectory) + PathHelper.RemovePrefixDbSlash(fileName)); }
/// <summary> /// this updates the main database item for a sidecar file /// </summary> /// <param name="xmpSubPath">sidecar file</param> /// <param name="directoryWithFileIndexItems">directory where the sidecar is located</param> /// <returns>completed task</returns> private async Task UpdateSidecarFile(string xmpSubPath, List <FileIndexItem> directoryWithFileIndexItems) { if (!ExtensionRolesHelper.IsExtensionSidecar(xmpSubPath)) { return; } var sidecarExt = FilenamesHelper.GetFileExtensionWithoutDot(xmpSubPath); foreach (var item in directoryWithFileIndexItems.Where(item => !item.SidecarExtensionsList.Contains(sidecarExt))) { item.AddSidecarExtension(sidecarExt); await _query.UpdateItemAsync(item); } }
[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")); }
private static IOrderedQueryable <FileIndexItem> GetObjectsByFilePathCollectionQuery(ApplicationDbContext context, IEnumerable <string> filePathList) { var predicates = new List <Expression <Func <FileIndexItem, bool> > >(); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var path in filePathList) { var fileNameWithoutExtension = FilenamesHelper.GetFileNameWithoutExtension(path); if (string.IsNullOrEmpty(FilenamesHelper.GetFileExtensionWithoutDot(path))) { predicates.Add(p => p.ParentDirectory == FilenamesHelper.GetParentPath(path) && p.FileName == fileNameWithoutExtension); continue; } predicates.Add(p => p.ParentDirectory == FilenamesHelper.GetParentPath(path) && p.FileName.StartsWith(fileNameWithoutExtension + ".")); } var predicate = PredicateBuilder.OrLoop(predicates); return(context.FileIndex.Where(predicate).OrderBy(r => r.FileName)); }
private ImportIndexItem ApplyStructure(ImportIndexItem importIndexItem, string overwriteStructure) { importIndexItem.Structure = _appSettings.Structure; // Feature to overwrite structures when importing using a header // Overwrite the structure in the ImportIndexItem if (!string.IsNullOrWhiteSpace(overwriteStructure)) { importIndexItem.Structure = overwriteStructure; } var structureService = new StructureService(_subPathStorage, importIndexItem.Structure); importIndexItem.FileIndexItem.ParentDirectory = structureService.ParseSubfolders( importIndexItem.FileIndexItem.DateTime, importIndexItem.FileIndexItem.FileCollectionName, FilenamesHelper.GetFileExtensionWithoutDot(importIndexItem.FileIndexItem.FileName)); importIndexItem.FileIndexItem.FileName = structureService.ParseFileName( importIndexItem.FileIndexItem.DateTime, importIndexItem.FileIndexItem.FileCollectionName, FilenamesHelper.GetFileExtensionWithoutDot(importIndexItem.FileIndexItem.FileName)); importIndexItem.FilePath = importIndexItem.FileIndexItem.FilePath; return(importIndexItem); }
[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")); }
public void FilenamesHelper_GetFileExtensionWithoutDot_NoExtension() { var result = FilenamesHelper.GetFileExtensionWithoutDot("/test_image"); Assert.AreEqual(string.Empty, result); }
public void FilenamesHelper_GetFileExtensionWithoutDot_Jpeg() { var result = FilenamesHelper.GetFileExtensionWithoutDot("/test.jpeg"); Assert.AreEqual("jpeg", result); }
public void FilenamesHelper_GetFileExtensionWithoutDot_FilePath() { var result = FilenamesHelper.GetFileExtensionWithoutDot("/folder/test.mp4"); Assert.AreEqual("mp4", result); }
public void FilenamesHelper_GetFileExtensionWithoutDot_UpperCase_Mp4() { var result = FilenamesHelper.GetFileExtensionWithoutDot("/test.MP4"); Assert.AreEqual("mp4", result); }