| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Services.ApiServices.DropBoxApi; |
| | | 4 | | using Services.DataServices; |
| | | 5 | | |
| | | 6 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 7 | | |
| | | 8 | | [Authorize(Roles = "Admin")] |
| | | 9 | | [Area("Studio2")] |
| | | 10 | | public class TransactionFilesController : Controller |
| | | 11 | | { |
| | | 12 | | private readonly DropboxApiClient _dropboxClient; |
| | | 13 | | private readonly OrdersDataServices _orderDataServices; |
| | | 14 | | |
| | 0 | 15 | | public TransactionFilesController(DropboxApiClient dropboxClient, |
| | 0 | 16 | | OrdersDataServices orderDataServices) |
| | | 17 | | { |
| | 0 | 18 | | _dropboxClient = dropboxClient; |
| | 0 | 19 | | _orderDataServices = orderDataServices; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | [HttpPost] |
| | | 23 | | public async Task<IActionResult> UploadFile(IFormFile file, [FromForm] List<int> orderIds) |
| | | 24 | | { |
| | 0 | 25 | | if (file == null || file.Length == 0) |
| | 0 | 26 | | return BadRequest(new { error = "Файл не выбран" }); |
| | | 27 | | |
| | 0 | 28 | | if (orderIds == null || orderIds.Count == 0) |
| | 0 | 29 | | return BadRequest(new { error = "Не указан ни один заказ" }); |
| | | 30 | | |
| | | 31 | | try |
| | | 32 | | { |
| | | 33 | | // Используем оригинальное имя файла |
| | 0 | 34 | | var fileName = file.FileName; |
| | | 35 | | |
| | | 36 | | // Сохраняем временный файл на сервере |
| | 0 | 37 | | var tempFilePath = Path.Combine(Path.GetTempPath(), fileName); |
| | 0 | 38 | | using (var stream = new FileStream(tempFilePath, FileMode.Create)) |
| | | 39 | | { |
| | 0 | 40 | | await file.CopyToAsync(stream); |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | var dropboxFolderPath = "/Приложения/Studio2/ФайлыЗаказов"; |
| | 0 | 44 | | await _dropboxClient.UploadFileAsync(tempFilePath, dropboxFolderPath, fileName); |
| | | 45 | | |
| | 0 | 46 | | var fullPath = $"{dropboxFolderPath}/{fileName}"; |
| | | 47 | | |
| | 0 | 48 | | foreach (var orderId in orderIds) |
| | | 49 | | { |
| | 0 | 50 | | await _orderDataServices.SetTransactionDocumentAsync(orderId, fullPath); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Удаляем временный файл |
| | 0 | 54 | | System.IO.File.Delete(tempFilePath); |
| | | 55 | | |
| | 0 | 56 | | return Ok(new { message = "Файл успешно загружен", fullPath }); |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | return StatusCode(500, new { error = $"Ошибка при загрузке файла: {ex.Message}" }); |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | [HttpGet] |
| | | 65 | | public async Task<IActionResult> DownloadFile(int orderId) |
| | | 66 | | { |
| | 0 | 67 | | var dropboxFilePath = await _orderDataServices.GetTransactionDocument(orderId); |
| | 0 | 68 | | if (string.IsNullOrEmpty(dropboxFilePath)) |
| | 0 | 69 | | return NotFound(new { error = "Файл для этого заказа не найден" }); |
| | | 70 | | try |
| | | 71 | | { |
| | 0 | 72 | | var lastSlashIndex = dropboxFilePath.LastIndexOf('/'); |
| | 0 | 73 | | var folderPath = dropboxFilePath.Substring(0, lastSlashIndex); |
| | 0 | 74 | | var fileName = dropboxFilePath.Substring(lastSlashIndex + 1); |
| | | 75 | | |
| | 0 | 76 | | var downloadLink = await _dropboxClient.GetDownloadLinkAsync(folderPath, fileName); |
| | 0 | 77 | | return Redirect(downloadLink); |
| | | 78 | | } |
| | 0 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 0 | 81 | | return StatusCode(500, new { error = $"Ошибка при получении ссылки: {ex.Message}" }); |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | [HttpGet] |
| | | 86 | | [HttpHead] |
| | | 87 | | public async Task<IActionResult> GetThumbnail(int orderId) |
| | | 88 | | { |
| | | 89 | | try |
| | | 90 | | { |
| | 0 | 91 | | var dropboxFilePath = await _orderDataServices.GetTransactionDocument(orderId); |
| | 0 | 92 | | if (string.IsNullOrEmpty(dropboxFilePath)) |
| | 0 | 93 | | return NotFound(new { error = "Файл для этого заказа не найден" }); |
| | | 94 | | |
| | 0 | 95 | | var lastSlashIndex = dropboxFilePath.LastIndexOf('/'); |
| | 0 | 96 | | var folderPath = dropboxFilePath.Substring(0, lastSlashIndex); |
| | 0 | 97 | | var fileName = dropboxFilePath.Substring(lastSlashIndex + 1); |
| | | 98 | | |
| | | 99 | | // Определяем MIME-тип по расширению файла |
| | 0 | 100 | | var extension = Path.GetExtension(fileName).ToLowerInvariant(); |
| | 0 | 101 | | var contentType = extension switch |
| | 0 | 102 | | { |
| | 0 | 103 | | ".pdf" => "application/pdf", |
| | 0 | 104 | | ".jpg" or ".jpeg" => "image/jpeg", |
| | 0 | 105 | | ".png" => "image/png", |
| | 0 | 106 | | ".gif" => "image/gif", |
| | 0 | 107 | | ".bmp" => "image/bmp", |
| | 0 | 108 | | ".webp" => "image/webp", |
| | 0 | 109 | | ".doc" => "application/msword", |
| | 0 | 110 | | ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| | 0 | 111 | | ".xls" => "application/vnd.ms-excel", |
| | 0 | 112 | | ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| | 0 | 113 | | _ => "application/octet-stream" |
| | 0 | 114 | | }; |
| | | 115 | | |
| | | 116 | | // Для HEAD запроса возвращаем только заголовки |
| | 0 | 117 | | if (Request.Method == "HEAD") |
| | | 118 | | { |
| | 0 | 119 | | Response.ContentType = contentType; |
| | 0 | 120 | | return NoContent(); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Для GET запроса загружаем и возвращаем файл |
| | 0 | 124 | | var fileBytes = await _dropboxClient.DownloadFileAsync(folderPath, fileName); |
| | 0 | 125 | | return File(fileBytes, contentType); |
| | | 126 | | } |
| | 0 | 127 | | catch (Exception ex) |
| | | 128 | | { |
| | 0 | 129 | | return NotFound($"Ошибка при загрузке миниатюры: {ex.Message}"); |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | [HttpGet] |
| | | 134 | | public async Task<IActionResult> GetFileForPreview(int orderId) |
| | | 135 | | { |
| | | 136 | | try |
| | | 137 | | { |
| | 0 | 138 | | var dropboxFilePath = await _orderDataServices.GetTransactionDocument(orderId); |
| | 0 | 139 | | if (string.IsNullOrEmpty(dropboxFilePath)) |
| | 0 | 140 | | return NotFound(new { error = "Файл для этого заказа не найден" }); |
| | | 141 | | |
| | 0 | 142 | | var lastSlashIndex = dropboxFilePath.LastIndexOf('/'); |
| | 0 | 143 | | var folderPath = dropboxFilePath.Substring(0, lastSlashIndex); |
| | 0 | 144 | | var fileName = dropboxFilePath.Substring(lastSlashIndex + 1); |
| | | 145 | | |
| | 0 | 146 | | var fileBytes = await _dropboxClient.DownloadFileAsync(folderPath, fileName); |
| | | 147 | | |
| | | 148 | | // Определяем MIME-тип по расширению файла |
| | 0 | 149 | | var extension = Path.GetExtension(fileName).ToLowerInvariant(); |
| | 0 | 150 | | var contentType = extension switch |
| | 0 | 151 | | { |
| | 0 | 152 | | ".pdf" => "application/pdf", |
| | 0 | 153 | | ".jpg" or ".jpeg" => "image/jpeg", |
| | 0 | 154 | | ".png" => "image/png", |
| | 0 | 155 | | ".gif" => "image/gif", |
| | 0 | 156 | | ".bmp" => "image/bmp", |
| | 0 | 157 | | ".webp" => "image/webp", |
| | 0 | 158 | | ".doc" => "application/msword", |
| | 0 | 159 | | ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| | 0 | 160 | | ".xls" => "application/vnd.ms-excel", |
| | 0 | 161 | | ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| | 0 | 162 | | _ => "application/octet-stream" |
| | 0 | 163 | | }; |
| | | 164 | | |
| | 0 | 165 | | return File(fileBytes, contentType); |
| | | 166 | | } |
| | 0 | 167 | | catch (Exception ex) |
| | | 168 | | { |
| | 0 | 169 | | return NotFound($"Ошибка при загрузке файла: {ex.Message}"); |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | } |