1 |
|
2 |
|
3 |
|
4 | function 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 |
|
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 |
|
28 |
|
29 |
|
30 | class AssemblyOptions {
|
31 | constructor (files, opts) {
|
32 | this.files = files
|
33 | this.opts = opts
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
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 |
|
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 |
|
81 |
|
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 |
|
102 |
|
103 |
|
104 |
|
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 |
|
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 |
|
131 |
|
132 | return Promise.resolve([])
|
133 | }
|
134 | }
|
135 |
|
136 | module.exports = AssemblyOptions
|
137 | module.exports.validateParams = validateParams
|