Skip to content

Commit

Permalink
feat: google storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuste committed May 7, 2017
1 parent d1c17d0 commit 4f68215
Show file tree
Hide file tree
Showing 12 changed files with 1,014 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ compile-tests: compile
@$(COFFEE) --output ./test-compiled --no-header --compile -b ./test

# Run lint for coffeescript
run-coffee-link:
./node_modules/coffeelint/bin/coffeelint .
run-coffee-lint:
# ./node_modules/coffeelint/bin/coffeelint src/ test/

test: compile-tests run-coffee-link
test: compile-tests run-coffee-lint
./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha ./test-compiled/**/*.js

test-debug: compile-tests
./node_modules/mocha/bin/mocha --debug-brk ./test-compiled/**/*.js

cover:
cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
238 changes: 238 additions & 0 deletions lib/filesystem-google-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@

/**
* @file NodeJS module that implements file system handler based on Google
* storage
* @author Alvaro Juste <juste.alvaro@gmail.com>
*/
"use strict";
var GoogleStorageClient, Stat, Stream, mime;

mime = require("mime");

Stream = require("stream");


/**
* @class File system constructor.
* @param {object} connection Accepts the connection to the file system.
*/

GoogleStorageClient = (function() {

/**
* @constructor Constructs a new filesystem connection to google cloud storage
* @param {Object} connection The connection which contains the following data
* {
* credentials: {client_email, private_key},
* projectId,
* bucketName,
* }
*/
function GoogleStorageClient(connection) {
this.connection = connection;
this.storage = require('@google-cloud/storage')(this.connection);
this.bucket = this.storage.bucket(this.connection.bucketName);
}

GoogleStorageClient.prototype.file = function(path) {
return this.bucket.file(path);
};


/**
* @function Move a file
* @param {String} source Source key
* @param {String} target Targett key
* @returns {Promise}
*/

GoogleStorageClient.prototype.move = function(source, target) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(source).move(target, function(err) {
if (err != null) {
return rej(err);
} else {
return res();
}
});
};
})(this));
};


/**
* @function Writes a file bucket
* @param {String} path Is the key
* @param {Buffer|String|Stream} data Data to be written into file.
* @param {Object} options Extra options
* @param {String}
* @returns {Promise}
*/

GoogleStorageClient.prototype.writeFile = function(path, data) {
return new Promise((function(_this) {
return function(res, rej) {
var inputStream;
inputStream = _this.file(path).createWriteStream({
metadata: {
contentType: mime.lookup(path)
}
}).on('error', rej).on('finish', res);
if (data instanceof Stream) {
return data.pipe(inputStream);
} else {
return inputStream.end(data);
}
};
})(this));
};


/**
* @function Reads an object
* @param {String} path The key of the object
* @returns {Stream}
*/

GoogleStorageClient.prototype.readFile = function(path) {
return this.file(path).createReadStream();
};


/**
* @function Checks if file exists.
* @param {path} string The key
* @returns {Promise}
*/

GoogleStorageClient.prototype.exists = function(path) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(path).exists(function(err, exists) {
if (err != null) {
return rej(err);
} else {
return res(exists);
}
});
};
})(this));
};


/*
* @function Copies a path into another.
* @param {from} string The source key.
* @param {to} string The destination key.
* @returns {Promise}
*/

GoogleStorageClient.prototype.copy = function(from, to) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(from).copy(to, function(err, exists) {
if (err != null) {
return rej(err);
} else {
return res();
}
});
};
})(this));
};


/**
* @function Removes a file
* @param {String} path The file to be removed
* @param {Boolean} [options.absolute] When true the path is not
* converted to absolute.
* @returns {Promise}
*/

GoogleStorageClient.prototype.removeFile = function(path) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(path)["delete"](function(err) {
if (err != null) {
return rej(err);
} else {
return res();
}
});
};
})(this));
};


/**
* @function Retrieves the STAT object for a specified path.
* @param {path} string The source path.
* @param {function} cb Standard callback function.
* @returns {Promise}
*/

GoogleStorageClient.prototype.stat = function(path) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(path).getMetadata(function(err, meta) {
if (err != null) {
return rej(err);
} else {
return res(new Stat(meta));
}
});
};
})(this));
};

return GoogleStorageClient;

})();


/**
* @class Stat wrapper
* @param {object} head The head object
*/

Stat = (function() {
function Stat(meta1) {
this.meta = meta1;
}


/**
* @function Checks if STAT is a directory.
*/

Stat.prototype.isDirectory = function() {
return false;
};


/**
* @function Returns the MIME type of file.
*/

Stat.prototype.getMime = function() {
return this.meta.contentType;
};


/**
* @function Get size of file in bytes
*/

Stat.prototype.getSize = function() {
return this.meta.size;
};

return Stat;

})();

module.exports = {
GoogleStorageClient: GoogleStorageClient
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "make test",
"prepublish": "make compile",
"cover": "make cover"
},
"author": "Alvaro Juste <juste.alvaro@gmail.com>",
Expand All @@ -16,13 +17,14 @@
"path": "^0.12.7"
},
"devDependencies": {
"@google-cloud/storage": "^1.1.0",
"assert": "^1.3.0",
"coffee-script": "^1.12.5",
"coffeelint": "^1.16.0",
"coveralls": "^2.13.0",
"istanbul": "^0.4.5",
"mocha": "^2.4.5",
"mock-require": "^2.0.1"
"mock-require": "^2.0.2"
},
"engine": "node >= 0.12",
"directories": {
Expand Down
Loading

0 comments on commit 4f68215

Please sign in to comment.