-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
172 lines (144 loc) · 4.41 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict'
const Base = require('bfx-facs-base')
const { getAsciiFileName } = require('./helpers')
class S3Grc extends Base {
constructor (caller, opts, ctx) {
super(caller, opts, ctx)
this.name = 'grc-s3'
this._hasConf = true
this.init()
this.uploadS3 = this.uploadS3.bind(this)
this.getDownloadUrl = this.getDownloadUrl.bind(this)
this.getInlineUrl = this.getInlineUrl.bind(this)
this.deleteFromS3 = this.deleteFromS3.bind(this)
}
base64MimeType (encoded) {
if (typeof encoded !== 'string') return null
const mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/)
if (mime && mime.length > 1) return mime[1]
else return null
}
base64ParseData (encoded) {
if (typeof encoded !== 'string') return null
const parse = encoded.split(',')
if (parse && parse.length > 1) return parse[1]
else return null
}
base64DataCheck (encoded) {
return this.base64MimeType(encoded) && this.base64ParseData(encoded)
}
parseBase64DataForS3 (encoded, filename, key, base64 = true) {
const s3 = this.conf
const header = {
contentType: this.base64MimeType(encoded),
acl: s3.acl,
bucket: s3.bucket
}
const asciiFileName = getAsciiFileName(filename)
if (asciiFileName) {
header.contentDisposition = `${s3.contentDisposition}; filename="${asciiFileName}"`
}
if (key) header.key = key
let buffer = encoded
if (base64) buffer = Buffer.from(this.base64ParseData(encoded), 'base64')
return [
buffer.toString('hex'), header
]
}
uploadS3 (data, filename, key, cb = null) {
let parsedData = data
const s3 = this.conf
const worker = s3.worker || 'rest:ext:s3'
if (this.base64DataCheck(data)) {
parsedData = this.parseBase64DataForS3(data, filename, key)
} else {
parsedData = this.parseBase64DataForS3(data, filename, key, false)
}
return this.caller.grc_bfx.req(
worker,
'uploadPublic',
parsedData,
{ timeout: 10000 },
cb)
}
/**
* @param {string} filename
* @param {string} key
* @param {object|function|null} [opts]
* @param {string} [opts.bucketName]
* @param {function} [cb]
* @returns {Promise|void}
*/
getDownloadUrl (filename, key, opts = null, cb = null) {
const asciiFileName = getAsciiFileName(filename)
const responseDisposition = (asciiFileName)
? `attachment; filename=${asciiFileName}`
: 'attachment'
return this._getPresignedUrl(responseDisposition, key, opts, cb)
}
/**
* @param {string} filename
* @param {string} key
* @param {object|function|null} [opts]
* @param {string} [opts.bucketName]
* @param {function} [cb]
* @returns {Promise|void}
*/
getInlineUrl (filename, key, opts = null, cb = null) {
const asciiFileName = getAsciiFileName(filename)
const responseDisposition = (asciiFileName)
? `inline; filename=${asciiFileName}`
: 'inline'
return this._getPresignedUrl(responseDisposition, key, opts, cb)
}
_getPresignedUrl (responseDisposition, key, opts = null, cb = null) {
if (typeof opts === 'function') {
cb = opts
opts = null
}
const signedUrlExpireTime = 120
const s3 = this.conf
const bucket = (opts && opts.bucketName) || s3.bucket
const worker = s3.worker || 'rest:ext:s3'
const optsGetPresignedUrl = [{
key, bucket, signedUrlExpireTime, responseDisposition
}]
return this.caller.grc_bfx.req(
worker,
'getPresignedUrl',
optsGetPresignedUrl,
{ timeout: 10000 },
cb)
}
deleteFromS3 (files, cb = null) {
if (!Array.isArray(files)) {
const err = new Error('ERR_API_NO_files_ARR')
return cb ? cb(err) : Promise.reject(err)
}
if (!files.length) {
const err = new Error('ERR_API_EMPTY_files_ARR')
return cb ? cb(err) : Promise.reject(err)
}
const deleteFiles = files.map(
file => file && file.key && { key: file.key }
)
if (deleteFiles.some(file => !file)) {
const err = new Error('ERR_API_NO_KEY_IN_FILE')
return cb ? cb(err) : Promise.reject(err)
}
const s3 = this.conf
const worker = s3.worker || 'rest:ext:s3'
const header = {
acl: s3.acl,
bucket: s3.bucket
}
const data = [deleteFiles, header]
return this.caller.grc_bfx.req(
worker,
'deleteFiles',
data,
{ timeout: 10000 },
cb)
}
}
module.exports = S3Grc