-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
1169 lines (1043 loc) · 36.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hasProperty from '@uppy/utils/lib/hasProperty'
import ErrorWithCause from '@uppy/utils/lib/ErrorWithCause'
import { RateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue'
import BasePlugin from '@uppy/core/lib/BasePlugin.js'
import type { DefinePluginOpts, PluginOpts } from '@uppy/core/lib/BasePlugin.js'
import Tus, { type TusDetailedError } from '@uppy/tus'
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { Uppy } from '@uppy/core'
import Assembly from './Assembly.ts'
import Client, { AssemblyError } from './Client.ts'
import AssemblyOptionsBuilder, {
type OptionsWithRestructuredFields,
} from './AssemblyOptions.ts'
import AssemblyWatcher from './AssemblyWatcher.ts'
import locale from './locale.ts'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import packageJson from '../package.json'
export interface AssemblyFile {
id: string
name: string
basename: string
ext: string
size: number
mime: string
type: string
field: string
md5hash: string
is_tus_file: boolean
original_md5hash: string
original_id: string
original_name: string
original_basename: string
original_path: string
url: string
ssl_url: string
tus_upload_url: string
meta: Record<string, any>
}
export interface AssemblyResult extends AssemblyFile {
cost: number
execTime: number
queue: string
queueTime: number
localId: string | null
}
export interface AssemblyResponse {
ok: string
message?: string
assembly_id: string
parent_id?: string
account_id: string
template_id?: string
instance: string
assembly_url: string
assembly_ssl_url: string
uppyserver_url: string
companion_url: string
websocket_url: string
tus_url: string
bytes_received: number
bytes_expected: number
upload_duration: number
client_agent?: string
client_ip?: string
client_referer?: string
transloadit_client: string
start_date: string
upload_meta_data_extracted: boolean
warnings: any[]
is_infinite: boolean
has_dupe_jobs: boolean
execution_start: string
execution_duration: number
queue_duration: number
jobs_queue_duration: number
notify_start?: any
notify_url?: string
notify_status?: any
notify_response_code?: any
notify_duration?: any
last_job_completed?: string
fields: Record<string, any>
running_jobs: any[]
bytes_usage: number
executing_jobs: any[]
started_jobs: string[]
parent_assembly_status: any
params: string
template?: any
merged_params: string
uploads: AssemblyFile[]
results: Record<string, AssemblyResult[]>
build_id: string
error?: string
stderr?: string
stdout?: string
reason?: string
}
const sendErrorToConsole = (originalErr: Error) => (err: Error) => {
const error = new ErrorWithCause('Failed to send error to the client', {
cause: err,
})
// eslint-disable-next-line no-console
console.error(error, originalErr)
}
const COMPANION_URL = 'https://api2.transloadit.com/companion'
// Regex matching acceptable postMessage() origins for authentication feedback from companion.
const COMPANION_ALLOWED_HOSTS = /\.transloadit\.com$/
// Regex used to check if a Companion address is run by Transloadit.
const TL_COMPANION = /https?:\/\/api2(?:-\w+)?\.transloadit\.com\/companion/
export interface AssemblyParameters {
auth: {
key: string
expires?: string
}
template_id?: string
steps?: { [step: string]: Record<string, unknown> }
fields?: { [name: string]: number | string }
notify_url?: string
}
export interface AssemblyOptions {
params?: AssemblyParameters | null
fields?: Record<string, string | number> | string[] | null
signature?: string | null
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface TransloaditOptions<M extends Meta, B extends Body>
extends PluginOpts {
service?: string
errorReporting?: boolean
waitForEncoding?: boolean
waitForMetadata?: boolean
importFromUploadURLs?: boolean
alwaysRunAssembly?: boolean
limit?: number
clientName?: string | null
retryDelays?: number[]
assemblyOptions?:
| AssemblyOptions
| ((
file?: UppyFile<M, B> | null,
options?: TransloaditOptions<M, B>,
) => Promise<AssemblyOptions> | AssemblyOptions)
}
const defaultOptions = {
service: 'https://api2.transloadit.com',
errorReporting: true,
waitForEncoding: false,
waitForMetadata: false,
alwaysRunAssembly: false,
importFromUploadURLs: false,
limit: 20,
retryDelays: [7_000, 10_000, 15_000, 20_000],
clientName: null,
} satisfies TransloaditOptions<any, any>
export type Opts<M extends Meta, B extends Body> = DefinePluginOpts<
TransloaditOptions<M, B>,
keyof typeof defaultOptions
>
type TransloaditState = {
assemblies: Record<string, AssemblyResponse>
files: Record<
string,
{ assembly: string; id: string; uploadedFile: AssemblyFile }
>
results: Array<{
result: AssemblyResult
stepName: string
id: string
assembly: string
}>
uploadsAssemblies: Record<string, string[]>
}
declare module '@uppy/core' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface UppyEventMap<M extends Meta, B extends Body> {
// We're also overriding the `restored` event as it is now populated with Transloadit state.
restored: (pluginData: Record<string, TransloaditState>) => void
'restore:get-data': (
setData: (
arg: Record<
string,
Pick<TransloaditState, 'assemblies' | 'uploadsAssemblies'>
>,
) => void,
) => void
'transloadit:assembly-created': (
assembly: AssemblyResponse,
fileIDs: string[],
) => void
'transloadit:assembly-cancel': (assembly: AssemblyResponse) => void
'transloadit:import-error': (
assembly: AssemblyResponse,
fileID: string,
error: Error,
) => void
'transloadit:assembly-error': (
assembly: AssemblyResponse,
error: Error,
) => void
'transloadit:assembly-executing': (assembly: AssemblyResponse) => void
'transloadit:assembly-cancelled': (assembly: AssemblyResponse) => void
'transloadit:upload': (
file: AssemblyFile,
assembly: AssemblyResponse,
) => void
'transloadit:result': (
stepName: string,
result: AssemblyResult,
assembly: AssemblyResponse,
) => void
'transloadit:complete': (assembly: AssemblyResponse) => void
'transloadit:execution-progress': (details: {
progress_combined?: number
}) => void
}
}
declare module '@uppy/utils/lib/UppyFile' {
// eslint-disable-next-line no-shadow, @typescript-eslint/no-unused-vars
export interface UppyFile<M extends Meta, B extends Body> {
transloadit?: { assembly: string }
tus?: { uploadUrl?: string | null }
}
}
/**
* Upload files to Transloadit using Tus.
*/
export default class Transloadit<
M extends Meta,
B extends Body,
> extends BasePlugin<Opts<M, B>, M, B, TransloaditState> {
static VERSION = packageJson.version
#rateLimitedQueue: RateLimitedQueue
client: Client<M, B>
activeAssemblies: Record<string, Assembly>
assemblyWatchers: Record<string, AssemblyWatcher<M, B>>
completedFiles: Record<string, boolean>
restored: Promise<void> | null
constructor(uppy: Uppy<M, B>, opts: TransloaditOptions<M, B>) {
super(uppy, { ...defaultOptions, ...opts })
this.type = 'uploader'
this.id = this.opts.id || 'Transloadit'
this.defaultLocale = locale
this.#rateLimitedQueue = new RateLimitedQueue(this.opts.limit)
this.i18nInit()
this.client = new Client({
service: this.opts.service,
client: this.#getClientVersion(),
errorReporting: this.opts.errorReporting,
rateLimitedQueue: this.#rateLimitedQueue,
})
// Contains Assembly instances for in-progress Assemblies.
this.activeAssemblies = {}
// Contains a mapping of uploadID to AssemblyWatcher
this.assemblyWatchers = {}
// Contains a file IDs that have completed postprocessing before the upload
// they belong to has entered the postprocess stage.
this.completedFiles = Object.create(null)
}
#getClientVersion() {
const list = [
// @ts-expect-error VERSION comes from babel, TS does not understand
`uppy-core:${this.uppy.constructor.VERSION}`,
// @ts-expect-error VERSION comes from babel, TS does not understand
`uppy-transloadit:${this.constructor.VERSION}`,
`uppy-tus:${Tus.VERSION}`,
]
const addPluginVersion = (pluginName: string, versionName: string) => {
const plugin = this.uppy.getPlugin(pluginName)
if (plugin) {
// @ts-expect-error VERSION comes from babel, TS does not understand
list.push(`${versionName}:${plugin.constructor.VERSION}`)
}
}
if (this.opts.importFromUploadURLs) {
addPluginVersion('XHRUpload', 'uppy-xhr-upload')
addPluginVersion('AwsS3', 'uppy-aws-s3')
addPluginVersion('AwsS3Multipart', 'uppy-aws-s3-multipart')
}
addPluginVersion('Dropbox', 'uppy-dropbox')
addPluginVersion('Box', 'uppy-box')
addPluginVersion('Facebook', 'uppy-facebook')
addPluginVersion('GoogleDrive', 'uppy-google-drive')
addPluginVersion('Instagram', 'uppy-instagram')
addPluginVersion('OneDrive', 'uppy-onedrive')
addPluginVersion('Zoom', 'uppy-zoom')
addPluginVersion('Url', 'uppy-url')
if (this.opts.clientName != null) {
list.push(this.opts.clientName)
}
return list.join(',')
}
/**
* Attach metadata to files to configure the Tus plugin to upload to Transloadit.
* Also use Transloadit's Companion
*
* See: https://github.com/tus/tusd/wiki/Uploading-to-Transloadit-using-tus#uploading-using-tus
*/
#attachAssemblyMetadata(file: UppyFile<M, B>, status: AssemblyResponse) {
// Add the metadata parameters Transloadit needs.
const meta = {
...file.meta,
assembly_url: status.assembly_url,
filename: file.name,
fieldname: 'file',
}
// Add Assembly-specific Tus endpoint.
const tus = {
...file.tus,
endpoint: status.tus_url,
// Include X-Request-ID headers for better debugging.
addRequestId: true,
}
// Set Companion location. We only add this, if 'file' has the attribute
// remote, because this is the criteria to identify remote files.
// We only replace the hostname for Transloadit's companions, so that
// people can also self-host them while still using Transloadit for encoding.
let { remote } = file
if (file.remote && TL_COMPANION.test(file.remote.companionUrl)) {
const newHost = status.companion_url.replace(/\/$/, '')
const path = file.remote.url
.replace(file.remote.companionUrl, '')
.replace(/^\//, '')
remote = {
...file.remote,
companionUrl: newHost,
url: `${newHost}/${path}`,
}
}
// Store the Assembly ID this file is in on the file under the `transloadit` key.
const newFile = {
...file,
transloadit: {
assembly: status.assembly_id,
},
}
// Only configure the Tus plugin if we are uploading straight to Transloadit (the default).
if (!this.opts.importFromUploadURLs) {
Object.assign(newFile, { meta, tus, remote })
}
return newFile
}
#createAssembly(
fileIDs: string[],
uploadID: string,
assemblyOptions: OptionsWithRestructuredFields,
) {
this.uppy.log('[Transloadit] Create Assembly')
return this.client
.createAssembly({
...assemblyOptions,
expectedFiles: fileIDs.length,
})
.then(async (newAssembly) => {
const files = this.uppy
.getFiles()
.filter(({ id }) => fileIDs.includes(id))
if (files.length !== fileIDs.length) {
if (files.length === 0) {
// All files have been removed, cancelling.
await this.client.cancelAssembly(newAssembly)
return null
}
// At least one file has been removed.
await this.client.updateNumberOfFilesInAssembly(
newAssembly,
files.length,
)
}
const assembly = new Assembly(newAssembly, this.#rateLimitedQueue)
const { status } = assembly
const assemblyID = status.assembly_id
const { assemblies, uploadsAssemblies } = this.getPluginState()
this.setPluginState({
// Store the Assembly status.
assemblies: {
...assemblies,
[assemblyID]: status,
},
// Store the list of Assemblies related to this upload.
uploadsAssemblies: {
...uploadsAssemblies,
[uploadID]: [...uploadsAssemblies[uploadID], assemblyID],
},
})
const updatedFiles: Record<string, UppyFile<M, B>> = {}
files.forEach((file) => {
updatedFiles[file.id] = this.#attachAssemblyMetadata(file, status)
})
this.uppy.setState({
files: {
...this.uppy.getState().files,
...updatedFiles,
},
})
// TODO: this should not live inside a `file-removed` event but somewhere more deterministic.
// Such as inside the function where the assembly has succeeded or cancelled.
// For the use case of cancelling the assembly when needed, we should try to do that with just `cancel-all`.
const fileRemovedHandler = (
fileRemoved: UppyFile<M, B>,
reason?: string,
) => {
// If the assembly has successfully completed, we do not need these checks.
// Otherwise we may cancel an assembly after it already succeeded
if (assembly.status?.ok === 'ASSEMBLY_COMPLETED') {
this.uppy.off('file-removed', fileRemovedHandler)
return
}
if (reason === 'cancel-all') {
assembly.close()
this.uppy.off('file-removed', fileRemovedHandler)
} else if (fileRemoved.id in updatedFiles) {
delete updatedFiles[fileRemoved.id]
const nbOfRemainingFiles = Object.keys(updatedFiles).length
if (nbOfRemainingFiles === 0) {
assembly.close()
this.#cancelAssembly(newAssembly).catch(() => {
/* ignore potential errors */
})
this.uppy.off('file-removed', fileRemovedHandler)
} else {
this.client
.updateNumberOfFilesInAssembly(newAssembly, nbOfRemainingFiles)
.catch(() => {
/* ignore potential errors */
})
}
}
}
this.uppy.on('file-removed', fileRemovedHandler)
this.uppy.emit('transloadit:assembly-created', status, fileIDs)
this.uppy.log(`[Transloadit] Created Assembly ${assemblyID}`)
return assembly
})
.catch((err) => {
// TODO: use AssemblyError?
const wrapped = new ErrorWithCause(
`${this.i18n('creatingAssemblyFailed')}: ${err.message}`,
{ cause: err },
)
if ('details' in err) {
// @ts-expect-error details is not in the Error type
wrapped.details = err.details
}
if ('assembly' in err) {
// @ts-expect-error assembly is not in the Error type
wrapped.assembly = err.assembly
}
throw wrapped
})
}
#createAssemblyWatcher(idOrArrayOfIds: string | string[], uploadID: string) {
// AssemblyWatcher tracks completion states of all Assemblies in this upload.
const ids =
Array.isArray(idOrArrayOfIds) ? idOrArrayOfIds : [idOrArrayOfIds]
const watcher = new AssemblyWatcher(this.uppy, ids)
watcher.on('assembly-complete', (id: string) => {
const files = this.getAssemblyFiles(id)
files.forEach((file) => {
this.completedFiles[file.id] = true
this.uppy.emit('postprocess-complete', file)
})
})
watcher.on('assembly-error', (id: string, error: Error) => {
// Clear postprocessing state for all our files.
const filesFromAssembly = this.getAssemblyFiles(id)
filesFromAssembly.forEach((file) => {
// TODO Maybe make a postprocess-error event here?
this.uppy.emit('upload-error', file, error)
this.uppy.emit('postprocess-complete', file)
})
// Reset `tus` key in the file state, so when the upload is retried,
// old tus upload is not re-used — Assebmly expects a new upload, can't currently
// re-use the old one. See: https://github.com/transloadit/uppy/issues/4412
// and `onReceiveUploadUrl` in @uppy/tus
const files = { ...this.uppy.getState().files }
filesFromAssembly.forEach((file) => delete files[file.id].tus)
this.uppy.setState({ files })
this.uppy.emit('error', error)
})
this.assemblyWatchers[uploadID] = watcher
}
#shouldWaitAfterUpload() {
return this.opts.waitForEncoding || this.opts.waitForMetadata
}
/**
* Used when `importFromUploadURLs` is enabled: reserves all files in
* the Assembly.
*/
#reserveFiles(assembly: Assembly, fileIDs: string[]) {
return Promise.all(
fileIDs.map((fileID) => {
const file = this.uppy.getFile(fileID)
return this.client.reserveFile(assembly.status, file)
}),
)
}
/**
* Used when `importFromUploadURLs` is enabled: adds files to the Assembly
* once they have been fully uploaded.
*/
#onFileUploadURLAvailable = (rawFile: UppyFile<M, B> | undefined) => {
const file = this.uppy.getFile(rawFile!.id)
if (!file?.transloadit?.assembly) {
return
}
const { assemblies } = this.getPluginState()
const assembly = assemblies[file.transloadit.assembly]
this.client.addFile(assembly, file).catch((err) => {
this.uppy.log(err)
this.uppy.emit('transloadit:import-error', assembly, file.id, err)
})
}
#findFile(uploadedFile: AssemblyFile) {
const files = this.uppy.getFiles()
for (let i = 0; i < files.length; i++) {
const file = files[i]
// Completed file upload.
if (file.uploadURL === uploadedFile.tus_upload_url) {
return file
}
// In-progress file upload.
if (file.tus && file.tus.uploadUrl === uploadedFile.tus_upload_url) {
return file
}
if (!uploadedFile.is_tus_file) {
// Fingers-crossed check for non-tus uploads, eg imported from S3.
if (
file.name === uploadedFile.name &&
file.size === uploadedFile.size
) {
return file
}
}
}
return undefined
}
#onFileUploadComplete(assemblyId: string, uploadedFile: AssemblyFile) {
const state = this.getPluginState()
const file = this.#findFile(uploadedFile)
if (!file) {
this.uppy.log(
'[Transloadit] Couldn’t find the file, it was likely removed in the process',
)
return
}
this.setPluginState({
files: {
...state.files,
[uploadedFile.id]: {
assembly: assemblyId,
id: file.id,
uploadedFile,
},
},
})
this.uppy.emit(
'transloadit:upload',
uploadedFile,
this.getAssembly(assemblyId),
)
}
#onResult(assemblyId: string, stepName: string, result: AssemblyResult) {
const state = this.getPluginState()
const file = state.files[result.original_id]
// The `file` may not exist if an import robot was used instead of a file upload.
result.localId = file ? file.id : null // eslint-disable-line no-param-reassign
const entry = {
result,
stepName,
id: result.id,
assembly: assemblyId,
}
this.setPluginState({
results: [...state.results, entry],
})
this.uppy.emit(
'transloadit:result',
stepName,
result,
this.getAssembly(assemblyId),
)
}
/**
* When an Assembly has finished processing, get the final state
* and emit it.
*/
#onAssemblyFinished(status: AssemblyResponse) {
const url = status.assembly_ssl_url
this.client.getAssemblyStatus(url).then((finalStatus) => {
const assemblyId = finalStatus.assembly_id
const state = this.getPluginState()
this.setPluginState({
assemblies: {
...state.assemblies,
[assemblyId]: finalStatus,
},
})
this.uppy.emit('transloadit:complete', finalStatus)
})
}
async #cancelAssembly(assembly: AssemblyResponse) {
await this.client.cancelAssembly(assembly)
// TODO bubble this through AssemblyWatcher so its event handlers can clean up correctly
this.uppy.emit('transloadit:assembly-cancelled', assembly)
}
/**
* When all files are removed, cancel in-progress Assemblies.
*/
#onCancelAll = async ({ reason }: { reason?: string } = {}) => {
try {
if (reason !== 'user') return
const { uploadsAssemblies } = this.getPluginState()
const assemblyIDs = Object.values(uploadsAssemblies).flat(1)
const assemblies = assemblyIDs.map((assemblyID) =>
this.getAssembly(assemblyID),
)
await Promise.all(
assemblies.map((assembly) => this.#cancelAssembly(assembly)),
)
} catch (err) {
this.uppy.log(err)
}
}
/**
* Custom state serialization for the Golden Retriever plugin.
* It will pass this back to the `_onRestored` function.
*/
#getPersistentData = (
setData: (
arg: Record<
string,
Pick<TransloaditState, 'assemblies' | 'uploadsAssemblies'>
>,
) => void,
) => {
const { assemblies, uploadsAssemblies } = this.getPluginState()
setData({
[this.id]: {
assemblies,
uploadsAssemblies,
},
})
}
#onRestored = (pluginData: Record<string, TransloaditState>) => {
const savedState =
pluginData && pluginData[this.id] ? pluginData[this.id] : {}
const previousAssemblies = (savedState as TransloaditState).assemblies || {}
const uploadsAssemblies =
(savedState as TransloaditState).uploadsAssemblies || {}
if (Object.keys(uploadsAssemblies).length === 0) {
// Nothing to restore.
return
}
// Convert loaded Assembly statuses to a Transloadit plugin state object.
const restoreState = (assemblies: TransloaditState['assemblies']) => {
const files: Record<
string,
{ id: string; assembly: string; uploadedFile: AssemblyFile }
> = {}
const results: {
result: AssemblyResult
stepName: string
id: string
assembly: string
}[] = []
for (const [id, status] of Object.entries(assemblies)) {
status.uploads.forEach((uploadedFile) => {
const file = this.#findFile(uploadedFile)
files[uploadedFile.id] = {
id: file!.id,
assembly: id,
uploadedFile,
}
})
const state = this.getPluginState()
Object.keys(status.results).forEach((stepName) => {
for (const result of status.results[stepName]) {
const file = state.files[result.original_id]
result.localId = file ? file.id : null
results.push({
id: result.id,
result,
stepName,
assembly: id,
})
}
})
}
this.setPluginState({
assemblies,
files,
results,
uploadsAssemblies,
})
}
// Set up the Assembly instances and AssemblyWatchers for existing Assemblies.
const restoreAssemblies = () => {
// eslint-disable-next-line no-shadow
const { assemblies, uploadsAssemblies } = this.getPluginState()
// Set up the assembly watchers again for all the ongoing uploads.
Object.keys(uploadsAssemblies).forEach((uploadID) => {
const assemblyIDs = uploadsAssemblies[uploadID]
this.#createAssemblyWatcher(assemblyIDs, uploadID)
})
const allAssemblyIDs = Object.keys(assemblies)
allAssemblyIDs.forEach((id) => {
const assembly = new Assembly(assemblies[id], this.#rateLimitedQueue)
this.#connectAssembly(assembly)
})
}
// Force-update all Assemblies to check for missed events.
const updateAssemblies = () => {
const { assemblies } = this.getPluginState()
return Promise.all(
Object.keys(assemblies).map((id) => {
return this.activeAssemblies[id].update()
}),
)
}
// Restore all Assembly state.
this.restored = Promise.resolve().then(() => {
restoreState(previousAssemblies)
restoreAssemblies()
updateAssemblies()
})
this.restored.then(() => {
this.restored = null
})
}
#connectAssembly(assembly: Assembly) {
const { status } = assembly
const id = status.assembly_id
this.activeAssemblies[id] = assembly
// Sync local `assemblies` state
assembly.on('status', (newStatus: AssemblyResponse) => {
const { assemblies } = this.getPluginState()
this.setPluginState({
assemblies: {
...assemblies,
[id]: newStatus,
},
})
})
assembly.on('upload', (file: AssemblyFile) => {
this.#onFileUploadComplete(id, file)
})
assembly.on('error', (error: AssemblyError) => {
error.assembly = assembly.status // eslint-disable-line no-param-reassign
this.uppy.emit('transloadit:assembly-error', assembly.status, error)
})
assembly.on('executing', () => {
this.uppy.emit('transloadit:assembly-executing', assembly.status)
})
assembly.on(
'execution-progress',
(details: { progress_combined?: number }) => {
this.uppy.emit('transloadit:execution-progress', details)
if (details.progress_combined != null) {
// TODO: Transloadit emits progress information for the entire Assembly combined
// (progress_combined) and for each imported/uploaded file (progress_per_original_file).
// Uppy's current design requires progress to be set for each file, which is then
// averaged to get the total progress (see calculateProcessingProgress.js).
// Therefore, we currently set the combined progres for every file, so that this is
// the same value that is displayed to the end user, although we have more accurate
// per-file progress as well. We cannot use this here or otherwise progress from
// imported files would not be counted towards the total progress because imported
// files are not registered with Uppy.
for (const file of this.uppy.getFiles()) {
this.uppy.emit('postprocess-progress', file, {
mode: 'determinate',
value: details.progress_combined / 100,
message: this.i18n('encoding'),
})
}
}
},
)
if (this.opts.waitForEncoding) {
assembly.on('result', (stepName: string, result: AssemblyResult) => {
this.#onResult(id, stepName, result)
})
}
if (this.opts.waitForEncoding) {
assembly.on('finished', () => {
this.#onAssemblyFinished(assembly.status)
})
} else if (this.opts.waitForMetadata) {
assembly.on('metadata', () => {
this.#onAssemblyFinished(assembly.status)
})
}
// No need to connect to the socket if the Assembly has completed by now.
// @ts-expect-error ok does not exist on Assembly?
if (assembly.ok === 'ASSEMBLY_COMPLETE') {
return assembly
}
assembly.connect()
return assembly
}
#prepareUpload = async (fileIDs: string[], uploadID: string) => {
const files = fileIDs.map((id) => this.uppy.getFile(id))
const filesWithoutErrors = files.filter((file) => {
if (!file.error) {
this.uppy.emit('preprocess-progress', file, {
mode: 'indeterminate',
message: this.i18n('creatingAssembly'),
})
return true
}
return false
})
const createAssembly = async ({
// eslint-disable-next-line no-shadow
fileIDs,
options,
}: {
fileIDs: string[]
options: OptionsWithRestructuredFields
}) => {
try {
const assembly = (await this.#createAssembly(
fileIDs,
uploadID,
options,
)) as Assembly
if (this.opts.importFromUploadURLs) {
await this.#reserveFiles(assembly, fileIDs)
}
fileIDs.forEach((fileID) => {
const file = this.uppy.getFile(fileID)
this.uppy.emit('preprocess-complete', file)
})
return assembly
} catch (err) {
fileIDs.forEach((fileID) => {
const file = this.uppy.getFile(fileID)
// Clear preprocessing state when the Assembly could not be created,
// otherwise the UI gets confused about the lingering progress keys
this.uppy.emit('preprocess-complete', file)
this.uppy.emit('upload-error', file, err)
})
throw err
}
}
const { uploadsAssemblies } = this.getPluginState()
this.setPluginState({
uploadsAssemblies: {
...uploadsAssemblies,
[uploadID]: [],
},
})
const builder = new AssemblyOptionsBuilder(filesWithoutErrors, this.opts)
await builder
.build()
.then((assemblies) => Promise.all(assemblies.map(createAssembly)))
.then((maybeCreatedAssemblies) => {
const createdAssemblies = maybeCreatedAssemblies.filter(Boolean)
const assemblyIDs = createdAssemblies.map(
(assembly) => assembly.status.assembly_id,
)
this.#createAssemblyWatcher(assemblyIDs, uploadID)
return Promise.all(
createdAssemblies.map((assembly) => this.#connectAssembly(assembly)),
)
})
// If something went wrong before any Assemblies could be created,
// clear all processing state.
.catch((err) => {
filesWithoutErrors.forEach((file) => {
this.uppy.emit('preprocess-complete', file)
this.uppy.emit('upload-error', file, err)
})
throw err
})
}
#afterUpload = (fileIDs: string[], uploadID: string): Promise<void> => {
const files = fileIDs.map((fileID) => this.uppy.getFile(fileID))
// Only use files without errors
const filteredFileIDs = files
.filter((file) => !file.error)
.map((file) => file.id)
const state = this.getPluginState()
// If we're still restoring state, wait for that to be done.
if (this.restored) {
return this.restored.then(() => {
return this.#afterUpload(filteredFileIDs, uploadID)
})
}
const assemblyIDs = state.uploadsAssemblies[uploadID]
const closeSocketConnections = () => {
assemblyIDs.forEach((assemblyID) => {
const assembly = this.activeAssemblies[assemblyID]
assembly.close()
delete this.activeAssemblies[assemblyID]
})
}