-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (42 loc) · 1.18 KB
/
index.js
File metadata and controls
48 lines (42 loc) · 1.18 KB
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
'use strict'
const adapter = require('./adapter')
const fileCache = require('./file-cache')
const processAndUploadImage = require('./image-processing')
module.exports = config => {
const defaults = {
adapter: {
provider: 'filesystem',
path: process.cwd()
},
cache: {
ttl: '',
enable: false,
path: ''
}
}
config = Object.assign({}, defaults, config)
const client = adapter.create(config.adapter)
const cache = fileCache.create(config.cache)
return {
upload (name, data, options) {
options = options || {}
if (!/^image\/.*/.test(options.ContentType)) {
return client.upload(name, data, options).then(cache.put(name, data))
} else {
const inputArgs = { name, data, options }
return processAndUploadImage(client, inputArgs, cache)
}
},
download (name, options) {
options = options || {}
return cache.get(name, options)
.catch(() => client.download(name, options).then(data => {
return cache.put(name, data).then(() => data)
}))
},
getUrl (name, options) {
options = options || {}
return client.getUrl(name, options)
}
}
}