22{
33 public class FileUploadHelper
44 {
5- // Base directory for all uploads
65 private static readonly string BaseUploadDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , "wwwroot" , "uploads" ) ;
76
8- // Allowed file extensions for upload
97 private static readonly string [ ] AllowedFileExtensions = [ ".jpg" , ".jpeg" , ".png" , ".gif" , ".pdf" , ".docx" , ".mp4" , ".mov" , ".avi" , ".mkv" ] ;
10- // Maximum file size allowed (500 MB for videos)
118 private const long MaxFileSize = 500 * 1024 * 1024 ;
129
1310 // Ensure the directory exists
@@ -19,16 +16,13 @@ private static void EnsureFolderExists(string folderPath)
1916 }
2017 }
2118
22- // Uploads a file and returns its relative URL
2319 public static async Task < string > UploadFileAsync ( IFormFile file , string folder )
2420 {
25- // Validate the file
2621 if ( file == null || file . Length == 0 || file . Length > MaxFileSize )
2722 {
2823 return null ;
2924 }
3025
31- // Get file extension and check if it's allowed
3226 var fileExtension = Path . GetExtension ( file . FileName ) . ToLower ( ) ;
3327 if ( ! AllowedFileExtensions . Contains ( fileExtension ) )
3428 {
@@ -37,74 +31,50 @@ public static async Task<string> UploadFileAsync(IFormFile file, string folder)
3731
3832 try
3933 {
40- // Full path of the folder for the specific file type (e.g., images, videos)
41- var uploadsFolder = Path . Combine ( BaseUploadDirectory , folder ) ; //"C:\\Projects\\EdufyAPI\\wwwroot\\images"
34+ var uploadsFolder = Path . Combine ( BaseUploadDirectory , folder ) ;
4235
43- // Ensure the upload folder exists
4436 EnsureFolderExists ( uploadsFolder ) ;
4537
46- // Generate a unique file name using GUID
47- var fileName = $ "{ Guid . NewGuid ( ) } { fileExtension } "; // "d3c72b6d-2c2d-4f30-8a87-6b2f3f5d2331.jpg"
38+ var fileName = $ "{ Guid . NewGuid ( ) } { fileExtension } ";
4839
49- // Combine the folder path and file name to get the full file path
50- var filePath = Path . Combine ( uploadsFolder , fileName ) ; // "C:\\Projects\\EdufyAPI\\wwwroot\\images\\d3c72b6d-2c2d-4f30-8a87-6b2f3f5d2331.jpg"
40+ var filePath = Path . Combine ( uploadsFolder , fileName ) ;
5141
52- // Save the file to the specified path asynchronously
53- /*If:
54- filePath = "C:\\Projects\\EdufyAPI\\wwwroot\\images\\d3c72b6d-2c2d-4f30-8a87-6b2f3f5d2331.jpg"
55- imageFile is an uploaded file from a form with a size of 2 MB
5642
57- The code:
58- Creates a new file named d3c72b6d-2c2d-4f30-8a87-6b2f3f5d2331.jpg in the wwwroot/images folder.
59- Asynchronously writes the contents of the uploaded image into this file.
60- Automatically closes the file once the writing process is complete.
61- This allows the image to be saved to the server while maintaining good application performance.*/
6243 using ( var stream = new FileStream ( filePath , FileMode . Create ) )
6344 {
6445 await file . CopyToAsync ( stream ) ;
6546 }
6647
67- // Return the relative URL for accessing the file
68- return $ "/uploads/{ folder } /{ fileName } "; //This makes the file accessible on the web after being saved to the server.
48+ return $ "/uploads/{ folder } /{ fileName } ";
6949 }
7050 catch ( Exception ex )
7151 {
72- // Log error if upload fails
7352 Console . WriteLine ( $ "File upload failed: { ex . Message } ") ;
7453
75- // Return null if the file could not be saved
7654 return null ;
7755 }
7856 }
7957
80- // Deletes a file from the specified folder
8158 public static void DeleteFile ( string fileUrl )
8259 {
83- // Validate the file URL
8460 if ( string . IsNullOrEmpty ( fileUrl ) )
8561 {
8662 return ;
8763 }
8864
8965 try
9066 {
91- // Extract the file path from the URL
92- // Convert from "/uploads/images/example.jpg" to "\uploads\images\example.jpg" ,because URLs use forward slashes (/), but Windows file paths use backslashes (\).
93- /* Path.DirectorySeparatorChar is a property provides the default character used to separate directories in file paths, depending on the operating system:
94- On Windows: It is a backslash (\)
95- On Linux and macOS: It is a forward slash(/)*/
67+
9668 var relativePath = fileUrl . Replace ( "/" , Path . DirectorySeparatorChar . ToString ( ) ) ;
9769 var filePath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "wwwroot" , relativePath ) ; // "C:\Projects\EdufyAPI\wwwroot\uploads\images\example.jpg"
9870
99- // Check if the file exists before attempting to delete
10071 if ( File . Exists ( filePath ) )
10172 {
10273 File . Delete ( filePath ) ;
10374 }
10475 }
10576 catch ( Exception ex )
10677 {
107- // Log error if deletion fails
10878 Console . WriteLine ( $ "File deletion failed: { ex . Message } ") ;
10979 }
11080 }
0 commit comments