48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Domain.Services
|
|
{
|
|
public static class ContentTypeHelper
|
|
{
|
|
public static string GetContentType(string fileName)
|
|
{
|
|
var extension = Path.GetExtension(fileName).ToLowerInvariant();
|
|
return extension switch
|
|
{
|
|
".pdf" => "application/pdf",
|
|
".doc" => "application/msword",
|
|
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
".xls" => "application/vnd.ms-excel",
|
|
".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
".png" => "image/png",
|
|
".jpg" or ".jpeg" => "image/jpeg",
|
|
".gif" => "image/gif",
|
|
".txt" => "text/plain",
|
|
_ => "application/octet-stream"
|
|
};
|
|
}
|
|
public static string ValidateFile(string filePath, string uploadsPath)
|
|
{
|
|
var message = "";
|
|
// Security: Prevent directory traversal attacks
|
|
var fullPath = Path.GetFullPath(filePath);
|
|
|
|
if (!fullPath.StartsWith(Path.GetFullPath(uploadsPath)))
|
|
{
|
|
message = "Invalid file path";
|
|
}
|
|
|
|
if (!System.IO.File.Exists(filePath))
|
|
{
|
|
message = "File not found";
|
|
}
|
|
return message;
|
|
}
|
|
}
|
|
}
|