|
@@ -1,5 +1,6 @@
|
|
|
using FileStorage.Extensions;
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using XF.Domain.Dependency;
|
|
|
using XF.Domain.Exceptions;
|
|
@@ -11,11 +12,15 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
|
|
|
private readonly IFileMetadataRepository _fileMetadataRepository;
|
|
|
private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
|
|
|
+ private readonly IOptionsSnapshot<FileExtConfiguration> _fileExtOptions;
|
|
|
+ private readonly ILogger<DefaultFileStorage> _logger;
|
|
|
|
|
|
- public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
|
|
|
+ public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository, IOptionsSnapshot<StorageConfiguration> optionsSnapshot, ILogger<DefaultFileStorage> logger, IOptionsSnapshot<FileExtConfiguration> fileExtOptions)
|
|
|
{
|
|
|
_fileMetadataRepository = fileMetadataRepository;
|
|
|
_fileUploadOptions = optionsSnapshot;
|
|
|
+ _logger = logger;
|
|
|
+ _fileExtOptions = fileExtOptions;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -26,8 +31,9 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
/// <param name="extraInfo"></param>
|
|
|
/// <param name="fileData"></param>
|
|
|
/// <param name="client"></param>
|
|
|
+ /// <param name="isVerification">是否验证文件格式</param>
|
|
|
/// <returns></returns>
|
|
|
- public async Task<FileMetadata> UploadAsync(string fileName, long length, string extraInfo, Stream fileData, string client)
|
|
|
+ public async Task<FileMetadata> UploadAsync(string fileName, long length, string extraInfo, Stream fileData, string client, bool? isVerification)
|
|
|
{
|
|
|
if (fileName == null)
|
|
|
{
|
|
@@ -35,7 +41,7 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
}
|
|
|
fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
|
|
|
|
|
|
- var rv = UploadLoca(fileName, length, extraInfo, fileData,client);
|
|
|
+ var rv = UploadLoca(fileName, length, extraInfo, fileData, client, isVerification);
|
|
|
if (!string.IsNullOrEmpty(rv))
|
|
|
{
|
|
|
FileMetadata fileModel = new FileMetadata();
|
|
@@ -59,9 +65,9 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public async Task<Uri> GetFileUrlAsync(string id,string clientId)
|
|
|
+ public async Task<Uri> GetFileUrlAsync(string id, string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(x=>x.Id== id && x.Client== clientId);
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
if (fileMetadata != null)
|
|
|
{
|
|
|
long sTime = (long)(DateTime.Now.AddHours(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
|
|
@@ -75,34 +81,34 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
|
|
|
public async Task<Uri> GetFileUrlIndefinitelyAsync(string id, string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
- if (fileMetadata != null)
|
|
|
- {
|
|
|
- long sTime = (long)(DateTime.Now.AddHours(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
|
|
|
- string encryptText = clientId + "|" + sTime + "|" + id;
|
|
|
- var signatureText = DESExtensions.Encrypt(encryptText);
|
|
|
- return new Uri("/file/files_indefinitely?id=" + id + "&expires=" + sTime + "&clientid=" + clientId + "&signature=" + signatureText);
|
|
|
- }
|
|
|
- throw new UserFriendlyException("无权限访问");
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
+ if (fileMetadata != null)
|
|
|
+ {
|
|
|
+ long sTime = (long)(DateTime.Now.AddHours(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
|
|
|
+ string encryptText = clientId + "|" + sTime + "|" + id;
|
|
|
+ var signatureText = DESExtensions.Encrypt(encryptText);
|
|
|
+ return new Uri("/file/files_indefinitely?id=" + id + "&expires=" + sTime + "&clientid=" + clientId + "&signature=" + signatureText);
|
|
|
+ }
|
|
|
+ throw new UserFriendlyException("无权限访问");
|
|
|
}
|
|
|
|
|
|
- public (Stream stream, string contentType,string fileName) DownLoadFile(string id,string clientId)
|
|
|
+ public (Stream stream, string contentType, string fileName) DownLoadFile(string id)
|
|
|
{
|
|
|
- var fileMetadata = _fileMetadataRepository.Get(x=>x.Id == id && x.Client == clientId);
|
|
|
- if (fileMetadata!=null)
|
|
|
+ var fileMetadata = _fileMetadataRepository.Get(x => x.Id == id);
|
|
|
+ if (fileMetadata != null)
|
|
|
{
|
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
|
- var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
|
|
|
+ var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
|
|
|
var stream = File.OpenRead(filePath);
|
|
|
- return (stream,contentType,fileMetadata.FileName);
|
|
|
+ return (stream, contentType, fileMetadata.FileName);
|
|
|
}
|
|
|
throw new UserFriendlyException("无权限访问");
|
|
|
}
|
|
|
|
|
|
public async Task<bool> DelFileAsync(string id, string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(x=>x.Id == id && x.Client == clientId);
|
|
|
- if (fileMetadata!=null)
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
+ if (fileMetadata != null)
|
|
|
{
|
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
|
await _fileMetadataRepository.RemoveAsync(id);
|
|
@@ -114,17 +120,17 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
|
|
|
public async Task<string> GetFilePathAsync(string id, string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
- return fileMetadata?.Path!;
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
+ return fileMetadata?.Path!;
|
|
|
}
|
|
|
|
|
|
- public async Task<string> GetFilePath(string id, string expires, string clientId, string signature)
|
|
|
+ public async Task<string> GetFilePath(string id, string expires, string clientId, string signature)
|
|
|
{
|
|
|
string decryptText = DESExtensions.Decrypt(signature);
|
|
|
if (!string.IsNullOrEmpty(decryptText))
|
|
|
{
|
|
|
string[] paramData = decryptText.Split('|');
|
|
|
-
|
|
|
+
|
|
|
if (id != paramData[2] || clientId != paramData[0] || expires != paramData[1])
|
|
|
throw UserFriendlyException.SameMessage("参数不合法");
|
|
|
|
|
@@ -141,27 +147,27 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
}
|
|
|
|
|
|
|
|
|
- public async Task<string> GetFilePathIndefinitely(string id, string expires, string clientId, string signature)
|
|
|
- {
|
|
|
- string decryptText = DESExtensions.Decrypt(signature);
|
|
|
- if (!string.IsNullOrEmpty(decryptText))
|
|
|
- {
|
|
|
- string[] paramData = decryptText.Split('|');
|
|
|
+ public async Task<string> GetFilePathIndefinitely(string id, string expires, string clientId, string signature)
|
|
|
+ {
|
|
|
+ string decryptText = DESExtensions.Decrypt(signature);
|
|
|
+ if (!string.IsNullOrEmpty(decryptText))
|
|
|
+ {
|
|
|
+ string[] paramData = decryptText.Split('|');
|
|
|
|
|
|
- if (id != paramData[2] || clientId != paramData[0] || expires != paramData[1])
|
|
|
- throw UserFriendlyException.SameMessage("参数不合法");
|
|
|
+ if (id != paramData[2] || clientId != paramData[0] || expires != paramData[1])
|
|
|
+ throw UserFriendlyException.SameMessage("参数不合法");
|
|
|
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
- if (fileMetadata == null)
|
|
|
- throw UserFriendlyException.SameMessage("无权限访问");
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x => x.Id == id && x.Client == clientId);
|
|
|
+ if (fileMetadata == null)
|
|
|
+ throw UserFriendlyException.SameMessage("无权限访问");
|
|
|
|
|
|
- return GetFullPath(fileMetadata.Path);
|
|
|
- }
|
|
|
- throw UserFriendlyException.SameMessage("无权限访问");
|
|
|
- }
|
|
|
+ return GetFullPath(fileMetadata.Path);
|
|
|
+ }
|
|
|
+ throw UserFriendlyException.SameMessage("无权限访问");
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- private string TaskGetFileContentTypeAsync(string fileName)
|
|
|
+ private string TaskGetFileContentTypeAsync(string fileName)
|
|
|
{
|
|
|
string suffix = Path.GetExtension(fileName);
|
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
@@ -170,7 +176,7 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
}
|
|
|
|
|
|
|
|
|
- private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData,string client)
|
|
|
+ private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData, string client, bool? isVerification)
|
|
|
{
|
|
|
var settings = _fileUploadOptions.Value.Local;
|
|
|
string groupDir = "";
|
|
@@ -184,22 +190,38 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
}
|
|
|
string sub = DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
|
|
- groupDir = Path.Combine(groupDir,client, sub);
|
|
|
+ groupDir = Path.Combine(groupDir, client, sub);
|
|
|
|
|
|
string pathHeader = groupDir;
|
|
|
|
|
|
string fulldir = GetFullPath(pathHeader);
|
|
|
+
|
|
|
+ _logger.LogInformation("路径:"+fulldir);
|
|
|
if (!Directory.Exists(fulldir))
|
|
|
{
|
|
|
Directory.CreateDirectory(fulldir);
|
|
|
}
|
|
|
-
|
|
|
+ _logger.LogInformation("已执行");
|
|
|
var ext = string.Empty;
|
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
|
{
|
|
|
var dotPos = fileName.LastIndexOf('.');
|
|
|
+ if (dotPos == 0)
|
|
|
+ {
|
|
|
+ throw UserFriendlyException.SameMessage("文件没有格式,请重新上传文件");
|
|
|
+ }
|
|
|
ext = fileName.Substring(dotPos + 1);
|
|
|
}
|
|
|
+ if (isVerification.HasValue && isVerification == true)
|
|
|
+ {
|
|
|
+ //新
|
|
|
+ var extList = _fileExtOptions.Value.FileExt.Split(",").ToList();
|
|
|
+ //extList.Add("m4a");
|
|
|
+ if (!extList.Contains(ext.ToLower()))
|
|
|
+ {
|
|
|
+ throw UserFriendlyException.SameMessage($"文件格式不正确,只能上传【{_fileExtOptions.Value.FileExt}】格式文件");
|
|
|
+ }
|
|
|
+ }
|
|
|
var filename = $"{Guid.NewGuid().ToString().Replace("-", string.Empty)}.{ext}";
|
|
|
var fullPath = Path.Combine(fulldir, filename);
|
|
|
using (var fileStream = File.Create(fullPath))
|