|
@@ -1,6 +1,8 @@
|
|
|
-using Microsoft.AspNetCore.StaticFiles;
|
|
|
+using FileStorage.Extensions;
|
|
|
+using Microsoft.AspNetCore.StaticFiles;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using XF.Domain.Dependency;
|
|
|
+using XF.Domain.Exceptions;
|
|
|
|
|
|
namespace FileStorage;
|
|
|
|
|
@@ -57,19 +59,23 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public async Task<Uri> GetFileUrlAsync(string id)
|
|
|
+ public async Task<Uri> GetFileUrlAsync(string id,string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(id);
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x=>x.Id== id && x.Client== clientId);
|
|
|
if (fileMetadata != null)
|
|
|
{
|
|
|
- return new Uri(_fileUploadOptions.Value.Local.VisDomain+fileMetadata.Path);
|
|
|
+ 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(_fileUploadOptions.Value.Local.VisDomain + "file/files?id=" + id + "&expires=" + sTime + "&clientid=" + clientId + "&signature=" + signatureText);
|
|
|
+ //return new Uri(_fileUploadOptions.Value.Local.VisDomain+fileMetadata.Path);
|
|
|
}
|
|
|
- return new Uri("");
|
|
|
+ throw new UserFriendlyException("无权限访问");
|
|
|
}
|
|
|
|
|
|
- public (Stream stream, string contentType,string fileName) DownLoadFile(string id)
|
|
|
+ public (Stream stream, string contentType,string fileName) DownLoadFile(string id,string clientId)
|
|
|
{
|
|
|
- var fileMetadata = _fileMetadataRepository.Get(id);
|
|
|
+ var fileMetadata = _fileMetadataRepository.Get(x=>x.Id == id && x.Client == clientId);
|
|
|
if (fileMetadata!=null)
|
|
|
{
|
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
@@ -77,12 +83,12 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
var stream = File.OpenRead(filePath);
|
|
|
return (stream,contentType,fileMetadata.FileName);
|
|
|
}
|
|
|
- return (null,"","");
|
|
|
+ throw new UserFriendlyException("无权限访问");
|
|
|
}
|
|
|
|
|
|
- public async Task<bool> DelFileAsync(string id)
|
|
|
+ public async Task<bool> DelFileAsync(string id, string clientId)
|
|
|
{
|
|
|
- var fileMetadata = await _fileMetadataRepository.GetAsync(id);
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(x=>x.Id == id && x.Client == clientId);
|
|
|
if (fileMetadata!=null)
|
|
|
{
|
|
|
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
@@ -93,6 +99,30 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ 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("参数不合法");
|
|
|
+
|
|
|
+ if (long.Parse(paramData[1]) < ((long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds))
|
|
|
+ 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("无权限访问");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private string TaskGetFileContentTypeAsync(string fileName)
|
|
|
{
|
|
|
string suffix = Path.GetExtension(fileName);
|