forked from alirizakeles/ghost-minio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (201 loc) · 6.02 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import StorageBase from 'ghost-storage-base'
import path, { join } from 'path'
import { readFile } from 'fs'
const Minio = require('minio')
const sanitizeFilename = require('sanitize-filename')
const readFileAsync = fp => new Promise((resolve, reject) => readFile(fp, (err, data) => err ? reject(err) : resolve(data)))
class Store extends StorageBase {
constructor(config = {}) {
super(config)
const {
endpoint,
port,
useSSL,
accessKey,
secretKey,
bucket
} = config
this.endpoint = process.env.storage__minio__endPoint || endpoint || ''
this.port = process.env.storage__minio__port+'' || port+'' || ''
if(process.env.storage__minio__useSSL && ['0', 'false'].includes(process.env.storage__minio__useSSL)){
this.useSSL = false
}else if(process.env.storage__minio__useSSL && ['1', 'true'].includes(process.env.storage__minio__useSSL)){
this.useSSL = true
}else if (process.env.storage__minio__useSSL){
throw new Error(`Invalid value ${process.env.storage__minio__useSSL} for useSSL`)
}else{
this.useSSL = useSSL || false
}
this.accessKey = process.env.storage__minio__accessKey || accessKey || ''
this.secretKey = process.env.storage__minio__secretKey || secretKey || ''
this.bucket = process.env.storage__minio__bucket || bucket || ''
let protocol = 'http://'
if (this.useSSL) {
protocol = 'https://'
}
let urlPort = ''
if (!isNaN(this.port)) {
let parsedPort = Number.parseInt(this.port)
if (parsedPort != 80 && parsedPort != 443) {
urlPort = ':' + this.port
}
}
this.baseUrl = protocol + this.endpoint + urlPort
}
/**
* Creates a chainable Minio object
*
* @return {Minio} An instace of the Minio client
*/
minioClient() {
let configs = {
endPoint: this.endpoint,
useSSL: this.useSSL,
accessKey: this.accessKey,
secretKey: this.secretKey
}
if (!isNaN(this.port)) {
let port = Number.parseInt(this.port)
if (port != 80 && port != 443) {
configs.port = port
}
}
return new Minio.Client(configs)
}
/**
* Check for the existence of a given file and will return
* metadata of that object if it does exist.
*
* @param {string} fileName
* @param {string} targetDir
* @return {Object}
*/
exists(fileName, targetDir) {
const directory = targetDir || this.getTargetDir(this.pathPrefix)
let _self = this
return new Promise((resolve, reject) => {
this.minioClient()
.statObject(_self.bucket, join(directory, fileName), (err, stat) => {
if (err) {
resolve(false)
}
resolve(true)
})
})
}
/**
* Saves an object to Minio and returns the URL for
* said object.
*
* @param {object} fileObject A Ghost provided object of metadata
* @param {string} targetDir
* @return {string} objectUrl A "resolved promise" (?) of the object's full URL
*/
save(fileObject, targetDir) {
const directory = targetDir || this.getTargetDir(this.pathPrefix)
let _self = this
return new Promise((resolve, reject) => {
Promise.all([
readFileAsync(fileObject.path)
]).then(([ fileStream, file ]) => {
this.getUniqueFileName(fileObject, directory).then((fileName) => {
let fileParse = path.parse(fileName)
_self.minioClient()
.putObject(_self.bucket, fileName, fileStream, fileObject.size, (err, etag) => {
if (err) {
reject(err)
}
let objectUrl = _self.baseUrl + '/' + _self.bucket + '/' +fileParse.dir+'/'+encodeURIComponent(fileParse.base)
resolve(objectUrl)
})
}).catch(err => reject(err))
})
.catch(err => reject(err))
})
}
/**
* Stream a file from Minio
*
* @param {string} fileName
* @param {string} targetDir
* @return {stream}
*/
serve(fileName, targetDir) {
const directory = targetDir || this.getTargetDir(this.pathPrefix)
let _self = this
return function customServe(req, res, next) {
const size = 0
this.minioClient()
.getObject(_self.bucket, join(directory, fileName), (err, stream) => {
if (err) {
next(err)
}
stream.on('data', (chunk) => {
size += chunk.length
})
stream.on('end', () => {
next(res)
})
stream.on('error', (err) => {
next(err)
})
})
.pipe(res)
}
}
/**
* Delete an object from Minio.
*
* @param {string} fileName
* @param {string} targetDir
* @return {boolean}
*/
delete(fileName, targetDir) {
const directory = targetDir || this.getTargetDir(this.pathPrefix)
let _self = this
return new Promise((resolve, reject) => {
this.minioClient()
.removeObject(_self.bucket, join(directory, fileName), (err) => {
if (err) {
reject(err)
}
resolve(true)
})
})
}
/**
* Stream the file from Minio.
*
* @param {string} fileName
* @param {string} targetDir
* @return {stream}
*/
read(fileName, targetDir) {
const directory = targetDir || this.getTargetDir(this.pathPrefix)
let _self = this
return new Promise((resolve, reject) => {
const size = 0
this.minioClient()
.getObject(_self.bucket, join(directory, fileName), (err, stream) => {
if (err) {
reject(err)
}
stream.on('data', (chunk) => {
size += chunk.length
})
stream.on('end', () => {
resolve(res)
})
stream.on('error', (err) => {
reject(err)
})
})
})
}
getSanitizedFileName(fileName) {
// below only matches ascii characters, @, and .
// unicode filenames like город.zip would therefore resolve to ----.zip
return sanitizeFilename(fileName, { replacement: '_' })
}
}
export default Store