|
@@ -12,7 +12,7 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
private readonly IFileMetadataRepository _fileMetadataRepository;
|
|
private readonly IFileMetadataRepository _fileMetadataRepository;
|
|
private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
|
|
private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
|
|
|
|
|
|
- public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
|
|
|
|
|
|
+ public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository, IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
|
|
{
|
|
{
|
|
_fileMetadataRepository = fileMetadataRepository;
|
|
_fileMetadataRepository = fileMetadataRepository;
|
|
_fileUploadOptions = optionsSnapshot;
|
|
_fileUploadOptions = optionsSnapshot;
|
|
@@ -26,8 +26,9 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
/// <param name="extraInfo"></param>
|
|
/// <param name="extraInfo"></param>
|
|
/// <param name="fileData"></param>
|
|
/// <param name="fileData"></param>
|
|
/// <param name="client"></param>
|
|
/// <param name="client"></param>
|
|
|
|
+ /// <param name="isVerification">是否验证文件格式</param>
|
|
/// <returns></returns>
|
|
/// <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)
|
|
if (fileName == null)
|
|
{
|
|
{
|
|
@@ -35,7 +36,7 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
}
|
|
}
|
|
fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
|
|
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))
|
|
if (!string.IsNullOrEmpty(rv))
|
|
{
|
|
{
|
|
FileMetadata fileModel = new FileMetadata();
|
|
FileMetadata fileModel = new FileMetadata();
|
|
@@ -59,9 +60,9 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
return null;
|
|
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)
|
|
if (fileMetadata != null)
|
|
{
|
|
{
|
|
long sTime = (long)(DateTime.Now.AddHours(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
|
|
long sTime = (long)(DateTime.Now.AddHours(1).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
|
|
@@ -75,34 +76,34 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
|
|
|
public async Task<Uri> GetFileUrlIndefinitelyAsync(string id, string clientId)
|
|
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, string clientId)
|
|
{
|
|
{
|
|
- var fileMetadata = _fileMetadataRepository.Get(x=>x.Id == id && x.Client == clientId);
|
|
|
|
- if (fileMetadata!=null)
|
|
|
|
|
|
+ var fileMetadata = _fileMetadataRepository.Get(x => x.Id == id && x.Client == clientId);
|
|
|
|
+ if (fileMetadata != null)
|
|
{
|
|
{
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
- var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
|
|
|
|
|
|
+ var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
|
|
var stream = File.OpenRead(filePath);
|
|
var stream = File.OpenRead(filePath);
|
|
- return (stream,contentType,fileMetadata.FileName);
|
|
|
|
|
|
+ return (stream, contentType, fileMetadata.FileName);
|
|
}
|
|
}
|
|
throw new UserFriendlyException("无权限访问");
|
|
throw new UserFriendlyException("无权限访问");
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<bool> DelFileAsync(string id, string clientId)
|
|
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);
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
await _fileMetadataRepository.RemoveAsync(id);
|
|
await _fileMetadataRepository.RemoveAsync(id);
|
|
@@ -114,17 +115,17 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
|
|
|
public async Task<string> GetFilePathAsync(string id, string clientId)
|
|
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);
|
|
string decryptText = DESExtensions.Decrypt(signature);
|
|
if (!string.IsNullOrEmpty(decryptText))
|
|
if (!string.IsNullOrEmpty(decryptText))
|
|
{
|
|
{
|
|
string[] paramData = decryptText.Split('|');
|
|
string[] paramData = decryptText.Split('|');
|
|
-
|
|
|
|
|
|
+
|
|
if (id != paramData[2] || clientId != paramData[0] || expires != paramData[1])
|
|
if (id != paramData[2] || clientId != paramData[0] || expires != paramData[1])
|
|
throw UserFriendlyException.SameMessage("参数不合法");
|
|
throw UserFriendlyException.SameMessage("参数不合法");
|
|
|
|
|
|
@@ -141,27 +142,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);
|
|
string suffix = Path.GetExtension(fileName);
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
@@ -170,7 +171,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;
|
|
var settings = _fileUploadOptions.Value.Local;
|
|
string groupDir = "";
|
|
string groupDir = "";
|
|
@@ -184,7 +185,7 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
}
|
|
}
|
|
string sub = DateTime.Now.ToString("yyyyMMdd");
|
|
string sub = DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
|
|
- groupDir = Path.Combine(groupDir,client, sub);
|
|
|
|
|
|
+ groupDir = Path.Combine(groupDir, client, sub);
|
|
|
|
|
|
string pathHeader = groupDir;
|
|
string pathHeader = groupDir;
|
|
|
|
|
|
@@ -198,34 +199,38 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
{
|
|
{
|
|
var dotPos = fileName.LastIndexOf('.');
|
|
var dotPos = fileName.LastIndexOf('.');
|
|
- if (dotPos==0)
|
|
|
|
|
|
+ if (dotPos == 0)
|
|
{
|
|
{
|
|
throw UserFriendlyException.SameMessage("文件没有格式,请重新上传文件");
|
|
throw UserFriendlyException.SameMessage("文件没有格式,请重新上传文件");
|
|
}
|
|
}
|
|
ext = fileName.Substring(dotPos + 1);
|
|
ext = fileName.Substring(dotPos + 1);
|
|
}
|
|
}
|
|
- //TODO验证文件格式
|
|
|
|
- var extList = new List<string>();
|
|
|
|
- extList.Add("txt");
|
|
|
|
- extList.Add("xls");
|
|
|
|
- extList.Add("xlsx");
|
|
|
|
- extList.Add("jpg");
|
|
|
|
- extList.Add("jpeg");
|
|
|
|
- extList.Add("png");
|
|
|
|
- extList.Add("doc");
|
|
|
|
- extList.Add("docx");
|
|
|
|
- extList.Add("rar");
|
|
|
|
- extList.Add("pdf");
|
|
|
|
- extList.Add("mp3");
|
|
|
|
- extList.Add("zip");
|
|
|
|
- extList.Add("wmv");
|
|
|
|
- extList.Add("mp4");
|
|
|
|
- extList.Add("svg");
|
|
|
|
- extList.Add("avi");
|
|
|
|
- extList.Add("m4a");
|
|
|
|
- if (!extList.Contains(ext))
|
|
|
|
- {
|
|
|
|
- throw UserFriendlyException.SameMessage("文件格式不正确,只能上传【doc,rar,jpg,pdf,mp3,xls,xlsx,zip,docx,wmv,mp4.png,svg,avijpeg.m4a.txt】格式文件");
|
|
|
|
|
|
+ if (isVerification.HasValue && isVerification == true)
|
|
|
|
+ {
|
|
|
|
+ //TODO验证文件格式
|
|
|
|
+ var extList = new List<string>();
|
|
|
|
+ extList.Add("txt");
|
|
|
|
+ extList.Add("xls");
|
|
|
|
+ extList.Add("xlsx");
|
|
|
|
+ extList.Add("jpg");
|
|
|
|
+ extList.Add("jpeg");
|
|
|
|
+ extList.Add("png");
|
|
|
|
+ extList.Add("doc");
|
|
|
|
+ extList.Add("docx");
|
|
|
|
+ extList.Add("rar");
|
|
|
|
+ extList.Add("pdf");
|
|
|
|
+ extList.Add("mp3");
|
|
|
|
+ extList.Add("zip");
|
|
|
|
+ extList.Add("wmv");
|
|
|
|
+ extList.Add("mp4");
|
|
|
|
+ extList.Add("svg");
|
|
|
|
+ extList.Add("avi");
|
|
|
|
+ //extList.Add("m4a");
|
|
|
|
+ if (!extList.Contains(ext))
|
|
|
|
+ {
|
|
|
|
+ // throw UserFriendlyException.SameMessage("文件格式不正确,只能上传【doc,rar,jpg,pdf,mp3,xls,xlsx,zip,docx,wmv,mp4.png,svg,avijpeg.m4a.txt】格式文件");
|
|
|
|
+ throw UserFriendlyException.SameMessage("文件格式不正确,只能上传【doc,rar,jpg,pdf,mp3,xls,xlsx,zip,docx,wmv,mp4.png,svg,avi,jpeg,txt】格式文件");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
var filename = $"{Guid.NewGuid().ToString().Replace("-", string.Empty)}.{ext}";
|
|
var filename = $"{Guid.NewGuid().ToString().Replace("-", string.Empty)}.{ext}";
|
|
var fullPath = Path.Combine(fulldir, filename);
|
|
var fullPath = Path.Combine(fulldir, filename);
|