UNPKG

3.55 kBJavaScriptView Raw
1/**
2 * Check that Assembly parameters are present and include all required fields.
3 */
4function validateParams (params) {
5 if (!params) {
6 throw new Error('Transloadit: The `params` option is required.')
7 }
8
9 if (typeof params === 'string') {
10 try {
11 params = JSON.parse(params)
12 } catch (err) {
13 // Tell the user that this is not an Uppy bug!
14 err.message = 'Transloadit: The `params` option is a malformed JSON string: ' +
15 err.message
16 throw err
17 }
18 }
19
20 if (!params.auth || !params.auth.key) {
21 throw new Error('Transloadit: The `params.auth.key` option is required. ' +
22 'You can find your Transloadit API key at https://transloadit.com/account/api-settings.')
23 }
24}
25
26/**
27 * Turn Transloadit plugin options and a list of files into a list of Assembly
28 * options.
29 */
30class AssemblyOptions {
31 constructor (files, opts) {
32 this.files = files
33 this.opts = opts
34 }
35
36 /**
37 * Normalize Uppy-specific Assembly option features to a Transloadit-
38 * compatible object.
39 */
40 _normalizeAssemblyOptions (file, assemblyOptions) {
41 if (Array.isArray(assemblyOptions.fields)) {
42 const fieldNames = assemblyOptions.fields
43 assemblyOptions.fields = {}
44 fieldNames.forEach((fieldName) => {
45 assemblyOptions.fields[fieldName] = file.meta[fieldName]
46 })
47 }
48
49 if (!assemblyOptions.fields) {
50 assemblyOptions.fields = {}
51 }
52
53 return assemblyOptions
54 }
55
56 /**
57 * Get Assembly options for a file.
58 */
59 _getAssemblyOptions (file) {
60 const options = this.opts
61
62 return Promise.resolve()
63 .then(() => {
64 return options.getAssemblyOptions(file, options)
65 })
66 .then((assemblyOptions) => {
67 return this._normalizeAssemblyOptions(file, assemblyOptions)
68 })
69 .then((assemblyOptions) => {
70 validateParams(assemblyOptions.params)
71
72 return {
73 fileIDs: [file.id],
74 options: assemblyOptions
75 }
76 })
77 }
78
79 /**
80 * Combine Assemblies with the same options into a single Assembly for all the
81 * relevant files.
82 */
83 _dedupe (list) {
84 const dedupeMap = Object.create(null)
85 list.forEach(({ fileIDs, options }) => {
86 const id = JSON.stringify(options)
87 if (dedupeMap[id]) {
88 dedupeMap[id].fileIDs.push(...fileIDs)
89 } else {
90 dedupeMap[id] = {
91 options,
92 fileIDs: [...fileIDs]
93 }
94 }
95 })
96
97 return Object.keys(dedupeMap).map((id) => dedupeMap[id])
98 }
99
100 /**
101 * Generate a set of Assemblies that will handle the upload.
102 * Returns a Promise for an object with keys:
103 * - fileIDs - an array of file IDs to add to this Assembly
104 * - options - Assembly options
105 */
106 build () {
107 const options = this.opts
108
109 if (this.files.length > 0) {
110 return Promise.all(
111 this.files.map((file) => this._getAssemblyOptions(file))
112 ).then((list) => {
113 return this._dedupe(list)
114 })
115 }
116
117 if (options.alwaysRunAssembly) {
118 // No files, just generate one Assembly
119 return Promise.resolve(
120 options.getAssemblyOptions(null, options)
121 ).then((assemblyOptions) => {
122 validateParams(assemblyOptions.params)
123 return [{
124 fileIDs: this.files.map((file) => file.id),
125 options: assemblyOptions
126 }]
127 })
128 }
129
130 // If there are no files and we do not `alwaysRunAssembly`,
131 // don't do anything.
132 return Promise.resolve([])
133 }
134}
135
136module.exports = AssemblyOptions
137module.exports.validateParams = validateParams