-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (91 loc) · 3.07 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
/* eslint-disable no-promise-executor-return */
/* eslint-disable max-lines */
'use strict'
const BaseAdapter = require('ghost-storage-base')
const logging = require('@tryghost/logging');
const rokka = require('rokka')
const fs = require("fs")
const request = require('request').defaults({encoding: null})
class RokkaAdapter extends BaseAdapter {
constructor(options) {
super(options)
const config = options || {}
// clone options and overwrite key for not leaking that to logs
const outputOptions = Object.assign({}, options)
outputOptions.key = "******"
logging.debug(`Rokka configuration: ${JSON.stringify(outputOptions)}`);
this.org = options.organization
this.defaultStack = options.defaultStack || 'dynamic/o-af-1'
this.sourceFileStack = options.sourceFileStack || 'source_file'
this.rawFileExtensions = options.rawFileExtensions?.split(',') || ['mp3']
this.rokka = rokka({apiKey: config.key || ''})
this.addFaceDetection = options.addFaceDetection || false
logging.info('Rokka Storage Adapter loaded');
}
exists() {
//Rokka handles this already.
return false;
}
urlToPath() {
//Rokka stores element flat.
return '/';
}
_defineStackToUse(fileName) {
if (!fileName) {
return this.defaultStack;
}
const extension = fileName.split('.').pop();
return this.rawFileExtensions.includes(extension) ? this.sourceFileStack : this.defaultStack;
}
async save(file, noFace) {
const stream = fs.createReadStream(file.path)
return new Promise((resolve, reject) => {
const meta = {
meta_user: {'tool': 'ghost'}
}
if (this.addFaceDetection && !noFace) {
meta["meta_dynamic"] = {'detection_face': {}}
}
const fileName = file.originalname ?? file.name
this.rokka.sourceimages.create(this.org, fileName, stream, meta).then(res => {
const rokkaImage = res.body.items[0]
const stackToUse = this._defineStackToUse(fileName);
const link = 'https://' + this.org + '.rokka.io/' + stackToUse + '/' + rokkaImage.short_hash + '/' + encodeURIComponent(
rokkaImage.name.replace(/\.[a-zA-Z]{3,4}$/,"").
replace(/[.\-]/g,"_")
) + '.' + rokkaImage.format
logging.info(`File Uploaded and accessible at: ${link}`);
resolve(link)
}).catch(err => {
logging.debug(`Error: ${JSON.stringify(err)}`)
// try without face, maybe there's an error there
if (this.addFaceDetection && !noFace) {
this.save(file, true)
} else {
reject(err)
}
})
})
}
serve() {
return (req, res, next) => {
next()
}
}
delete() {
// Let Ghost believe that the file has been delete. Rokka manages it
}
read(options) {
const opts = options || {}
return new Promise((resolve, reject) => request.get(opts.path, (err, res) => {
if (err) {
return reject({
err: err,
message: `Could not read image ${opts.path}`
})
}
return resolve(res.body)
}))
}
}
module.exports = RokkaAdapter