文件上传功能添加,注意使用Path.Combine处理通用路径问题(windows||docker)

This commit is contained in:
owenchen committed 2026-09-03 16:09:54 +08:00
1 parent 7817c5374c
commit 244b59a3fb
135 files changed
+16937 -75

No files matched your search

@@ -1,4 +1,8 @@
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Form;
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Policy;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,7 +11,7 @@ using System.Threading.Tasks;
namespace CloudBuilder.Security.Policy
{
public class FileTypePolicy
public class FileTypePolicy : AbstractPolicy
{
public const string VIDEO = "V";
public const string AUDIO = "A";
@@ -15,6 +19,19 @@ namespace CloudBuilder.Security.Policy
public const string DOCUMENT = "D";
public const string LYRIC = "L";
public static FileTypePolicy instance = null;
public FileTypePolicy(IContext context) : base(context)
{
this.values = new string[] { VIDEO, AUDIO, LYRIC, PHOTO, DOCUMENT };
}
public static FileTypePolicy GetInstance(IApplicationService service)
{
instance = service.ServiceProvider.GetKeyedService<FileTypePolicy>(typeof(FileTypePolicy).Name);
return instance;
}
public static string GetType(string value)
{
if (string.IsNullOrEmpty(value)) return DOCUMENT;
@@ -36,7 +53,6 @@ namespace CloudBuilder.Security.Policy
return DOCUMENT;
}
public static bool IsAudio(string value)
{
return GetType(value) == AUDIO;
@@ -57,67 +73,74 @@ namespace CloudBuilder.Security.Policy
return GetType(value) == LYRIC;
}
public static string PackUrl(IApplicationService service, string createdBy, string orderKey, string transcodeIndc)
{
string url = string.Empty;
var webRootPath = service.Configuration["FileServiceSettings:RequestPath"];
var phy = service.Configuration["FileServiceSettings:PhysicalFileDirectory"];
var webRootPath = service.Configuration[ServiceSettingsPolicy.RequestPath] ?? "";
var phy = service.Configuration[ServiceSettingsPolicy.PhysicalFileDirectory] ?? "";
var fileExtension = Path.GetExtension(orderKey);//获取文件格式,拓展名
if (GetType(orderKey!) == FileTypePolicy.PHOTO)
{
if (transcodeIndc == YesNoPolicy.YES)
{
var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
var backup = (service.Configuration[ServiceSettingsPolicy.DmsFileBackupDirectory] ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
}
else
{
var scale = service.Configuration["FileServiceSettings:ScaleDmsFileDirectory"].Replace(phy, "");
var filePath = $"{scale.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
var scale = (service.Configuration[ServiceSettingsPolicy.DmsFileScaleDirectory] ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(scale, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
}
}
else if (GetType(orderKey!) == FileTypePolicy.AUDIO)
{
var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
var backup = (service.Configuration[ServiceSettingsPolicy.DmsFileBackupDirectory] ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
}
else if (GetType(orderKey!) == FileTypePolicy.VIDEO)
{
if (transcodeIndc == YesNoPolicy.YES)
{
var upload = service.Configuration["FileServiceSettings:UploadDmsFileDirectory"].Replace(phy, "");
var filePath = $"{upload.Replace("\\", "/")}/{createdBy}/m3u8/";
url = service.Configuration["applicationUrl"] + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + "/main.m3u8";
var upload = (service.Configuration[ServiceSettingsPolicy.DmsFileUploadDirectory] ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(upload, createdBy, "m3u8", "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + "/main.m3u8";
}
else
{
var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
var backup = (service.Configuration[ServiceSettingsPolicy.DmsFileBackupDirectory] ?? "").Replace(phy ?? "", "");
var filePathRel = Path.Combine(backup, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
url = service.Configuration[AppsettingsPolicy.ApplicationUrl] + webRootPath + filePath + orderKey;
}
}
return url;
}
public static string PackSourceVideoUrl(IApplicationService service, string createdBy, string orderKey)
{
var webRootPath = service.Configuration["FileServiceSettings:RequestPath"];
var phy = service.Configuration["FileServiceSettings:PhysicalFileDirectory"];
var upload = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
var webRootPath = service.Configuration[ServiceSettingsPolicy.RequestPath] ?? "";
var phy = service.Configuration[ServiceSettingsPolicy.PhysicalFileDirectory] ?? "";
var upload = (service.Configuration[ServiceSettingsPolicy.DmsFileBackupDirectory] ?? "").Replace(phy ?? "", "");
var fileExtension = Path.GetExtension(orderKey);//获取文件格式,拓展名
var filePath = $"{upload.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
return service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
// 使用Path.Combine拼接路径,再将系统分隔符转为URL标准正斜杠
var filePathRel = Path.Combine(upload, createdBy, fileExtension.Replace(".", ""), "");
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/') + "/";
return (service.Configuration[AppsettingsPolicy.ApplicationUrl] ?? "") + webRootPath + filePath + orderKey;
}
public static string PackFilePath(IApplicationService service, string createdBy, string orderKey)
{
var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"]!;
var backup = service.Configuration[ServiceSettingsPolicy.DmsFileBackupDirectory]!;
var fileExtension = Path.GetExtension(orderKey).Replace(".", "");//获取文件格式,拓展名
return Path.Combine(backup, createdBy, fileExtension, orderKey);
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.Security.Policy
{
public class ServiceSettingsPolicy
{
public const string DmsFileScaleDirectory = "FileServiceSettings:DmsFileScaleDirectory";
public const string PhysicalFileDirectory = "FileServiceSettings:PhysicalFileDirectory";
public const string DmsFileUploadDirectory = "FileServiceSettings:DmsFileUploadDirectory";
public const string DmsFileBackupDirectory = "FileServiceSettings:DmsFileBackupDirectory";
public const string RequestPath = "FileServiceSettings:RequestPath";
public const string ExcelOutputDirectory = "FileServiceSettings:ExcelOutputDirectory";
public const string FfmpegPath = "Ffmpeg:Path";
public const string FfmpegPictrue = "Ffmpeg:Pictrue";
public const string FfmpegArguments = "Ffmpeg:Arguments";
}
}