|
@@ -1,20 +1,101 @@
|
|
-using XF.Domain.Dependency;
|
|
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
|
+using XF.Domain.Dependency;
|
|
|
|
|
|
namespace FileStorage;
|
|
namespace FileStorage;
|
|
|
|
|
|
public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
public class DefaultFileStorage : IFileStorage, IScopeDependency
|
|
{
|
|
{
|
|
- public string Path { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public string FileName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public string FileExt { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public long Length { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public string SaveMode { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public string ExtraInfo { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public Stream DataStream { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
- public DateTime UploadTime { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
-
|
|
|
|
- public string GetID()
|
|
|
|
|
|
+
|
|
|
|
+ private readonly IFileMetadataRepository _fileMetadataRepository;
|
|
|
|
+ private readonly IOptionsSnapshot<StorageConfiguration> _fileUploadOptions;
|
|
|
|
+
|
|
|
|
+ public DefaultFileStorage(IFileMetadataRepository fileMetadataRepository,IOptionsSnapshot<StorageConfiguration> optionsSnapshot)
|
|
|
|
+ {
|
|
|
|
+ _fileMetadataRepository = fileMetadataRepository;
|
|
|
|
+ _fileUploadOptions = optionsSnapshot;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public FileMetadata Upload(string fileName, long length, string extraInfo, Stream fileData)
|
|
|
|
+ {
|
|
|
|
+ if (fileName == null)
|
|
|
|
+ {
|
|
|
|
+ fileName = "unkown";
|
|
|
|
+ }
|
|
|
|
+ fileName = fileName.Replace("<", "").Replace(">", "").Replace(" ", "");
|
|
|
|
+
|
|
|
|
+ var rv = UploadLoca(fileName, length, extraInfo, fileData);
|
|
|
|
+ if (string.IsNullOrEmpty(rv))
|
|
|
|
+ {
|
|
|
|
+ FileMetadata fileModel = new FileMetadata();
|
|
|
|
+ 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;
|
|
|
|
+ _fileMetadataRepository.AddAsync(fileModel);
|
|
|
|
+ return fileModel;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private string UploadLoca(string fileName, long length, string extraInfo, Stream fileData)
|
|
|
|
+ {
|
|
|
|
+ var settings = _fileUploadOptions.Value.Local;
|
|
|
|
+ string groupDir = "";
|
|
|
|
+ if (settings != null)
|
|
|
|
+ {
|
|
|
|
+ groupDir = settings.Path;
|
|
|
|
+ }
|
|
|
|
+ if (string.IsNullOrEmpty(groupDir))
|
|
|
|
+ {
|
|
|
|
+ groupDir = "./uploads";
|
|
|
|
+ }
|
|
|
|
+ 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)
|
|
{
|
|
{
|
|
- throw new NotImplementedException();
|
|
|
|
|
|
+ string rv = "";
|
|
|
|
+ if (path.StartsWith("."))
|
|
|
|
+ {
|
|
|
|
+ rv = Path.Combine(Directory.GetCurrentDirectory(), path);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ rv = path;
|
|
|
|
+ }
|
|
|
|
+ rv = Path.GetFullPath(rv);
|
|
|
|
+ return rv;
|
|
}
|
|
}
|
|
}
|
|
}
|