-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
203 lines (168 loc) · 6.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
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
'use strict';
require('bluebird');
const StorageBase = require('ghost-storage-base'),
path = require('path'),
errors = require(path.join(__dirname, 'errors')),
debug = require('@tryghost/debug')('adapter'),
cloudinary = require('cloudinary').v2,
got = require('got'),
plugins = require(path.join(__dirname, '/plugins'));
class CloudinaryAdapter extends StorageBase {
/**
* @override
*/
constructor(options) {
super(options);
const config = options || {},
auth = config.auth || config,
// Kept to avoid a BCB with forked repo
legacy = config.configuration || {},
uploadOptions = config.upload || legacy.file || {},
fetchOptions = config.fetch || legacy.image || {};
this.useDatedFolder = config.useDatedFolder || false;
this.plugins = config.plugins || {};
this.uploaderOptions = {
upload: uploadOptions,
fetch: fetchOptions
};
debug('constructor:useDatedFolder:', this.useDatedFolder);
debug('constructor:uploaderOptions:', this.uploaderOptions);
debug('constructor:plugins:', this.plugins);
cloudinary.config(auth);
}
/**
* @override
*/
exists(filename) {
debug('exists:filename', filename);
const pubId = this.toCloudinaryId(filename);
return new Promise((resolve) => cloudinary.uploader.explicit(pubId, { type: 'upload' }, (err) => {
if (err) {
return resolve(false);
}
return resolve(true);
}));
}
/**
* @override
*/
save(image) {
// Creates a deep clone of Cloudinary options
const uploaderOptions = JSON.parse(JSON.stringify(Object.assign({}, this.uploaderOptions)));
// Forces the Cloudinary Public ID value based on the file name when upload option
// "use_filename" is set to true.
if (uploaderOptions.upload.use_filename !== 'undefined' && uploaderOptions.upload.use_filename) {
Object.assign(
uploaderOptions.upload,
{ public_id: path.parse(this.getSanitizedFileName(image.name)).name }
);
}
// Appends the dated folder if enabled
if (this.useDatedFolder) {
uploaderOptions.upload.folder = this.getTargetDir(uploaderOptions.upload.folder);
}
debug('save:uploadOptions:', uploaderOptions);
// Retinizes images if there is any config provided
if (this.plugins.retinajs) {
const retinajs = new plugins.RetinaJS(this.uploader, uploaderOptions, this.plugins.retinajs);
return retinajs.retinize(image);
}
return this.uploader(image.path, uploaderOptions, true);
}
/**
* Uploads an image with options to Cloudinary
* @param {string} imagePath The image path to upload (local or remote)
* @param {object} options Cloudinary upload + fetch options
* @param {boolean} url If true, will do an extra Cloudinary API call to fetch the uploaded image with fetch options
* @return {Promise} The uploader Promise
*/
uploader(imagePath, options, url) {
debug('uploader:imagePath', imagePath);
debug('uploader:options', options);
debug('uploader:url', url);
return new Promise((resolve, reject) => cloudinary.uploader.upload(imagePath, options.upload, (err, res) => {
if (err) {
debug('cloudinary.uploader:error', err);
return reject(new errors.CloudinaryAdapterError({
err: err,
message: `Could not upload image ${imagePath}`
}));
}
debug('cloudinary.uploader:res', res);
if (url) {
return resolve(cloudinary.url(res.public_id.concat('.', res.format), options.fetch));
}
return resolve();
}));
}
/**
* @override
*/
serve() {
return (req, res, next) => {
next();
};
}
/**
* @override
*/
delete(filename) {
debug('delete:filename', filename);
const pubId = this.toCloudinaryId(filename);
return new Promise((resolve, reject) => cloudinary.uploader.destroy(pubId, (err, res) => {
if (err) {
debug('delete:error', err);
return reject(new errors.CloudinaryAdapterError({
err: err,
message: `Could not delete image ${filename}`
}));
}
return resolve(res);
}));
}
/**
* @override
*/
read(options) {
const opts = options || {};
debug('read:opts', opts);
return new Promise(async (resolve, reject) => {
try {
return resolve(await got(opts.path, {
responseType: 'buffer',
resolveBodyOnly: true
}));
} catch (err) {
debug('read:error', err);
return reject(new errors.CloudinaryAdapterError({
err: err,
message: `Could not read image ${opts.path}`
}));
}
});
}
/**
* Extracts the a Cloudinary-ready file name for API usage.
* If a "folder" upload option is set, it will prepend its
* value.
* @param {string} filename The wanted image filename
* @return {string} Cloudinary-ready image name
*/
toCloudinaryFile(filename) {
const file = path.parse(filename).base;
if (typeof this.uploaderOptions.upload.folder !== 'undefined') {
return path.join(this.uploaderOptions.upload.folder, file);
}
return file;
}
/**
* Returns the Cloudinary public ID off a given filename
* @param {string} filename The wanted image filename
* @return {string} Cloudinary-ready ID for given image
*/
toCloudinaryId(filename) {
const parsed = path.parse(this.toCloudinaryFile(filename));
return path.join(parsed.dir, parsed.name);
}
}
module.exports = CloudinaryAdapter;