diff --git a/Concretes/FileStorage.cs b/Concretes/FileStorage.cs index 1744c56..4fd50a5 100644 --- a/Concretes/FileStorage.cs +++ b/Concretes/FileStorage.cs @@ -26,44 +26,24 @@ public FileUploadResult UploadFile(IFormFile file, string directoryPath) _fileStorageOptions.GlobalFileExtensionFilter); } - var combinedDirectoryPath = string.IsNullOrEmpty(_fileStorageOptions.BaseFolderPath) - ? directoryPath - : Path.Combine(_fileStorageOptions.BaseFolderPath, directoryPath); - var uploadDirectoryPath = Path.Combine(Environment.CurrentDirectory, combinedDirectoryPath); - - if (!File.Exists(uploadDirectoryPath)) Directory.CreateDirectory(uploadDirectoryPath); - - var fileNameGenerated = FileStorageHelpers.GenerateFileName(file.FileName); - var fullPath = Path.Combine(uploadDirectoryPath, fileNameGenerated); - - using (var stream = new FileStream(fullPath, FileMode.Create)) - { - file.CopyTo(stream); - } - - var filePath = fullPath.Replace(Environment.CurrentDirectory, "").Remove(0, 1); - - return new FileUploadResult(fileName: file.FileName, - filePath: filePath, - fullPath: fullPath, - contentType: file.ContentType, - fileNameGenerated: fileNameGenerated); + return Upload(file, directoryPath); } - public FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter) + public FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter, + bool includeGlobalFileExtensionFilter = true) { ArgumentNullException.ThrowIfNull(file, nameof(file)); ArgumentNullException.ThrowIfNull(directoryPath, nameof(directoryPath)); - string fileExtension = Path.GetExtension(file.FileName).ToLower(); - if (_fileStorageOptions.GlobalFileExtensionFilter is not null) + var fileExtension = Path.GetExtension(file.FileName).ToLower(); + if (_fileStorageOptions.GlobalFileExtensionFilter is not null && includeGlobalFileExtensionFilter) { extensionFilter = extensionFilter.Union(_fileStorageOptions.GlobalFileExtensionFilter).ToArray(); } FileStorageHelpers.CheckFileExtension(fileExtension, extensionFilter); - return UploadFile(file, directoryPath); + return Upload(file, directoryPath); } public void DeleteFile(string filePath) @@ -81,4 +61,30 @@ public Stream GetFile(string filePath) return new FileStream(filePath, FileMode.Open); } + + private FileUploadResult Upload(IFormFile file, string directoryPath) + { + var combinedDirectoryPath = string.IsNullOrEmpty(_fileStorageOptions.BaseFolderPath) + ? directoryPath + : Path.Combine(_fileStorageOptions.BaseFolderPath, directoryPath); + var uploadDirectoryPath = Path.Combine(Environment.CurrentDirectory, combinedDirectoryPath); + + if (!File.Exists(uploadDirectoryPath)) Directory.CreateDirectory(uploadDirectoryPath); + + var fileNameGenerated = FileStorageHelpers.GenerateFileName(file.FileName); + var fullPath = Path.Combine(uploadDirectoryPath, fileNameGenerated); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + file.CopyTo(stream); + } + + var filePath = fullPath.Replace(Environment.CurrentDirectory, "").Remove(0, 1); + + return new FileUploadResult(fileName: file.FileName, + filePath: filePath, + fullPath: fullPath, + contentType: file.ContentType, + fileNameGenerated: fileNameGenerated); + } } \ No newline at end of file diff --git a/Interfaces/IFileStorage.cs b/Interfaces/IFileStorage.cs index a004954..3ae9702 100644 --- a/Interfaces/IFileStorage.cs +++ b/Interfaces/IFileStorage.cs @@ -6,19 +6,28 @@ namespace LocalFileStorage.Interfaces; public interface IFileStorage { /// - /// This method is used to upload a file to the file storage. + /// Uploads a file to the specified directory. /// - /// The file to be uploaded. + /// The instance representing the file to be uploaded. /// The path to upload the file to. + /// Returns a indicating the outcome of the file upload operation. FileUploadResult UploadFile(IFormFile file, string directoryPath); /// - /// This method is used to upload a file to the file storage. + /// Uploads a file to the specified directory with optional file extension filtering. /// - /// The file to be uploaded. + /// The instance representing the file to be uploaded. /// The path to upload the file to. - /// The file extensions that are allowed to be uploaded. - FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter); + /// An array of file extensions that are allowed to be uploaded. + /// + /// Indicates whether to include the global file extension filter settings defined at application level. + /// If set to true (default), both the global file extension filter and the extensions provided in the 'extensionFilter' parameter will be enforced. + /// If set to false, only the extensions provided in the 'extensionFilter' parameter will be considered. + /// + /// Returns a indicating the outcome of the file upload operation. + FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter, + bool includeGlobalFileExtensionFilter = true); + /// /// This method is used to delete a file from the file storage. @@ -32,4 +41,4 @@ public interface IFileStorage /// The path of the file to be retrieved. /// The file stream of the file. Stream GetFile(string filePath); -} +} \ No newline at end of file diff --git a/LocalFileStorage.csproj b/LocalFileStorage.csproj index 0d044ca..149334b 100644 --- a/LocalFileStorage.csproj +++ b/LocalFileStorage.csproj @@ -7,12 +7,10 @@ true $(NoWarn);1591 rknyryn.LocalFileStorage - 3.0.0 + 3.1.0 - - Transitioned services from 'AddScoped' to 'AddSingleton' for enhanced performance. - - Renamed the 'path' parameter to 'directoryPath' for more clarity. - - Implemented 'Path.IsRooted' check for the base folder path to ensure valid directory paths. - - Enhanced the package functionality to return both relative and full file paths. + - File extension filter bug fixed. + - Include global extension filter parameter added. Kaan Yarayan Local File Storage diff --git a/README.md b/README.md index c1f84bf..0e6af87 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ public class FileStorageController : ControllerBase public FileUploadResult UploadFile(IFormFile uploadedFile) { FileUploadResult fileUploadResult = _fileStorage.UploadFile(file: uploadedFile, directoryPath: "documents"); - // FileUploadResult fileUploadResult = fileStorage.UploadFile(file: uploadedFile, directoryPath: "documents", extensionFilter: [".jpg", ".png"]); + // FileUploadResult fileUploadResult = fileStorage.UploadFile(file: uploadedFile, directoryPath: "documents", extensionFilter: [".jpg"]); + // FileUploadResult fileUploadResult = fileStorage.UploadFile(file: uploadedFile, directoryPath: "documents", extensionFilter: [".jpg"], includeGlobalFileExtensionFilter: false); return fileUploadResult; }