-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.15 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
const { Storage } = require('@google-cloud/storage');
const BaseAdapter = require('ghost-storage-base');
class GoogleCloudStorage extends BaseAdapter {
constructor(config) {
super();
this.config = config;
this.storage = new Storage({
projectId: this.config.projectId,
keyFilename: this.config.keyFilename
});
this.bucket = this.storage.bucket(this.config.bucketName);
}
async save(file) {
const options = {
destination: file.name,
resumable: false,
public: true,
metadata: {
cacheControl: `public, max-age=2678400`
},
};
try {
const [uploadedFile] = await this.bucket.upload(file.path, options);
return uploadedFile.publicUrl();
} catch (err) {
console.error(err);
}
}
async exists(fileName) {
const [exists] = await this.bucket.file(fileName).exists();
return exists;
}
serve() {
return (req, res, next) => next();
}
async delete(fileName) {
return await this.bucket.file(fileName).delete();
}
read(options) {
return this.bucket.file(options.path).createReadStream();
}
}
module.exports = GoogleCloudStorage;