1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using FileStorage;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Options;
- using System.ComponentModel.DataAnnotations;
- using static System.Runtime.InteropServices.JavaScript.JSType;
- namespace FileHandlers
- {
- [Display(Name = "local")]
- public class FwLocalFileHandler:FwFileHandlerBase
- {
- private readonly IOptionsSnapshot<FileUploadOptions> _fileUploadOptions;
- public FwLocalFileHandler(IOptionsSnapshot<FileUploadOptions> fileUploadOptions)
- {
- _fileUploadOptions = fileUploadOptions;
- }
- public override string Upload(string fileName, long length, string extraInfo,Stream fileData)
- {
- var settings = _fileUploadOptions.Value.Settings.Where(x=> x.Key.ToLower()=="local").Select(x=>x.Value).FirstOrDefault();
- string groupDir = "";
- if(settings!=null)
- {
- groupDir = settings.FirstOrDefault().GroupLocation;
- }
- 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)
- {
- string rv = "";
- if (path.StartsWith("."))
- {
- rv = Path.Combine(Directory.GetCurrentDirectory(), path);
- }
- else
- {
- rv = path;
- }
- rv = Path.GetFullPath(rv);
- return rv;
- }
- }
- }
|