-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
95 lines (76 loc) · 2.54 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
'use strict';
const BaseStorage = require("ghost-storage-base");
const fs = require("fs");
const Promise = require("bluebird");
const request = require("request");
const azure = require('azure-storage');
const url = require('url');
var options = {};
class AzureStorageAdapter extends BaseStorage {
constructor(config) {
super();
options = config || {};
options.connectionString = options.connectionString || process.env.AZURE_STORAGE_CONNECTION_STRING;
options.container = options.container || 'ghost';
options.useHttps = options.useHttps == 'true';
}
exists(filename) {
console.log(filename);
return request(filename)
.then(res => res.statusCode === 200)
.catch(() => false);
}
save(image) {
var fileService = azure.createBlobService(options.connectionString);
let date = new Date();
var uniqueName = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + date.getHours() + date.getMinutes() + "_" + image.name;
return new Promise(function (resolve, reject) {
fileService.createContainerIfNotExists(options.container, { publicAccessLevel: 'blob' }, function (error) {
if (error)
console.log(error);
else {
console.log('Created the container or already existed. Container:' + options.container);
fileService.createBlockBlobFromLocalFile(options.container, uniqueName, image.path, function (error) {
if (error) {
console.log(error);
reject(error.message);
}
else {
var urlValue = fileService.getUrl(options.container, uniqueName);
if (!options.cdnUrl) {
resolve(urlValue);
}
var parsedUrl = url.parse(urlValue, true, true);
var protocol = (options.useHttps ? "https" : "http") + "://";
resolve(protocol + options.cdnUrl + parsedUrl.path);
}
});
}
});
});
}
serve() {
return function customServe(req, res, next) {
next();
}
}
delete() {
}
read(options) {
return new Promise(function (resolve, reject) {
var requestSettings = {
method: 'GET',
url: options.path,
encoding: null
};
request(requestSettings, function (error, response, body) {
// Use body as a binary Buffer
if (error)
return reject(new Error("Cannot download image" + options.path));
else
resolve(body);
});
})
}
}
module.exports = AzureStorageAdapter;