Skip to content

Commit

Permalink
Merge pull request #8 from ajuste/fix/gstorage-directory
Browse files Browse the repository at this point in the history
gstorage transform path for directories to work
  • Loading branch information
ajuste committed May 21, 2017
2 parents f818603 + 088d440 commit 8aa725f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
17 changes: 14 additions & 3 deletions lib/filesystem-google-storage.js
Expand Up @@ -35,7 +35,18 @@ GoogleStorageClient = (function() {
}

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


/**
* @function Transforms a path to confirm with google storage
* @param {String} path The path to be transformed
* @returns {String} transformed path
*/

GoogleStorageClient.prototype.transformPath = function(path) {
return path.replace(/^(\/)+/, '');
};


Expand All @@ -49,7 +60,7 @@ GoogleStorageClient = (function() {
GoogleStorageClient.prototype.move = function(source, target) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(source).move(target, function(err) {
return _this.file(source).move(_this.transformPath(target), function(err) {
if (err != null) {
return rej(err);
} else {
Expand Down Expand Up @@ -135,7 +146,7 @@ GoogleStorageClient = (function() {
GoogleStorageClient.prototype.copy = function(from, to) {
return new Promise((function(_this) {
return function(res, rej) {
return _this.file(from).copy(to, function(err, exists) {
return _this.file(from).copy(_this.transformPath(to), function(err, exists) {
if (err != null) {
return rej(err);
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jaune-fs",
"version": "0.0.7",
"version": "0.0.8",
"description": "file system for jaune framework",
"main": "index.js",
"scripts": {
Expand Down
14 changes: 11 additions & 3 deletions src/filesystem-google-storage.coffee
Expand Up @@ -29,7 +29,14 @@ class GoogleStorageClient
@storage = require('@google-cloud/storage') @connection
@bucket = @storage.bucket @connection.bucketName

file: (path) -> @bucket.file path
file: (path) -> @bucket.file @transformPath path

###*
* @function Transforms a path to confirm with google storage
* @param {String} path The path to be transformed
* @returns {String} transformed path
###
transformPath: (path) -> path.replace /^(\/)+/, ''

###*
* @function Move a file
Expand All @@ -42,7 +49,7 @@ class GoogleStorageClient
new Promise (res, rej) =>

@file source
.move target, (err) -> if err? then rej err else res()
.move @transformPath(target), (err) -> if err? then rej err else res()

###*
* @function Writes a file bucket
Expand Down Expand Up @@ -97,7 +104,8 @@ class GoogleStorageClient

new Promise (res, rej) =>

@file(from).copy to, (err, exists) -> if err? then rej err else res()
@file(from).copy @transformPath(to), (err, exists) ->
if err? then rej err else res()

###*
* @function Removes a file
Expand Down
31 changes: 31 additions & 0 deletions test/filesystem-google-storage/transformPath.coffee
@@ -0,0 +1,31 @@
{
equal
} = require 'assert'

{
GoogleStorageClient
} = require '../../lib/filesystem-google-storage'

stream = require 'stream'

connection =
bucketName: 'free-fair-core'
projectId: 'free-fair'
credentials:
project_id: 'proj-id'
private_key: 'the-key'

describe 'filesystem-google-storage-fs', ->

describe 'transformPath', ->

before ->
@fs = new GoogleStorageClient connection

it 'should transformPath for a path starting with /', ->

equal @fs.transformPath('////a/b/c'), 'a/b/c'

it 'does not transform', ->

equal @fs.transformPath('a/b/c'), 'a/b/c'

0 comments on commit 8aa725f

Please sign in to comment.