-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (60 loc) · 1.9 KB
/
index.js
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
module.exports = {
init(options) {
const matrix = new Matrix(options);
return {
upload(file, params = {}) {
const provider = matrix.getProvider(file);
strapi.log.info(`[strapi-provider-upload-matrix] File uploading using [${provider.name}] provider`);
return provider.module.upload(file, params);
},
delete(file, params = {}) {
const provider = matrix.getProvider(file);
strapi.log.info(`[strapi-provider-upload-matrix] File deleting using [${provider.name}] provider`);
return provider.module.delete(file, params);
},
};
},
};
class Matrix {
constructor(options) {
this._options = options;
this._providers = {};
}
getProvider(file) {
const provider = this.resolve(file);
if (!provider) {
throw "[strapi-provider-upload-matrix] No provider provided";
}
const id = provider.id;
const name = provider.use.provider;
const options = provider.use.providerOptions;
if (!this._providers[id]) {
// Init provider and store to cache
this._providers[id] = {
id,
name,
options,
module: require(`strapi-provider-upload-${name}`).init(options),
}
}
return this._providers[id];
}
resolve(file) {
// Lookup by extension
const byExtension = this.resolveByExtension(file.ext);
if (byExtension) return byExtension;
// Lookup by mime type
// @todo
// Fallback
const byFallback = this.resolveByFallback();
if (byFallback) return byFallback;
strapi.log.error(`[strapi-provider-upload-matrix] No fallback provider defined`);
}
resolveByExtension(extension) {
const ext = extension.replace(/\./g, '').toLowerCase();
return (this._options.resolvers || []).find(r => (r.test.ext || []).includes(ext));
}
resolveByFallback() {
return (this._options.resolvers || []).find(r => r.test === '*');
}
}