@solidpepper/solidpepper-upload-youbooky 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,11 @@ import { FormsModule } from '@angular/forms';
7
7
  import * as i3 from '@ngx-translate/core';
8
8
  import { TranslateService, TranslateModule } from '@ngx-translate/core';
9
9
  import { Router, RouterModule } from '@angular/router';
10
- import { HttpClient, HttpHeaders } from '@angular/common/http';
10
+ import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
11
11
  import { firstValueFrom } from 'rxjs';
12
12
  import { DocumentsService, SettingsService, UsersService } from '@solidpepper/solidpepper-service';
13
13
  import * as pdfjsLib from 'pdfjs-dist';
14
+ import JSZip from 'jszip';
14
15
  import { FlipbookDataService, FlipbookComponent } from '@solidpepper/solidpepper-flipbook';
15
16
  import { spIconPdf, spIconUpload, spIconUser, spIconBookOpen, spIconCheck, SpIconComponent } from '@solidpepper/solidpepper-icons';
16
17
 
@@ -31,6 +32,9 @@ var fr = {
31
32
  IMPORT_PROGRESS_SUBTITLE: 'Étapes du traitement',
32
33
  TOTAL_PROGRESS: 'Progression globale',
33
34
  CREATING_DOCUMENT: 'Création du document...',
35
+ PREPARING_ARCHIVE: "Préparation de l'archive compressée...",
36
+ UPLOADING_ARCHIVE_PROGRESS: "Envoi de l'archive au serveur ({{progress}}%)...",
37
+ PROCESSING_PAGE_PROGRESS: 'Traitement de la page {{current}} sur {{total}}...',
34
38
  UPLOADING_PAGES: 'Téléversement des pages',
35
39
  UPLOADING_PAGE_PROGRESS: 'Téléversement de la page {{current}} sur {{total}}...',
36
40
  REDIRECTING: 'Finalisation et redirection...',
@@ -125,6 +129,9 @@ var en = {
125
129
  IMPORT_PROGRESS_SUBTITLE: 'Processing steps',
126
130
  TOTAL_PROGRESS: 'Total progress',
127
131
  CREATING_DOCUMENT: 'Creating document...',
132
+ PREPARING_ARCHIVE: 'Preparing compressed archive...',
133
+ UPLOADING_ARCHIVE_PROGRESS: 'Uploading archive to server ({{progress}}%)...',
134
+ PROCESSING_PAGE_PROGRESS: 'Processing page {{current}} of {{total}}...',
128
135
  UPLOADING_PAGES: 'Uploading pages',
129
136
  UPLOADING_PAGE_PROGRESS: 'Uploading page {{current}} of {{total}}...',
130
137
  REDIRECTING: 'Finalizing and redirecting...',
@@ -219,6 +226,9 @@ var es = {
219
226
  IMPORT_PROGRESS_SUBTITLE: 'Pasos de procesamiento',
220
227
  TOTAL_PROGRESS: 'Progreso total',
221
228
  CREATING_DOCUMENT: 'Creando documento...',
229
+ PREPARING_ARCHIVE: 'Preparando archivo comprimido...',
230
+ UPLOADING_ARCHIVE_PROGRESS: 'Enviando archivo al servidor ({{progress}}%)...',
231
+ PROCESSING_PAGE_PROGRESS: 'Procesando página {{current}} de {{total}}...',
222
232
  UPLOADING_PAGES: 'Carga de páginas',
223
233
  UPLOADING_PAGE_PROGRESS: 'Cargando página {{current}} de {{total}}...',
224
234
  REDIRECTING: 'Finalizando y redirigiendo...',
@@ -415,10 +425,23 @@ class SolidpepperUploadYoubookyComponent {
415
425
  if (this.otpResendInterval) {
416
426
  clearInterval(this.otpResendInterval);
417
427
  }
428
+ this.revokePreviewUrls();
418
429
  if (this.flipbookDataService) {
419
430
  this.flipbookDataService.pages = [];
420
431
  }
421
432
  }
433
+ revokePreviewUrls() {
434
+ if (!this.pdfPreviews || typeof URL === 'undefined')
435
+ return;
436
+ for (const p of this.pdfPreviews) {
437
+ if (p?.url && p.url.startsWith('blob:')) {
438
+ try {
439
+ URL.revokeObjectURL(p.url);
440
+ }
441
+ catch { }
442
+ }
443
+ }
444
+ }
422
445
  setStep(step) {
423
446
  this.currentStep = step;
424
447
  this.stepChange.emit(step);
@@ -463,6 +486,7 @@ class SolidpepperUploadYoubookyComponent {
463
486
  if (this.flipbookDataService) {
464
487
  this.flipbookDataService.pages = [];
465
488
  }
489
+ this.revokePreviewUrls();
466
490
  this.pdfPreviews = [];
467
491
  this.pdfFile = null;
468
492
  this.isPdfProcessingCancelled = true;
@@ -492,6 +516,7 @@ class SolidpepperUploadYoubookyComponent {
492
516
  this.pdfProgress = 0;
493
517
  this.processingCurrentPage = 0;
494
518
  this.processingTotalPages = 0;
519
+ this.revokePreviewUrls();
495
520
  this.pdfPreviews = [];
496
521
  this.isFlipbookReady = false;
497
522
  this.isPdfProcessingCancelled = false;
@@ -575,11 +600,23 @@ class SolidpepperUploadYoubookyComponent {
575
600
  if (finalWidth === 793 && finalHeight === 1123) {
576
601
  finalHeight = 1122;
577
602
  }
578
- previews.push({
579
- url: canvas.toDataURL('image/webp', 0.95),
580
- width: finalWidth,
581
- height: finalHeight,
582
- });
603
+ const blob = await new Promise((resolve) => canvas.toBlob((b) => resolve(b), 'image/webp', 0.92));
604
+ if (blob) {
605
+ const objectUrl = URL.createObjectURL(blob);
606
+ previews.push({
607
+ url: objectUrl,
608
+ width: finalWidth,
609
+ height: finalHeight,
610
+ blob,
611
+ });
612
+ }
613
+ else {
614
+ previews.push({
615
+ url: canvas.toDataURL('image/webp', 0.92),
616
+ width: finalWidth,
617
+ height: finalHeight,
618
+ });
619
+ }
583
620
  if (onProgress) {
584
621
  onProgress(Math.round((pageNum / totalPages) * 100), pageNum, totalPages);
585
622
  }
@@ -1028,6 +1065,178 @@ class SolidpepperUploadYoubookyComponent {
1028
1065
  }
1029
1066
  }
1030
1067
  // =============================== UPLOAD & IMPORT FLOW ===============================
1068
+ async blobToDataUrl(blob) {
1069
+ return new Promise((resolve, reject) => {
1070
+ const reader = new FileReader();
1071
+ reader.onloadend = () => resolve(reader.result || '');
1072
+ reader.onerror = (err) => reject(err);
1073
+ reader.readAsDataURL(blob);
1074
+ });
1075
+ }
1076
+ async dataUrlToBlob(url) {
1077
+ const res = await fetch(url);
1078
+ return await res.blob();
1079
+ }
1080
+ async buildPagesZipBlob() {
1081
+ const zip = new JSZip();
1082
+ const manifest = [];
1083
+ for (let i = 0; i < this.pdfPreviews.length; i++) {
1084
+ const p = this.pdfPreviews[i];
1085
+ const fileName = `${i + 1}.webp`;
1086
+ manifest.push({
1087
+ page: i + 1,
1088
+ fileName,
1089
+ width: p.width,
1090
+ height: p.height,
1091
+ });
1092
+ const blob = p.blob || (await this.dataUrlToBlob(p.url));
1093
+ zip.file(fileName, blob, { binary: true, compression: 'STORE' });
1094
+ }
1095
+ zip.file('manifest.json', JSON.stringify(manifest), {
1096
+ compression: 'DEFLATE',
1097
+ });
1098
+ return await zip.generateAsync({
1099
+ type: 'blob',
1100
+ compression: 'STORE',
1101
+ });
1102
+ }
1103
+ importDocumentZipStream(assetGuid, overwrite, zipBlob, totalFiles) {
1104
+ return new Promise((resolve, reject) => {
1105
+ const formData = new FormData();
1106
+ formData.append('assetGuid', assetGuid);
1107
+ formData.append('overwrite', overwrite ? 'true' : 'false');
1108
+ formData.append('file', zipBlob, 'pages.zip');
1109
+ const endpoint = this.getEffectiveApiUrl('Dam/documents/import-zip-stream');
1110
+ let headers = new HttpHeaders();
1111
+ const token = this.getEffectiveToken();
1112
+ if (token) {
1113
+ headers = headers.set('Authorization', `Bearer ${token}`);
1114
+ }
1115
+ let processedTextLength = 0;
1116
+ let lineBuffer = '';
1117
+ const failedPages = [];
1118
+ const processNdjsonChunk = (newText) => {
1119
+ if (!newText)
1120
+ return;
1121
+ lineBuffer += newText;
1122
+ const lines = lineBuffer.split('\n');
1123
+ lineBuffer = lines.pop() || '';
1124
+ for (const rawLine of lines) {
1125
+ const trimmed = rawLine.trim();
1126
+ if (!trimmed)
1127
+ continue;
1128
+ try {
1129
+ const evt = JSON.parse(trimmed);
1130
+ if (evt.type === 'start') {
1131
+ this.uploadingPageIndex = 0;
1132
+ this.scrollToActivePage(0);
1133
+ this.cdr.detectChanges();
1134
+ }
1135
+ else if (evt.type === 'page') {
1136
+ const idx = typeof evt.index === 'number'
1137
+ ? evt.index
1138
+ : (evt.page || 1) - 1;
1139
+ this.uploadedPagesSet.add(idx);
1140
+ const doneCount = this.uploadedPagesSet.size;
1141
+ this.uploadingPageIndex =
1142
+ idx + 1 < totalFiles ? idx + 1 : null;
1143
+ this.scrollToActivePage(idx);
1144
+ this.pdfProgress = Math.min(99, 25 + Math.round((doneCount / totalFiles) * 75));
1145
+ this.pdfUploadStatusText =
1146
+ this.translate('media.dam-documents.dam-documents-list.PROCESSING_PAGE_PROGRESS', { current: doneCount, total: totalFiles }) || `Traitement de la page ${doneCount} sur ${totalFiles}...`;
1147
+ this.cdr.detectChanges();
1148
+ }
1149
+ else if (evt.type === 'page_error') {
1150
+ const failedPageNum = evt.page || (evt.index ?? 0) + 1;
1151
+ if (!failedPages.includes(failedPageNum)) {
1152
+ failedPages.push(failedPageNum);
1153
+ }
1154
+ }
1155
+ else if (evt.type === 'completed') {
1156
+ if (evt.flipbookUrl) {
1157
+ this.serverFlipbookUrl = evt.flipbookUrl;
1158
+ }
1159
+ if (evt.reviewerUrl) {
1160
+ this.serverReviewerUrl = evt.reviewerUrl;
1161
+ }
1162
+ if (Array.isArray(evt.failedPages)) {
1163
+ for (const fp of evt.failedPages) {
1164
+ if (!failedPages.includes(fp)) {
1165
+ failedPages.push(fp);
1166
+ }
1167
+ }
1168
+ }
1169
+ this.pdfProgress = 100;
1170
+ this.uploadingPageIndex = null;
1171
+ this.cdr.detectChanges();
1172
+ }
1173
+ else if (evt.type === 'error') {
1174
+ throw new Error(evt.error || 'Stream import error');
1175
+ }
1176
+ }
1177
+ catch (parseErr) {
1178
+ // Ignore incomplete/non-JSON lines unless it's an explicit stream error
1179
+ if (parseErr instanceof Error &&
1180
+ parseErr.message === 'Stream import error') {
1181
+ throw parseErr;
1182
+ }
1183
+ }
1184
+ }
1185
+ };
1186
+ this.http
1187
+ .post(endpoint, formData, {
1188
+ headers,
1189
+ observe: 'events',
1190
+ responseType: 'text',
1191
+ reportProgress: true,
1192
+ })
1193
+ .subscribe({
1194
+ next: (event) => {
1195
+ if (event.type === HttpEventType.UploadProgress) {
1196
+ const pct = event.total
1197
+ ? Math.round((event.loaded / event.total) * 100)
1198
+ : 0;
1199
+ if (this.uploadedPagesSet.size === 0) {
1200
+ this.pdfProgress = Math.min(25, Math.round(pct * 0.25));
1201
+ if (pct < 100) {
1202
+ this.pdfUploadStatusText =
1203
+ this.translate('media.dam-documents.dam-documents-list.UPLOADING_ARCHIVE_PROGRESS', { progress: pct }) || `Envoi de l'archive au serveur (${pct}%)...`;
1204
+ }
1205
+ else {
1206
+ this.uploadingPageIndex = 0;
1207
+ this.pdfUploadStatusText =
1208
+ this.translate('media.dam-documents.dam-documents-list.PROCESSING_PAGE_PROGRESS', { current: 1, total: totalFiles }) || `Traitement de la page 1 sur ${totalFiles}...`;
1209
+ }
1210
+ this.cdr.detectChanges();
1211
+ }
1212
+ }
1213
+ else if (event.type === HttpEventType.DownloadProgress) {
1214
+ const fullText = event.partialText || '';
1215
+ if (fullText.length > processedTextLength) {
1216
+ const delta = fullText.substring(processedTextLength);
1217
+ processedTextLength = fullText.length;
1218
+ processNdjsonChunk(delta);
1219
+ }
1220
+ }
1221
+ else if (event.type === HttpEventType.Response) {
1222
+ const fullText = event.body || '';
1223
+ if (fullText.length > processedTextLength) {
1224
+ const delta = fullText.substring(processedTextLength);
1225
+ processedTextLength = fullText.length;
1226
+ processNdjsonChunk(delta + '\n');
1227
+ }
1228
+ else if (lineBuffer.trim().length > 0) {
1229
+ processNdjsonChunk('\n');
1230
+ }
1231
+ resolve({ failedPages });
1232
+ }
1233
+ },
1234
+ error: (err) => {
1235
+ reject(err);
1236
+ },
1237
+ });
1238
+ });
1239
+ }
1031
1240
  importDocumentFiles(assetGuid, overwrite, files) {
1032
1241
  const formData = new FormData();
1033
1242
  formData.append('assetGuid', assetGuid);
@@ -1066,6 +1275,67 @@ class SolidpepperUploadYoubookyComponent {
1066
1275
  }
1067
1276
  return key;
1068
1277
  }
1278
+ async fallbackBatchSvgUpload(assetGuid, totalFiles) {
1279
+ const allFileItems = [];
1280
+ for (let i = 0; i < totalFiles; i++) {
1281
+ const preview = this.pdfPreviews[i];
1282
+ const dataUrl = preview.url.startsWith('data:') || !preview.blob
1283
+ ? preview.url
1284
+ : await this.blobToDataUrl(preview.blob);
1285
+ const svgContent = this.generateSvgContent({
1286
+ url: dataUrl,
1287
+ width: preview.width,
1288
+ height: preview.height,
1289
+ });
1290
+ const blob = new Blob([svgContent], { type: 'image/svg+xml' });
1291
+ const fileName = `${i + 1}.svg`;
1292
+ const file = new File([blob], fileName, { type: 'image/svg+xml' });
1293
+ allFileItems.push({ index: i, file });
1294
+ }
1295
+ const BATCH_SIZE = 5;
1296
+ const failedPages = [];
1297
+ for (let i = 0; i < totalFiles; i += BATCH_SIZE) {
1298
+ const batchItems = allFileItems
1299
+ .slice(i, i + BATCH_SIZE)
1300
+ .filter((item) => !this.uploadedPagesSet.has(item.index));
1301
+ if (batchItems.length === 0)
1302
+ continue;
1303
+ const batchFiles = batchItems.map((item) => item.file);
1304
+ const startIndex = batchItems[0].index;
1305
+ const endIndex = Math.min(i + BATCH_SIZE, totalFiles);
1306
+ this.uploadingPageIndex = startIndex;
1307
+ this.scrollToActivePage(startIndex);
1308
+ this.pdfUploadStatusText =
1309
+ this.translate('media.dam-documents.dam-documents-list.UPLOADING_PAGE_PROGRESS', { current: endIndex, total: totalFiles }) ||
1310
+ `Téléversement des pages ${startIndex + 1} à ${endIndex} sur ${totalFiles}...`;
1311
+ this.cdr.detectChanges();
1312
+ try {
1313
+ await this.uploadWithRetry(() => this.importDocumentFiles(assetGuid, true, batchFiles), 2, 500);
1314
+ for (const item of batchItems) {
1315
+ this.uploadedPagesSet.add(item.index);
1316
+ }
1317
+ }
1318
+ catch (batchErr) {
1319
+ console.warn(`Batch ${startIndex + 1}-${endIndex} failed, falling back to page-by-page upload:`, batchErr);
1320
+ for (const item of batchItems) {
1321
+ this.uploadingPageIndex = item.index;
1322
+ this.scrollToActivePage(item.index);
1323
+ this.cdr.detectChanges();
1324
+ try {
1325
+ await this.uploadWithRetry(() => this.importDocumentFiles(assetGuid, true, [item.file]), 2, 500);
1326
+ this.uploadedPagesSet.add(item.index);
1327
+ }
1328
+ catch (pageErr) {
1329
+ console.error(`Page ${item.index + 1} failed:`, pageErr);
1330
+ failedPages.push(item.index + 1);
1331
+ }
1332
+ }
1333
+ }
1334
+ this.pdfProgress = Math.round((endIndex / totalFiles) * 100);
1335
+ this.cdr.detectChanges();
1336
+ }
1337
+ return failedPages;
1338
+ }
1069
1339
  async importDocumentFromPdf() {
1070
1340
  const docName = this.projectName.trim();
1071
1341
  if (!this.pdfFile ||
@@ -1094,6 +1364,7 @@ class SolidpepperUploadYoubookyComponent {
1094
1364
  documentName: docName,
1095
1365
  folderGuid: folderGuid,
1096
1366
  documentType: 'document',
1367
+ skipDefaultPage: true,
1097
1368
  }, { headers }));
1098
1369
  const assetGuid = (typeof createRes?.assetGuid === 'string' && createRes.assetGuid) ||
1099
1370
  (typeof createRes?.AssetGuid === 'string' && createRes.AssetGuid) ||
@@ -1113,60 +1384,32 @@ class SolidpepperUploadYoubookyComponent {
1113
1384
  this.serverReviewerUrl = createRes.reviewerUrl || createRes.ReviewerUrl;
1114
1385
  }
1115
1386
  const totalFiles = this.pdfPreviews.length;
1116
- // Prepare all File objects
1117
- const allFileItems = [];
1118
- for (let i = 0; i < totalFiles; i++) {
1119
- const preview = this.pdfPreviews[i];
1120
- const svgContent = this.generateSvgContent(preview);
1121
- const blob = new Blob([svgContent], { type: 'image/svg+xml' });
1122
- const fileName = `${i + 1}.svg`;
1123
- const file = new File([blob], fileName, { type: 'image/svg+xml' });
1124
- allFileItems.push({ index: i, file });
1125
- }
1126
- const BATCH_SIZE = 5;
1127
- const failedPages = [];
1128
- // Fast sequential batch upload (1 request per 5 pages sequentially) to preserve exact order, optimize speed and avoid server concurrency 500s
1129
- for (let i = 0; i < totalFiles; i += BATCH_SIZE) {
1130
- const batchItems = allFileItems.slice(i, i + BATCH_SIZE);
1131
- const batchFiles = batchItems.map((item) => item.file);
1132
- const startIndex = i;
1133
- const endIndex = Math.min(i + BATCH_SIZE, totalFiles);
1134
- this.uploadingPageIndex = startIndex;
1135
- this.scrollToActivePage(startIndex);
1387
+ let failedPages = [];
1388
+ try {
1136
1389
  this.pdfUploadStatusText =
1137
- this.translate('media.dam-documents.dam-documents-list.UPLOADING_PAGE_PROGRESS', { current: endIndex, total: totalFiles }) ||
1138
- `Téléversement des pages ${startIndex + 1} à ${endIndex} sur ${totalFiles}...`;
1390
+ this.translate('media.dam-documents.dam-documents-list.PREPARING_ARCHIVE') || "Préparation de l'archive compressée...";
1139
1391
  this.cdr.detectChanges();
1140
- try {
1141
- await this.uploadWithRetry(() => this.importDocumentFiles(assetGuid, true, batchFiles), 2, 500);
1142
- for (const item of batchItems) {
1143
- this.uploadedPagesSet.add(item.index);
1144
- }
1145
- }
1146
- catch (batchErr) {
1147
- console.warn(`Batch ${startIndex + 1}-${endIndex} failed, falling back to page-by-page upload:`, batchErr);
1148
- // Fallback: isolate per-page upload for this batch
1149
- for (const item of batchItems) {
1150
- this.uploadingPageIndex = item.index;
1151
- this.scrollToActivePage(item.index);
1152
- this.cdr.detectChanges();
1153
- try {
1154
- await this.uploadWithRetry(() => this.importDocumentFiles(assetGuid, true, [item.file]), 2, 500);
1155
- this.uploadedPagesSet.add(item.index);
1156
- }
1157
- catch (pageErr) {
1158
- console.error(`Page ${item.index + 1} failed:`, pageErr);
1159
- failedPages.push(item.index + 1);
1392
+ const zipBlob = await this.buildPagesZipBlob();
1393
+ const streamResult = await this.importDocumentZipStream(assetGuid, true, zipBlob, totalFiles);
1394
+ failedPages = streamResult.failedPages;
1395
+ // Ensure all non-failed pages are marked as uploaded if completed event arrived at once
1396
+ if (this.uploadedPagesSet.size < totalFiles - failedPages.length) {
1397
+ for (let i = 0; i < totalFiles; i++) {
1398
+ if (!failedPages.includes(i + 1)) {
1399
+ this.uploadedPagesSet.add(i);
1160
1400
  }
1161
1401
  }
1162
1402
  }
1163
- this.pdfProgress = Math.round((endIndex / totalFiles) * 100);
1164
- this.cdr.detectChanges();
1403
+ }
1404
+ catch (zipErr) {
1405
+ console.warn('[Youbooky Import] Binary ZIP stream failed or unsupported, falling back to batch SVG upload:', zipErr);
1406
+ failedPages = await this.fallbackBatchSvgUpload(assetGuid, totalFiles);
1165
1407
  }
1166
1408
  if (failedPages.length > 0 && failedPages.length === totalFiles) {
1167
1409
  throw new Error('All pages failed to upload');
1168
1410
  }
1169
1411
  this.uploadingPageIndex = null;
1412
+ this.pdfProgress = 100;
1170
1413
  if (failedPages.length > 0) {
1171
1414
  this.pdfUploadStatusText =
1172
1415
  this.translate('media.dam-documents.dam-documents-list.IMPORT_PARTIAL_SUCCESS', { failed: failedPages.join(', ') }) ||