Skip to content

Commit

Permalink
Merge pull request #12 from Niyo/master
Browse files Browse the repository at this point in the history
Ghost 1.X azure storage adapter
  • Loading branch information
TerribleDev authored Oct 26, 2017
2 parents d3db035 + d43f4cd commit 517f9a9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ Create new azure storage account, and get the connection string (can be found in

Add `storage` block to file `config.js` in each environment as below:
```
storage: {
active: 'ghost-azurestorage',
'ghost-azurestorage': {
connectionString: 'YourConnectionStringHere',
container: 'YourOptionalContainerName',
cdnUrl: "YourCDNEndpointDomain",
useHttps : "UseHttpsInEndpoint" //Optional: CDN protocol. Defaults to http if omitted. Set to "true", to enable.
"storage": {
"active": "ghost-azurestorage",
"ghost-azurestorage": {
"connectionString": "YourConnectionStringHere",
"container": "YourOptionalContainerName",
"cdnUrl": "YourCDNEndpointDomain",
"useHttps" : "true" //Optional: CDN protocol. Defaults to http if omitted. Set to "true", to enable.
}
},
```

## Environment Variables
Expand Down
108 changes: 76 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,91 @@
'use strict';
var azure = require('azure-storage'),
fs = require('fs'),
path = require('path'),
when = require('when'),
nodefn = require('when/node'),
url = require('url'),
options = {};
'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();

function azurestore(config) {
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);
}

azurestore.prototype.save = function (image) {
save(image) {
var fileService = azure.createBlobService(options.connectionString);
var uniqueName = new Date().getMonth() + "/" + new Date().getFullYear() + "/" + image.name;
return nodefn.call(fileService.createContainerIfNotExists.bind(fileService), options.container, { publicAccessLevel: 'blob' })
.then(nodefn.call(fileService.createBlockBlobFromLocalFile.bind(fileService), options.container, uniqueName, image.path))
.delay(500) //todo: this was placed here per issue #4 (aka sometimes things 404 right after upload) figure out a better way than just adding a delay
.then(function () {
var urlValue = fileService.getUrl(options.container, uniqueName);

if(!options.cdnUrl){
return urlValue;
}

var parsedUrl = url.parse(urlValue, true, true);
var protocol = (options.useHttps ? "https" : "http") + "://";
let date = new Date();
var uniqueName = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + date.getHours() + date.getMinutes() + "_" + image.name;

return protocol + options.cdnUrl + parsedUrl.path;
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);
}
});
}
});
});
};
}

azurestore.prototype.serve = function () {
return function (req, res, next) {
next();
};
};
serve() {
return function customServe(req, res, next) {
next();
}
}

delete() {

}

read(options) {
return new Promise(function (resolve, reject) {
request.get(options.path, function (err, res)
{
if (err)
return reject(new Error("Cannot download image" + options.path));
else
resolve(res.body);
});
});
}

}

module.exports = azurestore;
module.exports = AzureStorageAdapter;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
},
"homepage": "http://tparnell.io",
"dependencies": {
"azure-storage": "~0.6.0",
"when": "^3.7.4",
"azure-storage": "~2.6.0",
"url": "^0.11.0"
}
}

0 comments on commit 517f9a9

Please sign in to comment.