123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using Microsoft.AspNetCore.StaticFiles;
- using Microsoft.Extensions.Options;
- using XF.Domain.Dependency;
- namespace FileStorage;
- public class DefaultFileStorage : IFileStorage, IScopeDependency
- {
- private readonly IFileMetadataRepository _fileMetadataRepository;
- private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
- public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
- {
- _fileMetadataRepository = fileMetadataRepository;
- _fileUploadOptions = optionsSnapshot;
- }
- /// <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)
- {
- fileName = "unkown";
- }
- fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
- var rv = UploadLoca(fileName, length, extraInfo, fileData,client);
- if (!string.IsNullOrEmpty(rv))
- {
- FileMetadata fileModel = new FileMetadata();
- fileModel.Client = client;
- fileModel.FileName = fileName;
- var ext = string.Empty;
- if (string.IsNullOrEmpty(fileName) == false)
- {
- var dotPos = fileName.LastIndexOf('.');
- ext = fileName[(dotPos + 1)..];
- }
- fileModel.FileExt = ext;
- fileModel.Path = rv;
- fileModel.Length = length;
- fileModel.SaveMode = _fileUploadOptions.Value.Impt;
- fileModel.ExtraInfo = extraInfo;
- fileModel.UploadTime = DateTime.Now;
- _fileMetadataRepository.AddAsync(fileModel);
- return fileModel;
- }
- 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("");
- }
- 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;
- string groupDir = "";
- if (settings != null)
- {
- groupDir = settings.Path;
- }
- if (string.IsNullOrEmpty(groupDir))
- {
- groupDir = "uploads";
- }
- string sub = DateTime.Now.ToString("yyyyMMdd");
- groupDir = Path.Combine(groupDir,client, sub);
- string pathHeader = groupDir;
- string fulldir = GetFullPath(pathHeader);
- if (!Directory.Exists(fulldir))
- {
- Directory.CreateDirectory(fulldir);
- }
- var ext = string.Empty;
- if (!string.IsNullOrEmpty(fileName))
- {
- var dotPos = fileName.LastIndexOf('.');
- ext = fileName.Substring(dotPos + 1);
- }
- var filename = $"{Guid.NewGuid().ToString().Replace("-", string.Empty)}.{ext}";
- var fullPath = Path.Combine(fulldir, filename);
- using (var fileStream = File.Create(fullPath))
- {
- fileData.CopyTo(fileStream);
- }
- fileData.Dispose();
- return (Path.Combine(pathHeader, filename));
- }
- private string GetFullPath(string path)
- {
- string rv = "";
- if (path.StartsWith("."))
- {
- rv = Path.Combine(Directory.GetCurrentDirectory(), path);
- }
- else
- {
- rv = path;
- }
- rv = Path.GetFullPath(rv);
- return rv;
- }
- }
|