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 _fileUploadOptions; public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot optionsSnapshot) { _fileMetadataRepository = fileMetadataRepository; _fileUploadOptions = optionsSnapshot; } /// /// 本地上传 /// /// /// /// /// /// /// 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 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 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; } }