This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
75 lines (67 loc) · 2.08 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
var BlueBird = require('bluebird');
var cloudinary = require('cloudinary');
var util = require('util');
var BaseAdapter = require('ghost-storage-base');
var path = require('path');
class CloudinaryAdapter extends BaseAdapter{
constructor(options) {
super(options);
this.config = options || {};
cloudinary.config(options);
}
exists(filename) {
return new BlueBird(function(resolve) {
cloudinary.v2.api.resource(path.parse(filename).name, {type: 'upload'}, function(error, result) {
if (result) {
resolve(result);
} else {
resolve(false);
}
});
});
}
save(image, targetDir) {
var cloudinaryImageSettings = this.config.configuration.image;
var cloudinaryFileSettings = this.config.configuration.file || {};
//Using the real image name sanitizing it for the web
cloudinaryFileSettings.public_id = path.parse(this.getSanitizedFileName(image.name)).name;
return new BlueBird(function(resolve) {
cloudinary.uploader.upload(image.path, function(result) {
if (result.error) {
return reject(new errors.GhostError({
err: result.error,
message: 'Could not upload the image: ' + image.path
}));
} else {
resolve(cloudinary.url(result.public_id.concat(".", result.format), cloudinaryImageSettings));
}
}, cloudinaryFileSettings);
});
}
serve() {
return function customServe(req, res, next) {
next();
};
}
delete(filename) {
return new BlueBird(function(resolve) {
cloudinary.uploader.destroy(path.parse(filename).name, function(result) {
resolve(result);
});
});
}
read(options) {
options = options || {};
return new BlueBird(function (resolve, reject) {
var request = require('request').defaults({ encoding: null });
request.get(options.path, function (err, res) {
if (err) {
reject(new Error("Cannot download image"));
} else {
resolve(res.body);
}
});
});
}
}
module.exports = CloudinaryAdapter;