From 8a66b8b7e9d21ae7c9fb63875d702138b3c3feeb Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 28 May 2026 19:15:14 +0330 Subject: [PATCH] last ... --- Interfaces/IXStorageProvider.cs | 102 +++-- Providers/XStorageProvider.cs | 656 +++++++++++++++++++------------- 2 files changed, 459 insertions(+), 299 deletions(-) diff --git a/Interfaces/IXStorageProvider.cs b/Interfaces/IXStorageProvider.cs index 4aec04d..cdd5d18 100644 --- a/Interfaces/IXStorageProvider.cs +++ b/Interfaces/IXStorageProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.FileProviders; @@ -11,7 +12,8 @@ using xStorageService.Models; namespace xStorageService.Interfaces { - public partial interface IXStorageProvider { + public partial interface IXStorageProvider + { XStorageConfiguration Configuration { get; } XStorageHelper StorageHelper { get; } IFileProvider FileProvider { get; } @@ -19,85 +21,101 @@ namespace xStorageService.Interfaces // #region Special Folders Path Getter (s) ... - string GetStorageFolderPath (); - string GetTempFolderPath (); - string GetThumbFolderPath (); - string GetUploadsFolderPath (); - string GetWidgetsFolderPath (); - string GetImageFolderPath (); - string GetAudioFolderPath (); - string GetVideoFolderPath (); - string GetDocumentFolderPath (); - string FilePathResolver (string fullPath); + string GetStorageFolderPath(); + string GetTempFolderPath(); + string GetThumbFolderPath(); + string GetUploadsFolderPath(); + string GetWidgetsFolderPath(); + string GetImageFolderPath(); + string GetAudioFolderPath(); + string GetVideoFolderPath(); + string GetDocumentFolderPath(); + string FilePathResolver(string fullPath); string ConvertToFileUrl(string fullPath); #endregion // #region Prepare Storage Structure ... - void Prepare (); + void Prepare(); #endregion // #region Validators ... - bool IsFolderExists (string path); - bool IsFileExists (string filePath); - bool IsSupportThumb (string fileName); - bool IsSupportThumb (XFileType type); - void ValidateFileExists (string filePath); - void ValidateFileModel (XFileDto file); - void ValidateFile (IFormFile file); - void ValidateProfileImage (IFormFile file); + bool IsFolderExists(string path); + bool IsFileExists(string filePath); + bool IsSupportThumb(string fileName); + bool IsSupportThumb(XFileType type); + void ValidateFileExists(string filePath); + void ValidateFileModel(XFileDto file); + void ValidateFile(IFormFile file); + void ValidateProfileImage(IFormFile file); #endregion // #region Generators ... - string NewFileName (); - string GenerateThumbName (string fileName); - string GetNewFileName (string fileName); + string NewFileName(); + string GenerateThumbName(string fileName); + string GetNewFileName(string fileName); #endregion // #region Tools Actions ... - string GetFileExtension (string fileName); - string GetFileNameWithoutExtension (string fileName); - FileExtensions GetFileExtensionType (string fileName); - XFileType GetFileType (string fileName); - XFileType GetFileType (FileExtensions fileExtension); - string GetThumbnailFileFullPath (string thumFileName); - string GetRelatedThumbnailFullPath (string fileName); - string GetFileTypeFolderPath (XFileType type); - string GetFileFolderPath ( + string GetFileExtension(string fileName); + string GetFileNameWithoutExtension(string fileName); + FileExtensions GetFileExtensionType(string fileName); + XFileType GetFileType(string fileName); + XFileType GetFileType(FileExtensions fileExtension); + string GetThumbnailFileFullPath(string thumFileName); + string GetRelatedThumbnailFullPath(string fileName); + string GetFileTypeFolderPath(XFileType type); + string GetFileFolderPath( string fileName, bool isTemp = false, bool isWidget = false ); - string GetFileFullPath ( + string GetFileFullPath( string fileName, bool isTemp = false, bool isWidget = false ); - Task SaveFileAsync ( + Task SaveFileAsync( IFormFile file, bool isTemp = false, - bool isWidget = false + bool isWidget = false, + CancellationToken cancellationToken = default ); - void DeleteFile ( + void DeleteFile( string fileFullPath, bool forceFileExists = true, Exception exception = null ); - ICollection DeleteFiles ( + ICollection DeleteFiles( ICollection fileFullPaths, bool forceFileExists = true, Exception exception = null ); // Task CreateThumbnail (XFile file); - Task CreateThumbnail (XFileDto file); - Task CreateThumbnail (string fileName); - Task HandleProfileImageSave (IFormFile file); - Task HandleFileSave (IFormFile file); - Task> HandleFilesSave (IFormFileCollection files); + Task CreateThumbnail( + XFileDto file, + CancellationToken cancellationToken = default + ); + Task CreateThumbnail( + string fileName, + CancellationToken cancellationToken = default + ); + Task HandleProfileImageSave( + IFormFile file, + CancellationToken cancellationToken = default + ); + Task HandleFileSave( + IFormFile file, + CancellationToken cancellationToken = default + ); + Task> HandleFilesSave( + IFormFileCollection files, + CancellationToken cancellationToken = default + ); #endregion } } \ No newline at end of file diff --git a/Providers/XStorageProvider.cs b/Providers/XStorageProvider.cs index 826897e..cf39331 100644 --- a/Providers/XStorageProvider.cs +++ b/Providers/XStorageProvider.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.FileProviders; @@ -15,8 +16,10 @@ using xStorageService.Helpers; using xStorageService.Interfaces; using xStorageService.Models; -namespace xStorageService.Providers { - public partial class XStorageProvider : IXStorageProvider { +namespace xStorageService.Providers +{ + public partial class XStorageProvider : IXStorageProvider + { public readonly string BasePath; private readonly XFileType[] ThumbSupprtedTypes = new XFileType[] { @@ -28,12 +31,13 @@ namespace xStorageService.Providers { public IXThumbnailProvider ThumbnailProvider { get; } public XStorageConfiguration Configuration { get; } - public XStorageProvider ( + public XStorageProvider( IFileProvider fileProvider, XStorageHelper storageHelper, IXThumbnailProvider thumbnailProvider, XStorageConfiguration configuration - ) { + ) + { // FileProvider = fileProvider; Configuration = configuration; @@ -42,8 +46,8 @@ namespace xStorageService.Providers { // BasePath = Path - .Combine (Directory - .GetCurrentDirectory (), Configuration.BaseFolder); + .Combine(Directory + .GetCurrentDirectory(), Configuration.BaseFolder); } // @@ -52,72 +56,81 @@ namespace xStorageService.Providers { /// Get Storage Folder Path /// /// - public string GetStorageFolderPath () { - return Path.Combine (BasePath, Configuration.Storage); + public string GetStorageFolderPath() + { + return Path.Combine(BasePath, Configuration.Storage); } /// /// Get Temp Folder Path /// /// - public string GetTempFolderPath () { - return Path.Combine (GetStorageFolderPath (), Configuration.Temp); + public string GetTempFolderPath() + { + return Path.Combine(GetStorageFolderPath(), Configuration.Temp); } /// /// Get Uploads Folder Path /// /// - public string GetUploadsFolderPath () { - return Path.Combine (GetStorageFolderPath (), Configuration.Uploads); + public string GetUploadsFolderPath() + { + return Path.Combine(GetStorageFolderPath(), Configuration.Uploads); } /// /// Get Widgets Folder Path /// /// - public string GetWidgetsFolderPath () { - return Path.Combine (GetStorageFolderPath (), Configuration.Widgets); + public string GetWidgetsFolderPath() + { + return Path.Combine(GetStorageFolderPath(), Configuration.Widgets); } /// /// Get Thumb Folder Path /// /// - public string GetThumbFolderPath () { - return Path.Combine (GetUploadsFolderPath (), Configuration.Thumb); + public string GetThumbFolderPath() + { + return Path.Combine(GetUploadsFolderPath(), Configuration.Thumb); } /// /// Get Images Folder Path /// /// - public string GetImageFolderPath () { - return Path.Combine (GetUploadsFolderPath (), Configuration.Image); + public string GetImageFolderPath() + { + return Path.Combine(GetUploadsFolderPath(), Configuration.Image); } /// /// Get Audios Folder Path /// /// - public string GetAudioFolderPath () { - return Path.Combine (GetUploadsFolderPath (), Configuration.Audio); + public string GetAudioFolderPath() + { + return Path.Combine(GetUploadsFolderPath(), Configuration.Audio); } /// /// Get Videos Folder Path /// /// - public string GetVideoFolderPath () { - return Path.Combine (GetUploadsFolderPath (), Configuration.Video); + public string GetVideoFolderPath() + { + return Path.Combine(GetUploadsFolderPath(), Configuration.Video); } /// /// Get Documents Folder Path /// /// - public string GetDocumentFolderPath () { - return Path.Combine (GetUploadsFolderPath (), Configuration.Document); + public string GetDocumentFolderPath() + { + return Path.Combine(GetUploadsFolderPath(), Configuration.Document); } /// @@ -125,8 +138,9 @@ namespace xStorageService.Providers { /// /// /// - public string FilePathResolver (string fullPath) { - return fullPath.Replace (BasePath, ""); + public string FilePathResolver(string fullPath) + { + return fullPath.Replace(BasePath, ""); } /// @@ -134,11 +148,12 @@ namespace xStorageService.Providers { /// /// /// - public string ConvertToProfileImageUrl (string fullPath) { + public string ConvertToProfileImageUrl(string fullPath) + { // - var authority = !Configuration.IdentityAuthority.IsNullOrEmpty () && - Configuration.IdentityAuthority.EndsWith ("/") ? - Configuration.IdentityAuthority.Substring (0, Configuration.IdentityAuthority.Length - 1) : + var authority = !Configuration.IdentityAuthority.IsNullOrEmpty() && + Configuration.IdentityAuthority.EndsWith("/") ? + Configuration.IdentityAuthority.Substring(0, Configuration.IdentityAuthority.Length - 1) : Configuration.IdentityAuthority; // @@ -150,11 +165,12 @@ namespace xStorageService.Providers { /// /// /// - public string ConvertToFileUrl (string fullPath) { + public string ConvertToFileUrl(string fullPath) + { // - var authority = !Configuration.Authority.IsNullOrEmpty () && - Configuration.Authority.EndsWith ("/") ? - Configuration.Authority.Substring (0, Configuration.Authority.Length - 1) : + var authority = !Configuration.Authority.IsNullOrEmpty() && + Configuration.Authority.EndsWith("/") ? + Configuration.Authority.Substring(0, Configuration.Authority.Length - 1) : Configuration.Authority; // @@ -168,69 +184,82 @@ namespace xStorageService.Providers { /// Check Folder Structure of Storage Area /// and Create them if not Exists /// - public void Prepare () { + public void Prepare() + { // - try { + try + { // // Storage Folder Path Check ... - if (!IsFolderExists (GetStorageFolderPath ())) { - Directory.CreateDirectory (GetStorageFolderPath ()); + if (!IsFolderExists(GetStorageFolderPath())) + { + Directory.CreateDirectory(GetStorageFolderPath()); } // // Temp Folder Path Check ... - if (!IsFolderExists (GetTempFolderPath ())) { - Directory.CreateDirectory (GetTempFolderPath ()); + if (!IsFolderExists(GetTempFolderPath())) + { + Directory.CreateDirectory(GetTempFolderPath()); } // // Temp Folder Path Check ... - if (!IsFolderExists (GetThumbFolderPath ())) { - Directory.CreateDirectory (GetThumbFolderPath ()); + if (!IsFolderExists(GetThumbFolderPath())) + { + Directory.CreateDirectory(GetThumbFolderPath()); } // // Widgets Folder Path Chekc ... - if (!IsFolderExists (GetWidgetsFolderPath ())) { - Directory.CreateDirectory (GetWidgetsFolderPath ()); + if (!IsFolderExists(GetWidgetsFolderPath())) + { + Directory.CreateDirectory(GetWidgetsFolderPath()); } // // Uploads Folder Path Chekc ... - if (!IsFolderExists (GetUploadsFolderPath ())) { - Directory.CreateDirectory (GetUploadsFolderPath ()); + if (!IsFolderExists(GetUploadsFolderPath())) + { + Directory.CreateDirectory(GetUploadsFolderPath()); } // // Image Folder Path Chekc ... - if (!IsFolderExists (GetImageFolderPath ())) { - Directory.CreateDirectory (GetImageFolderPath ()); + if (!IsFolderExists(GetImageFolderPath())) + { + Directory.CreateDirectory(GetImageFolderPath()); } // // Music Folder Path Chekc ... - if (!IsFolderExists (GetAudioFolderPath ())) { - Directory.CreateDirectory (GetAudioFolderPath ()); + if (!IsFolderExists(GetAudioFolderPath())) + { + Directory.CreateDirectory(GetAudioFolderPath()); } // // Video Folder Path Chekc ... - if (!IsFolderExists (GetVideoFolderPath ())) { - Directory.CreateDirectory (GetVideoFolderPath ()); + if (!IsFolderExists(GetVideoFolderPath())) + { + Directory.CreateDirectory(GetVideoFolderPath()); } // // Document Folder Path Chekc ... - if (!IsFolderExists (GetDocumentFolderPath ())) { - Directory.CreateDirectory (GetDocumentFolderPath ()); + if (!IsFolderExists(GetDocumentFolderPath())) + { + Directory.CreateDirectory(GetDocumentFolderPath()); } - } catch { - XException.StorageServiceInitialFailed.Throw (); + } + catch + { + XException.StorageServiceInitialFailed.Throw(); } // // Set Temp Folder for Actions ... - ThumbnailProvider.SetTempFolder (GetTempFolderPath ()); + ThumbnailProvider.SetTempFolder(GetTempFolderPath()); } #endregion @@ -241,8 +270,9 @@ namespace xStorageService.Providers { /// /// /// - public bool IsFolderExists (string path) { - return Directory.Exists (path); + public bool IsFolderExists(string path) + { + return Directory.Exists(path); } /// @@ -250,8 +280,9 @@ namespace xStorageService.Providers { /// /// /// - public bool IsFileExists (string filePath) { - return File.Exists (filePath); + public bool IsFileExists(string filePath) + { + return File.Exists(filePath); } /// @@ -259,15 +290,17 @@ namespace xStorageService.Providers { /// /// /// - public bool IsSupportThumb (string fileName) { + public bool IsSupportThumb(string fileName) + { // - if (!fileName.StartsWith (Configuration.FilePrefix)) { - XException.InavlidFile.Throw (); + if (!fileName.StartsWith(Configuration.FilePrefix)) + { + XException.InavlidFile.Throw(); } // - var fileType = StorageHelper.GetType (fileName); - return IsSupportThumb (fileType); + var fileType = StorageHelper.GetType(fileName); + return IsSupportThumb(fileType); } /// @@ -275,18 +308,21 @@ namespace xStorageService.Providers { /// /// /// - public bool IsSupportThumb (XFileType type) { - return ThumbSupprtedTypes.Contains (type); + public bool IsSupportThumb(XFileType type) + { + return ThumbSupprtedTypes.Contains(type); } /// /// Validate a File Exists or not /// /// - public void ValidateFileExists (string filePath) { + public void ValidateFileExists(string filePath) + { // - if (!IsFileExists (filePath)) { - XException.NotFound.Throw (); + if (!IsFileExists(filePath)) + { + XException.NotFound.Throw(); } } @@ -294,25 +330,29 @@ namespace xStorageService.Providers { /// Validate a File Model with it's Related Physical File /// /// - public void ValidateFileModel (XFileDto file) { + public void ValidateFileModel(XFileDto file) + { // // Validate Args ... - if (file == null) { - XException.InvalidArgs.Throw (); + if (file == null) + { + XException.InvalidArgs.Throw(); } // // Retrieve Full File Path ... var fileFullPath = file.Path; - if (fileFullPath.IsNullOrEmpty ()) { - XException.InavlidFile.Throw (); + if (fileFullPath.IsNullOrEmpty()) + { + XException.InavlidFile.Throw(); } // // Retrieve is File Exists or not ... - var isPhysicalFileExists = IsFileExists (file.Path); - if (!isPhysicalFileExists) { - XException.NotFound.Throw (); + var isPhysicalFileExists = IsFileExists(file.Path); + if (!isPhysicalFileExists) + { + XException.NotFound.Throw(); } } @@ -320,23 +360,27 @@ namespace xStorageService.Providers { /// Validate File for Upload /// /// - public void ValidateFile (IFormFile file) { + public void ValidateFile(IFormFile file) + { // // Validate Args ... - if (file == null) { - XException.InvalidArgs.Throw (); + if (file == null) + { + XException.InvalidArgs.Throw(); } // // Check File Size not Zero ... - if (file.Length == 0) { - XException.EmptyFile.Throw (); + if (file.Length == 0) + { + XException.EmptyFile.Throw(); } // // Check File Max Size ... - if (file.Length > Configuration.MaxFileSize) { - XException.MaxFileSizeExceeded.Throw (); + if (file.Length > Configuration.MaxFileSize) + { + XException.MaxFileSizeExceeded.Throw(); } } @@ -344,34 +388,38 @@ namespace xStorageService.Providers { /// Validate a Collection of IFormFile /// /// - public void ValidateFiles (IFormFileCollection files) { + public void ValidateFiles(IFormFileCollection files) + { // // Validate Args ... - if (!files.HasChild ()) { - XException.InvalidArgs.Throw (); + if (!files.HasChild()) + { + XException.InvalidArgs.Throw(); } // - files.ToList () - .ForEach (f => ValidateFile (f)); + files.ToList() + .ForEach(f => ValidateFile(f)); } /// /// Validate File for Profile Image /// /// - public void ValidateProfileImage (IFormFile file) { + public void ValidateProfileImage(IFormFile file) + { // // Check File Validation ... // since Args Checked in File Validation, there is no need to // double check it ... - ValidateFile (file); + ValidateFile(file); // // Check File Type ... - var fileType = GetFileType (file.FileName); - if (fileType != XFileType.Image) { - XException.UnsupportedFileType.Throw (); + var fileType = GetFileType(file.FileName); + if (fileType != XFileType.Image) + { + XException.UnsupportedFileType.Throw(); } } #endregion @@ -382,8 +430,9 @@ namespace xStorageService.Providers { /// Generate New File /// /// - public string NewFileName () { - return Configuration.FilePrefix + Guid.NewGuid ().ToString ().ToLower (); + public string NewFileName() + { + return Configuration.FilePrefix + Guid.NewGuid().ToString().ToLower(); } /// @@ -391,14 +440,16 @@ namespace xStorageService.Providers { /// /// /// - public string GenerateThumbName (string fileName) { + public string GenerateThumbName(string fileName) + { // - if (!IsSupportThumb (fileName)) { - XException.UnsupportedFileType.Throw (); + if (!IsSupportThumb(fileName)) + { + XException.UnsupportedFileType.Throw(); } // - return fileName.Replace (Configuration.FilePrefix, Configuration.ThumbPrefix); + return fileName.Replace(Configuration.FilePrefix, Configuration.ThumbPrefix); } /// @@ -406,20 +457,22 @@ namespace xStorageService.Providers { /// /// /// - public string GetNewFileName (string fileName) { + public string GetNewFileName(string fileName) + { // // Validate Args ... - if (fileName.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + if (fileName.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // - var fileExt = GetFileExtension (fileName); - var newFileNameWithoutExt = NewFileName (); + var fileExt = GetFileExtension(fileName); + var newFileNameWithoutExt = NewFileName(); // // Generate New File Name ... - var newFileName = string.Format ("{0}{1}", newFileNameWithoutExt, fileExt); + var newFileName = string.Format("{0}{1}", newFileNameWithoutExt, fileExt); // return newFileName; @@ -433,11 +486,13 @@ namespace xStorageService.Providers { /// /// /// - public string GetFileExtension (string fileName) { + public string GetFileExtension(string fileName) + { // - var result = StorageHelper.GetFileExtension (fileName); - if (result.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + var result = StorageHelper.GetFileExtension(fileName); + if (result.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // @@ -449,11 +504,13 @@ namespace xStorageService.Providers { /// /// /// - public string GetFileNameWithoutExtension (string fileName) { + public string GetFileNameWithoutExtension(string fileName) + { // - var result = StorageHelper.GetFileNameWithoutExtension (fileName); - if (result.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + var result = StorageHelper.GetFileNameWithoutExtension(fileName); + if (result.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // @@ -465,21 +522,24 @@ namespace xStorageService.Providers { /// /// /// - public FileExtensions GetFileExtensionType (string fileName) { + public FileExtensions GetFileExtensionType(string fileName) + { // - var fileExt = GetFileExtension (fileName); - if (fileExt.IsNullOrEmpty ()) { - XException.InvalidData.Throw (); + var fileExt = GetFileExtension(fileName); + if (fileExt.IsNullOrEmpty()) + { + XException.InvalidData.Throw(); } // - var result = StorageHelper.GetExtensionType (fileExt); - if (result == -1) { - XException.UnsupportedFileType.Throw (); + var result = StorageHelper.GetExtensionType(fileExt); + if (result == -1) + { + XException.UnsupportedFileType.Throw(); } // - return (FileExtensions) result; + return (FileExtensions)result; } /// @@ -487,10 +547,11 @@ namespace xStorageService.Providers { /// /// /// - public XFileType GetFileType (string fileName) { + public XFileType GetFileType(string fileName) + { // - var fileExtension = GetFileExtensionType (fileName); - return GetFileType (fileExtension); + var fileExtension = GetFileExtensionType(fileName); + return GetFileType(fileExtension); } /// @@ -498,8 +559,9 @@ namespace xStorageService.Providers { /// /// /// - public XFileType GetFileType (FileExtensions fileExtension) { - return StorageHelper.GetType ((int) fileExtension); + public XFileType GetFileType(FileExtensions fileExtension) + { + return StorageHelper.GetType((int)fileExtension); } /// @@ -507,14 +569,16 @@ namespace xStorageService.Providers { /// /// /// - public string GetThumbnailFileFullPath (string thumFileName) { + public string GetThumbnailFileFullPath(string thumFileName) + { // - if (!thumFileName.StartsWith (Configuration.ThumbPrefix)) { - XException.InavlidFile.Throw (); + if (!thumFileName.StartsWith(Configuration.ThumbPrefix)) + { + XException.InavlidFile.Throw(); } // - return Path.Combine (GetThumbFolderPath (), thumFileName); + return Path.Combine(GetThumbFolderPath(), thumFileName); } /// @@ -522,10 +586,11 @@ namespace xStorageService.Providers { /// /// /// - public string GetRelatedThumbnailFullPath (string fileName) { + public string GetRelatedThumbnailFullPath(string fileName) + { // - var thumbName = GenerateThumbName (fileName); - return GetThumbnailFileFullPath (thumbName); + var thumbName = GenerateThumbName(fileName); + return GetThumbnailFileFullPath(thumbName); } /// @@ -533,24 +598,26 @@ namespace xStorageService.Providers { /// /// /// - public string GetFileTypeFolderPath (XFileType type) { + public string GetFileTypeFolderPath(XFileType type) + { // // Retrieve Folder Path Based on file ... - switch (type) { + switch (type) + { case XFileType.Image: case XFileType.CoverImage: case XFileType.ProfileImasge: - return GetImageFolderPath (); + return GetImageFolderPath(); case XFileType.Audio: - return GetAudioFolderPath (); + return GetAudioFolderPath(); case XFileType.Video: - return GetVideoFolderPath (); + return GetVideoFolderPath(); case XFileType.Document: default: - return GetDocumentFolderPath (); + return GetDocumentFolderPath(); } } @@ -561,18 +628,21 @@ namespace xStorageService.Providers { /// /// /// - public string GetFileFolderPath ( + public string GetFileFolderPath( string fileName, bool isTemp = false, - bool isWidget = false) { + bool isWidget = false + ) + { // // Validate Args ... - if (fileName.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + if (fileName.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // - var fileType = GetFileType (fileName); + var fileType = GetFileType(fileName); // var folderPath = ""; @@ -580,29 +650,33 @@ namespace xStorageService.Providers { // // if isTemp equals true // ignore file type and return Temp Folder ... - if (isTemp) { - folderPath = GetTempFolderPath (); + if (isTemp) + { + folderPath = GetTempFolderPath(); } // // if isWidget equals true and isTemp equals false // ignore file type and return Widget's Folder ... - if (isWidget && !isTemp) { - folderPath = GetWidgetsFolderPath (); + if (isWidget && !isTemp) + { + folderPath = GetWidgetsFolderPath(); } // // if both isTemp and isWidget equals false // which is the default state, retrieve proper folder path // based on file type and return it ... - if (!isTemp && !isWidget) { - folderPath = GetFileTypeFolderPath (fileType); + if (!isTemp && !isWidget) + { + folderPath = GetFileTypeFolderPath(fileType); } // // Check folderPath restrict to have Value ... - if (folderPath.IsNullOrEmpty ()) { - XException.InvalidData.Throw (); + if (folderPath.IsNullOrEmpty()) + { + XException.InvalidData.Throw(); } // @@ -617,50 +691,63 @@ namespace xStorageService.Providers { /// /// /// - public string GetFileFullPath ( + public string GetFileFullPath( string fileName, bool isTemp = false, bool isWidget = false - ) { + ) + { // - var folderPath = GetFileFolderPath ( + var folderPath = GetFileFolderPath( fileName, - isTemp : isTemp, - isWidget : isWidget + isTemp: isTemp, + isWidget: isWidget ); // - if (folderPath.IsNullOrEmpty ()) { - XException.ActionFailed.Throw (); + if (folderPath.IsNullOrEmpty()) + { + XException.ActionFailed.Throw(); } // - return Path.Combine (folderPath, fileName); + return Path.Combine(folderPath, fileName); } /// /// Save a File into Storage Based on it's Type /// /// + /// + /// + /// /// - public async Task SaveFileAsync ( + public async Task SaveFileAsync( IFormFile file, bool isTemp = false, - bool isWidget = false) { + bool isWidget = false, + CancellationToken cancellationToken = default + ) + { // // Validate Args ... - ValidateFile (file); + ValidateFile(file); // // Extract and Generate required Data ... - var fileName = GetNewFileName (file.FileName); - var folderPath = GetFileFolderPath (file.FileName, isTemp, isWidget); - var fileFullPath = Path.Combine (folderPath, fileName); + var fileName = GetNewFileName(file.FileName); + var folderPath = GetFileFolderPath(file.FileName, isTemp, isWidget); + var fileFullPath = Path.Combine(folderPath, fileName); // // Save File ... - using (var stream = new FileStream (fileFullPath, FileMode.Create)) { - await file.CopyToAsync (stream); + using (var stream = new FileStream(fileFullPath, FileMode.Create)) + { + // + await file.CopyToAsync( + stream, + cancellationToken + ); } // @@ -674,41 +761,49 @@ namespace xStorageService.Providers { /// Delete a Physical File by Validate it's Exists /// /// - public void DeleteFile ( + /// + /// + public void DeleteFile( string fileFullPath, bool forceFileExists = true, - Exception exception = null) { + Exception exception = null + ) + { // // Validate Args ... if (!forceFileExists && - fileFullPath.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + fileFullPath.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // // Prepare Default Exception ... - if (exception == null) { - exception = XException.NotFound.ToException (); + if (exception == null) + { + exception = XException.NotFound.ToException(); } // // Check File Exists ... - var isFileExists = File.Exists (fileFullPath); + var isFileExists = File.Exists(fileFullPath); if (forceFileExists && - !isFileExists) { - XException.NotFound.Throw (); + !isFileExists) + { + XException.NotFound.Throw(); } // // Handle Prevent Error when forceFileExists false // and File doesn't Exists ... if (!forceFileExists && - !isFileExists) { + !isFileExists) + { return; } // - File.Delete (fileFullPath); + File.Delete(fileFullPath); } /// @@ -719,116 +814,140 @@ namespace xStorageService.Providers { /// /// /// - public ICollection DeleteFiles ( + public ICollection DeleteFiles( ICollection fileFullPaths, bool forceFileExists = true, Exception exception = null - ) { + ) + { // - if (exception.IsNull ()) { - exception = XException.NotFound.ToException (); + if (exception.IsNull()) + { + exception = XException.NotFound.ToException(); } // - var result = fileFullPaths.Where (f => !IsFileExists (GetFileFullPath (f))).ToList (); - if (forceFileExists && result.HasChild ()) { + var result = fileFullPaths.Where(f => !IsFileExists(GetFileFullPath(f))).ToList(); + if (forceFileExists && result.HasChild()) + { throw exception; } // - var existsFile = fileFullPaths.Where (f => IsFileExists (f)).ToList (); - existsFile.ForEach (f => { - DeleteFile ( + var existsFile = fileFullPaths.Where(f => IsFileExists(f)).ToList(); + existsFile.ForEach(f => + { + DeleteFile( f, - forceFileExists : forceFileExists, - exception : exception + forceFileExists: forceFileExists, + exception: exception ); }); // - return fileFullPaths.Except (existsFile).ToList (); + return fileFullPaths.Except(existsFile).ToList(); } /// /// Create Thumbnail based on FileName.Ext /// /// + /// /// - public async Task CreateThumbnail (string fileName) { + public async Task CreateThumbnail( + string fileName, + CancellationToken cancellationToken = default + ) + { // - if (fileName.IsNullOrEmpty ()) { + if (fileName.IsNullOrEmpty()) + { return; } // - await Task.Run (() => { + await Task.Run(() => + { // - var fileFullPath = GetFileFullPath (fileName); - if (!IsFileExists (fileFullPath)) { - XException.NotFound.Throw (); + var fileFullPath = GetFileFullPath(fileName); + if (!IsFileExists(fileFullPath)) + { + XException.NotFound.Throw(); } // - var thumbFileName = GenerateThumbName (fileName); - var thumbFullPath = GetThumbnailFileFullPath (thumbFileName); + var thumbFileName = GenerateThumbName(fileName); + var thumbFullPath = GetThumbnailFileFullPath(thumbFileName); // - ThumbnailProvider.ResizeImage ( + ThumbnailProvider.ResizeImage( fileFullPath, thumbFullPath, Configuration.ThumbSize, Configuration.ThumbQuality ); - }); + }, cancellationToken); } /// /// Create Thumbnail based on XFile model /// /// + /// /// - public async Task CreateThumbnail (XFileDto file) { + public async Task CreateThumbnail( + XFileDto file, + CancellationToken cancellationToken = default + ) + { // - await CreateThumbnail (file.Name); + await CreateThumbnail( + file.Name, + cancellationToken + ); } - // /// - // /// Create Thumbnail based on XFile model - // /// - // /// - // /// - // public async Task CreateThumbnail (XFile file) { - // // - // await CreateThumbnail (file.Name); - // } - /// /// Save and Create a Thumbnail For Profile Image /// /// + /// /// - public async Task HandleProfileImageSave (IFormFile file) { + public async Task HandleProfileImageSave( + IFormFile file, + CancellationToken cancellationToken = default + ) + { // - ValidateProfileImage (file); + ValidateProfileImage(file); // - var savedFileName = await SaveFileAsync (file, isTemp : false, isWidget : false); - var thumbFileName = GenerateThumbName (savedFileName); + var savedFileName = await SaveFileAsync( + file, + isTemp: false, + isWidget: false, + cancellationToken: cancellationToken + ); + var thumbFileName = GenerateThumbName(savedFileName); // - var savedFileFullPath = GetFileFullPath (savedFileName); - var thumbFullPath = GetThumbnailFileFullPath (thumbFileName); + var savedFileFullPath = GetFileFullPath(savedFileName); + var thumbFullPath = GetThumbnailFileFullPath(thumbFileName); // - await CreateThumbnail (savedFileName); + await CreateThumbnail( + savedFileName, + cancellationToken + ); // - return new XFileResult { + return new XFileResult + { Name = file.Name, - FileName = savedFileName, - FilePath = ConvertToProfileImageUrl (savedFileFullPath), - Thmbnail = thumbFileName, - ThmbnailPath = ConvertToProfileImageUrl (thumbFullPath) + FileName = savedFileName, + FilePath = ConvertToProfileImageUrl(savedFileFullPath), + Thmbnail = thumbFileName, + ThmbnailPath = ConvertToProfileImageUrl(thumbFullPath) }; } @@ -836,38 +955,45 @@ namespace xStorageService.Providers { /// Handle File Upload with Support of Thumbnails ... /// /// + /// /// - public async Task HandleFileSave (IFormFile file) { + public async Task HandleFileSave( + IFormFile file, + CancellationToken cancellationToken = default + ) + { // - ValidateFile (file); + ValidateFile(file); // - var savedFileName = await SaveFileAsync (file, isTemp : false, isWidget : false); + var savedFileName = await SaveFileAsync(file, isTemp: false, isWidget: false); // - var savedFileFullPath = GetFileFullPath (savedFileName); + var savedFileFullPath = GetFileFullPath(savedFileName); // var thumbFileName = ""; var thumbFullPath = ""; - var fileType = GetFileType (savedFileName); - if (fileType.IsImageKind ()) { + var fileType = GetFileType(savedFileName); + if (fileType.IsImageKind()) + { // - thumbFileName = GenerateThumbName (savedFileName); - thumbFullPath = GetThumbnailFileFullPath (thumbFileName); + thumbFileName = GenerateThumbName(savedFileName); + thumbFullPath = GetThumbnailFileFullPath(thumbFileName); // - await CreateThumbnail (savedFileName); + await CreateThumbnail(savedFileName); } // var result = - new XFileResult { + new XFileResult + { Name = file.FileName, FileName = savedFileName, - FilePath = ConvertToFileUrl (savedFileFullPath), + FilePath = ConvertToFileUrl(savedFileFullPath), Thmbnail = thumbFileName, - ThmbnailPath = ConvertToFileUrl (thumbFullPath), + ThmbnailPath = ConvertToFileUrl(thumbFullPath), }; // @@ -878,41 +1004,57 @@ namespace xStorageService.Providers { /// Handle File Upload with Support of Thumbnails ... /// /// + /// /// - public async Task> HandleFilesSave (IFormFileCollection files) { + public async Task> HandleFilesSave( + IFormFileCollection files, + CancellationToken cancellationToken = default + ) + { // - ValidateFiles (files); + ValidateFiles(files); // - var result = new Collection (); - foreach (var file in files) { + var result = new Collection(); + foreach (var file in files) + { // - var savedFileName = await SaveFileAsync (file, isTemp : false, isWidget : false); + var savedFileName = await SaveFileAsync( + file, + isTemp: false, + isWidget: false, + cancellationToken: cancellationToken + ); // - var savedFileFullPath = GetFileFullPath (savedFileName); + var savedFileFullPath = GetFileFullPath(savedFileName); // var thumbFileName = ""; var thumbFullPath = ""; - var fileType = GetFileType (savedFileName); - if (fileType.IsImageKind ()) { + var fileType = GetFileType(savedFileName); + if (fileType.IsImageKind()) + { // - thumbFileName = GenerateThumbName (savedFileName); - thumbFullPath = GetThumbnailFileFullPath (thumbFileName); + thumbFileName = GenerateThumbName(savedFileName); + thumbFullPath = GetThumbnailFileFullPath(thumbFileName); // - await CreateThumbnail (savedFileName); + await CreateThumbnail( + savedFileName, + cancellationToken + ); } // - result.Add ( - new XFileResult { + result.Add( + new XFileResult + { Name = file.FileName, - FileName = savedFileName, - FilePath = ConvertToFileUrl (savedFileFullPath), - Thmbnail = thumbFileName, - ThmbnailPath = ConvertToFileUrl (thumbFullPath), + FileName = savedFileName, + FilePath = ConvertToFileUrl(savedFileFullPath), + Thmbnail = thumbFileName, + ThmbnailPath = ConvertToFileUrl(thumbFullPath), }); }