UNPKG

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