| | | 1 | | using Services.ApiServices.DropBoxApi; |
| | | 2 | | |
| | | 3 | | namespace Services.LabelServices; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Разовый перенос существующих PDF-этикеток из Dropbox-папки в новое ServerFileStorage-хранилище. |
| | | 7 | | /// Не recurring — запускается вручную один раз при переходе с Dropbox. Идемпотентна: уже |
| | | 8 | | /// перенесённые файлы пропускаются, безопасно перезапускать при сбое на середине. |
| | | 9 | | /// </summary> |
| | | 10 | | public class DropboxToServerFileStorageMigrationJob |
| | | 11 | | { |
| | | 12 | | private const string DropboxFolderPath = "/Приложения/Studio2/Этикетки"; |
| | 0 | 13 | | private static readonly TimeSpan DelayBetweenFiles = TimeSpan.FromMilliseconds(100); |
| | | 14 | | |
| | | 15 | | private readonly DropboxApiClient _dropboxApiClient; |
| | | 16 | | private readonly ILabelFileStorage _labelFileStorage; |
| | | 17 | | |
| | 0 | 18 | | public DropboxToServerFileStorageMigrationJob(DropboxApiClient dropboxApiClient, ILabelFileStorage labelFileStorage) |
| | | 19 | | { |
| | 0 | 20 | | _dropboxApiClient = dropboxApiClient; |
| | 0 | 21 | | _labelFileStorage = labelFileStorage; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<string> ExecuteAsync() |
| | | 25 | | { |
| | | 26 | | // GetPdfShipmentNumberIndexAsync обходит пагинацию Dropbox list_folder (HasMore/ListFolderContinueAsync), |
| | | 27 | | // в отличие от GetFilesListAsync — на большой папке тот молча вернул бы неполный список. |
| | 0 | 28 | | var baseNames = await _dropboxApiClient.GetPdfShipmentNumberIndexAsync(DropboxFolderPath); |
| | | 29 | | |
| | 0 | 30 | | var migrated = 0; |
| | 0 | 31 | | var skipped = 0; |
| | 0 | 32 | | var failed = 0; |
| | 0 | 33 | | var failures = new List<string>(); |
| | | 34 | | |
| | 0 | 35 | | foreach (var baseName in baseNames) |
| | | 36 | | { |
| | 0 | 37 | | var fileName = $"{baseName}.pdf"; |
| | | 38 | | try |
| | | 39 | | { |
| | 0 | 40 | | if (await _labelFileStorage.ExistsAsync(fileName)) |
| | | 41 | | { |
| | 0 | 42 | | skipped++; |
| | 0 | 43 | | continue; |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | var bytes = await _dropboxApiClient.DownloadFileAsync(DropboxFolderPath, fileName); |
| | 0 | 47 | | if (bytes is not { Length: > 0 }) |
| | | 48 | | { |
| | 0 | 49 | | failed++; |
| | 0 | 50 | | failures.Add($"{fileName}: пустой файл"); |
| | 0 | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | await _labelFileStorage.UploadAsync(bytes, fileName, overwrite: false); |
| | 0 | 55 | | migrated++; |
| | 0 | 56 | | await Task.Delay(DelayBetweenFiles); |
| | 0 | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | failed++; |
| | 0 | 61 | | failures.Add($"{fileName}: {ex.Message}"); |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var summary = $"Migrated {migrated}, skipped {skipped} (already present), failed {failed}"; |
| | 0 | 66 | | return failures.Count > 0 |
| | 0 | 67 | | ? $"{summary}. Failures: {string.Join("; ", failures.Take(20))}" |
| | 0 | 68 | | : summary; |
| | 0 | 69 | | } |
| | | 70 | | } |