|
@@ -1,4 +1,5 @@
|
|
-using Microsoft.Extensions.Options;
|
|
|
|
|
|
+using Microsoft.AspNetCore.StaticFiles;
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
using XF.Domain.Dependency;
|
|
using XF.Domain.Dependency;
|
|
|
|
|
|
namespace FileStorage;
|
|
namespace FileStorage;
|
|
@@ -15,8 +16,16 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
_fileUploadOptions = optionsSnapshot;
|
|
_fileUploadOptions = optionsSnapshot;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- public FileMetadata Upload(string fileName, long length, string extraInfo, Stream fileData)
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 本地上传
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="fileName"></param>
|
|
|
|
+ /// <param name="length"></param>
|
|
|
|
+ /// <param name="extraInfo"></param>
|
|
|
|
+ /// <param name="fileData"></param>
|
|
|
|
+ /// <param name="client"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public FileMetadata Upload(string fileName, long length, string extraInfo, Stream fileData, string client)
|
|
{
|
|
{
|
|
if (fileName == null)
|
|
if (fileName == null)
|
|
{
|
|
{
|
|
@@ -24,10 +33,11 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
}
|
|
}
|
|
fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
|
|
fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
|
|
|
|
|
|
- var rv = UploadLoca(fileName, length, extraInfo, fileData);
|
|
|
|
- if (string.IsNullOrEmpty(rv))
|
|
|
|
|
|
+ var rv = UploadLoca(fileName, length, extraInfo, fileData,client);
|
|
|
|
+ if (!string.IsNullOrEmpty(rv))
|
|
{
|
|
{
|
|
FileMetadata fileModel = new FileMetadata();
|
|
FileMetadata fileModel = new FileMetadata();
|
|
|
|
+ fileModel.Client = client;
|
|
fileModel.FileName = fileName;
|
|
fileModel.FileName = fileName;
|
|
var ext = string.Empty;
|
|
var ext = string.Empty;
|
|
if (string.IsNullOrEmpty(fileName) == false)
|
|
if (string.IsNullOrEmpty(fileName) == false)
|
|
@@ -40,14 +50,59 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
fileModel.Length = length;
|
|
fileModel.Length = length;
|
|
fileModel.SaveMode = _fileUploadOptions.Value.Impt;
|
|
fileModel.SaveMode = _fileUploadOptions.Value.Impt;
|
|
fileModel.ExtraInfo = extraInfo;
|
|
fileModel.ExtraInfo = extraInfo;
|
|
|
|
+ fileModel.UploadTime = DateTime.Now;
|
|
_fileMetadataRepository.AddAsync(fileModel);
|
|
_fileMetadataRepository.AddAsync(fileModel);
|
|
return fileModel;
|
|
return fileModel;
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public async Task<Uri> GetFileUrlAsync(string id)
|
|
|
|
+ {
|
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(id);
|
|
|
|
+ if (fileMetadata != null)
|
|
|
|
+ {
|
|
|
|
+ return new Uri(_fileUploadOptions.Value.Local.VisDomain+fileMetadata.Path);
|
|
|
|
+ }
|
|
|
|
+ return new Uri("");
|
|
|
|
+ }
|
|
|
|
|
|
- private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData)
|
|
|
|
|
|
+ public (Stream stream, string contentType,string fileName) DownLoadFile(string id)
|
|
|
|
+ {
|
|
|
|
+ var fileMetadata = _fileMetadataRepository.Get(id);
|
|
|
|
+ if (fileMetadata!=null)
|
|
|
|
+ {
|
|
|
|
+ string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
|
|
+ var contentType = TaskGetFileContentTypeAsync(fileMetadata.Path);
|
|
|
|
+ var stream = File.OpenRead(filePath);
|
|
|
|
+ return (stream,contentType,fileMetadata.FileName);
|
|
|
|
+ }
|
|
|
|
+ return (null,"","");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<bool> DelFileAsync(string id)
|
|
|
|
+ {
|
|
|
|
+ var fileMetadata = await _fileMetadataRepository.GetAsync(id);
|
|
|
|
+ if (fileMetadata!=null)
|
|
|
|
+ {
|
|
|
|
+ string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileMetadata.Path);
|
|
|
|
+ await _fileMetadataRepository.RemoveAsync(id);
|
|
|
|
+ File.Delete(filePath);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private string TaskGetFileContentTypeAsync(string fileName)
|
|
|
|
+ {
|
|
|
|
+ string suffix = Path.GetExtension(fileName);
|
|
|
|
+ var provider = new FileExtensionContentTypeProvider();
|
|
|
|
+ var contentType = provider.Mappings[suffix];
|
|
|
|
+ return contentType;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData,string client)
|
|
{
|
|
{
|
|
var settings = _fileUploadOptions.Value.Local;
|
|
var settings = _fileUploadOptions.Value.Local;
|
|
string groupDir = "";
|
|
string groupDir = "";
|
|
@@ -57,8 +112,12 @@ public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(groupDir))
|
|
if (string.IsNullOrEmpty(groupDir))
|
|
{
|
|
{
|
|
- groupDir = "./uploads";
|
|
|
|
|
|
+ groupDir = "uploads";
|
|
}
|
|
}
|
|
|
|
+ string sub = DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
+
|
|
|
|
+ groupDir = Path.Combine(groupDir,client, sub);
|
|
|
|
+
|
|
string pathHeader = groupDir;
|
|
string pathHeader = groupDir;
|
|
|
|
|
|
string fulldir = GetFullPath(pathHeader);
|
|
string fulldir = GetFullPath(pathHeader);
|