| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using SVETA.Api.Data.Domain; |
| | | 4 | | using SVETA.Api.Services.Interfaces; |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Drawing; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace SVETA.Api.Services.Implements |
| | | 13 | | { |
| | | 14 | | public class DiskStorageService : IDiskStorageService |
| | | 15 | | { |
| | | 16 | | private readonly UploadDownloadSettings _settings; |
| | | 17 | | readonly ImagesSettings _imagesSettings; |
| | | 18 | | |
| | 112 | 19 | | public DiskStorageService(IOptions<UploadDownloadSettings> settings, IOptions<ImagesSettings> imagesSettings) |
| | 112 | 20 | | { |
| | 112 | 21 | | _settings = settings.Value; |
| | 112 | 22 | | _imagesSettings = imagesSettings.Value; |
| | 112 | 23 | | } |
| | | 24 | | |
| | | 25 | | public string GenerateUniqueFileName(string fileName) |
| | 0 | 26 | | { |
| | 0 | 27 | | return GetRandomSuffixFileName(fileName); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public string GetFullPictureFileName(string fileName) |
| | 0 | 31 | | { |
| | 0 | 32 | | return _imagesSettings.ImageSavePath.TrimEnd('/') + "/" + fileName.Trim('/'); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public bool PictureExists(string fileName) |
| | 0 | 36 | | { |
| | 0 | 37 | | if (string.IsNullOrEmpty(fileName)) return false; |
| | | 38 | | |
| | 0 | 39 | | fileName = GetFullPictureFileName(fileName); |
| | 0 | 40 | | return File.Exists(fileName); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public Stream SaveUpload(IFormFile file, out string fileName) |
| | 0 | 44 | | { |
| | 0 | 45 | | fileName = GetRandomSuffixFileName(file.FileName); |
| | 0 | 46 | | var fullName = Path.Combine(_settings.UploadsSavePath, fileName); |
| | | 47 | | |
| | 0 | 48 | | using (var stream = file.OpenReadStream()) |
| | 0 | 49 | | { |
| | 0 | 50 | | using (var fileStream = new FileStream(fullName, FileMode.Create, FileAccess.Write)) |
| | 0 | 51 | | { |
| | 0 | 52 | | stream.CopyTo(fileStream); |
| | 0 | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | return File.OpenRead(fullName); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public string SaveDownload(string fileName, string fileContent, bool uniqueName = true) |
| | 0 | 60 | | { |
| | 0 | 61 | | fileName = uniqueName ? GetRandomSuffixFileName(fileName) : fileName; |
| | 0 | 62 | | var fullName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | | 63 | | |
| | 0 | 64 | | using (var stream = new StreamWriter(fullName, false, System.Text.Encoding.UTF8)) |
| | 0 | 65 | | { |
| | 0 | 66 | | stream.Write(fileContent); |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return _settings.DownloadUrl.TrimEnd('/') + "/" + fileName; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public string SaveDownload(string fileName, string fileContent, out string fullFileName) |
| | 0 | 73 | | { |
| | 0 | 74 | | fileName = GetRandomSuffixFileName(fileName); |
| | 0 | 75 | | fullFileName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | | 76 | | |
| | 0 | 77 | | using (var stream = new StreamWriter(fullFileName, false, System.Text.Encoding.UTF8)) |
| | 0 | 78 | | { |
| | 0 | 79 | | stream.Write(fileContent); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | return _settings.DownloadUrl.TrimEnd('/') + "/" + fileName; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public string SaveDownload(string fileName, Stream fileContent, out string fullFileName) |
| | 0 | 86 | | { |
| | 0 | 87 | | fileName = GetRandomSuffixFileName(fileName); |
| | 0 | 88 | | fullFileName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | | 89 | | |
| | 0 | 90 | | fileContent.Position = 0; |
| | 0 | 91 | | using (var stream = new FileStream(fullFileName, FileMode.Create)) |
| | 0 | 92 | | { |
| | 0 | 93 | | fileContent.CopyTo(stream); |
| | 0 | 94 | | } |
| | 0 | 95 | | fileContent.Position = 0; |
| | | 96 | | |
| | 0 | 97 | | return _settings.DownloadUrl.TrimEnd('/') + "/" + fileName; |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | public string SaveDownload(string fileName, Stream fileContent, bool uniqueName = true) |
| | 0 | 101 | | { |
| | 0 | 102 | | fileName = uniqueName ? GetRandomSuffixFileName(fileName) : fileName; |
| | 0 | 103 | | var fullName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | | 104 | | |
| | 0 | 105 | | using (var fileStream = File.Create(fullName)) |
| | 0 | 106 | | { |
| | 0 | 107 | | fileContent.Seek(0, SeekOrigin.Begin); |
| | 0 | 108 | | fileContent.CopyTo(fileStream); |
| | 0 | 109 | | } |
| | 0 | 110 | | fileContent.Seek(0, SeekOrigin.Begin); |
| | | 111 | | |
| | 0 | 112 | | return _settings.DownloadUrl.TrimEnd('/') + "/" + fileName; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | public Stream ReadDownload(string fileName) |
| | 0 | 116 | | { |
| | 0 | 117 | | var fullName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | 0 | 118 | | return File.OpenRead(fullName); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Загружает файл от клиента в Download каталог для возможности дальнешей загрузки другим клиентом |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="file">файл</param> |
| | | 125 | | /// <param name="getFullPath">true если вернуть файл с полным путем, иначе только название файла</param> |
| | | 126 | | /// <returns></returns> |
| | | 127 | | public async Task<string> SaveUploadToDownloadDir(IFormFile file, bool getFullPath = false) |
| | 0 | 128 | | { |
| | 0 | 129 | | string fileName = GetRandomSuffixFileName(file.FileName); |
| | 0 | 130 | | var fullName = Path.Combine(_settings.DownloadsSavePath, fileName); |
| | 0 | 131 | | using (var stream = new FileStream(fullName, FileMode.Create)) |
| | 0 | 132 | | await file.CopyToAsync(stream); |
| | 0 | 133 | | return getFullPath ? fullName : fileName; |
| | 0 | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | public string GetDownloadUrl(string filename) => _settings.DownloadUrl.TrimEnd('/') + "/" + filename; |
| | 0 | 137 | | public string GetDownloadPath(string filename) => Path.Combine(_settings.DownloadsSavePath, filename); |
| | | 138 | | |
| | | 139 | | string GetRandomSuffixFileName(string fileName) |
| | 0 | 140 | | { |
| | 0 | 141 | | return Path.GetFileNameWithoutExtension(fileName) + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension( |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | public bool GetPictureSize(string fileName, out int width, out int height) |
| | 0 | 145 | | { |
| | 0 | 146 | | width = 0; |
| | 0 | 147 | | height = 0; |
| | | 148 | | |
| | 0 | 149 | | if (!PictureExists(fileName)) return false; |
| | | 150 | | |
| | 0 | 151 | | fileName = GetFullPictureFileName(fileName); |
| | 0 | 152 | | using (var img = Image.FromFile(fileName)) |
| | 0 | 153 | | { |
| | 0 | 154 | | width = img.Width; |
| | 0 | 155 | | height = img.Height; |
| | 0 | 156 | | return true; |
| | | 157 | | } |
| | 0 | 158 | | } |
| | | 159 | | } |
| | | 160 | | } |