Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions Concretes/FileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
23 changes: 16 additions & 7 deletions Interfaces/IFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ namespace LocalFileStorage.Interfaces;
public interface IFileStorage
{
/// <summary>
/// This method is used to upload a file to the file storage.
/// Uploads a file to the specified directory.
/// </summary>
/// <param name="file">The file to be uploaded.</param>
/// <param name="file">The <see cref="IFormFile"/> instance representing the file to be uploaded.</param>
/// <param name="directoryPath">The path to upload the file to.</param>
/// <returns>Returns a <see cref="FileUploadResult"/> indicating the outcome of the file upload operation.</returns>
FileUploadResult UploadFile(IFormFile file, string directoryPath);

/// <summary>
/// This method is used to upload a file to the file storage.
/// Uploads a file to the specified directory with optional file extension filtering.
/// </summary>
/// <param name="file">The file to be uploaded.</param>
/// <param name="file">The <see cref="IFormFile"/> instance representing the file to be uploaded.</param>
/// <param name="directoryPath">The path to upload the file to.</param>
/// <param name="extensionFilter">The file extensions that are allowed to be uploaded.</param>
FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter);
/// <param name="extensionFilter">An array of file extensions that are allowed to be uploaded.</param>
/// <param name="includeGlobalFileExtensionFilter">
/// 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.
/// </param>
/// <returns>Returns a <see cref="FileUploadResult"/> indicating the outcome of the file upload operation.</returns>
FileUploadResult UploadFile(IFormFile file, string directoryPath, string[] extensionFilter,
bool includeGlobalFileExtensionFilter = true);


/// <summary>
/// This method is used to delete a file from the file storage.
Expand All @@ -32,4 +41,4 @@ public interface IFileStorage
/// <param name="filePath">The path of the file to be retrieved.</param>
/// <returns>The file stream of the file.</returns>
Stream GetFile(string filePath);
}
}
8 changes: 3 additions & 5 deletions LocalFileStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<PackageId>rknyryn.LocalFileStorage</PackageId>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<PackageReleaseNotes>
- 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.
</PackageReleaseNotes>
<Authors>Kaan Yarayan</Authors>
<Description>Local File Storage</Description>
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down