-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
151 lines (131 loc) · 3.59 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
/**
* Created by ChengZheLin on 2019/5/10.
* Features:
*/
'use strict'
const COS = require('cos-nodejs-sdk-v5')
const BaseAdapter = require('ghost-storage-base')
const URL = require('url')
// const RE = /(.*)(?=\/ghost\/content)/
class QCloudCustomAdapter extends BaseAdapter {
constructor (config) {
super()
let cfg = config || {};
if (!config || !config?.baseUrl) {
const env = process.env;
cfg = {
"baseUrl": env.GHOST_STORAGE_ADAPTER_COS_BASEURL,
"basePath": env.GHOST_STORAGE_ADAPTER_COS_BASEPATH,
"rename": env.GHOST_STORAGE_ADAPTER_COS_RENAME === 'true',
"SecretId": env.GHOST_STORAGE_ADAPTER_COS_SECRETID,
"SecretKey": env.GHOST_STORAGE_ADAPTER_COS_SECRETKEY,
"Bucket": env.GHOST_STORAGE_ADAPTER_COS_BUCKET,
"Region": env.GHOST_STORAGE_ADAPTER_COS_REGION
}
}
this.baseParams = {
Bucket: cfg.Bucket,
Region: cfg.Region
}
this.baseUrl = cfg.baseUrl
this.basePath = cfg.basePath || '/ghost/content/images/'
this.rename = cfg.rename || false
this.cos = new COS({
SecretId: cfg.SecretId,
SecretKey: cfg.SecretKey
})
}
exists (fileName, targetDir) {
const fileUrl = path.join(targetDir || this.storagePath, fileName)
const url = new URL(fileUrl);
const Key = url.pathname
return new Promise((resolve, reject) => {
this.cos.headObject({
...this.baseParams,
Key
}, (err, data) => {
if (err) {
err.statusCode
? resolve(false)
: reject(err)
} else {
data.statusCode === 200
? resolve(true)
: resolve(false)
}
})
})
}
save (file) {
return new Promise((resolve, reject) => {
this.cos.sliceUploadFile({
...this.baseParams,
Key: this.generatePushKey(file),
FilePath: file.path
}, (err, data) => {
if (err) {
reject(err)
} else {
let url = this.baseUrl
? data.Location.replace(/.*\.com\//, this.baseUrl)
: '//' + data.Location
resolve(url)
}
})
})
}
generatePushKey (file) {
const date = new Date()
let YY = date.getFullYear()
let MM = date.getMonth() + 1
if (MM <= 9) { MM = '0' + MM }
if (this.rename) {
return `${this.basePath}${YY}/${MM}/${file.filename.replace(/\s+/img, '').substring(0, 16)}${file.ext}`
} else {
return `${this.basePath}${YY}/${MM}/${file.name}`
}
}
serve () {
return function customServe (req, res, next) {
next()
}
}
delete () {
return Promise.reject('not implemented')
/*return new Promise((resolve, reject) => {
this.cos.deleteObject({
...this.bsseParams,
key
}, function (err, data) {
if (err) {
reject(err)
} else {
(data.statusCode < 300 && data.statusCode >= 200)
? resolve(true)
: resolve(false)
}
})
})*/
}
read (options) {
// const Key = options.path.replace(this.baseUrl, '')
const Key = new URL(options.path).pathname
return new Promise((resolve, reject) => {
this.cos.getObject({
...this.baseParams,
Key
}, (err, data) => {
if (err || data.error) {
return reject(new Error(`
Could not read image \n ${Key}
error: ${JSON.stringify(err)}
response: ${JSON.stringify(data)}
`))
}
resolve(data.Body)
})
})
}
}
exports.default = QCloudCustomAdapter;
module.exports = QCloudCustomAdapter;