Add Container/File classes
This commit is contained in:
parent
eb1b942b88
commit
14af972336
50
app.js
50
app.js
|
@ -2,21 +2,45 @@ var storage = require('pkgcloud').storage;
|
||||||
|
|
||||||
var client = storage.createClient({
|
var client = storage.createClient({
|
||||||
provider: 'rackspace',
|
provider: 'rackspace',
|
||||||
username: 'your-user-name',
|
username: 'strongloop',
|
||||||
apiKey: 'your-api-key'
|
apiKey: 'your-rackspace-api-key'
|
||||||
});
|
});
|
||||||
|
|
||||||
// Container
|
// Container
|
||||||
|
|
||||||
client.getContainers(function (err, containers) { });
|
client.getContainers(function (err, containers) {
|
||||||
client.createContainer(options, function (err, container) { });
|
containers.forEach(function(c) {
|
||||||
client.destroyContainer(containerName, function (err) { });
|
console.log(c.name);
|
||||||
client.getContainer(containerName, function (err, container) { });
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// File
|
/*
|
||||||
|
client.createContainer(options, function (err, container) { });
|
||||||
|
client.destroyContainer(containerName, function (err) { });
|
||||||
|
client.getContainer(containerName, function (err, container) { });
|
||||||
|
|
||||||
client.upload(options, function (err) { });
|
// File
|
||||||
client.download(options, function (err) { });
|
|
||||||
client.getFiles(container, function (err, files) { });
|
client.upload(options, function (err) { });
|
||||||
client.getFile(container, file, function (err, server) { });
|
client.download(options, function (err) { });
|
||||||
client.removeFile(container, file, function (err) { });
|
client.getFiles(container, function (err, files) { });
|
||||||
|
client.getFile(container, file, function (err, server) { });
|
||||||
|
client.removeFile(container, file, function (err) { });
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
var s3 = storage.createClient({
|
||||||
|
provider: 'amazon',
|
||||||
|
key: 'your-amazon-key',
|
||||||
|
keyId: 'your-amazon-key-id'
|
||||||
|
});
|
||||||
|
|
||||||
|
s3.getContainers(function (err, containers) {
|
||||||
|
if(err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
containers.forEach(function(c) {
|
||||||
|
console.log(c.name);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
exports.createClient = function (options) {
|
exports.createClient = function (options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var provider = options.provider || 'file';
|
var provider = options.provider || 'filesystem';
|
||||||
|
|
||||||
if ('function' !== typeof provider) {
|
if ('function' !== typeof provider) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
var base = require('pkgcloud').storage;
|
||||||
|
var util = require('util');
|
||||||
|
|
||||||
|
var Container = exports.Container = function Container(client, details) {
|
||||||
|
base.Container.call(this, client, details);
|
||||||
|
};
|
||||||
|
|
||||||
|
util.inherits(Container, base.Container);
|
||||||
|
|
||||||
|
Container.prototype._setProperties = function(details) {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
var base = require('pkgcloud').storage;
|
||||||
|
var util = require('util');
|
||||||
|
|
||||||
|
var File = exports.File = function File(client, details) {
|
||||||
|
base.File.call(this, client, details);
|
||||||
|
};
|
||||||
|
|
||||||
|
util.inherits(File, base.File);
|
||||||
|
|
||||||
|
File.prototype._setProperties = function(details) {
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
async = require('async');
|
async = require('async'),
|
||||||
|
File = require('./file').File,
|
||||||
|
Container = require('./container').Container;
|
||||||
|
|
||||||
module.exports = FileSystemProvider;
|
module.exports = FileSystemProvider;
|
||||||
|
|
||||||
|
@ -37,7 +39,7 @@ function validateName(name, cb) {
|
||||||
FileSystemProvider.prototype.getContainers = function (cb) {
|
FileSystemProvider.prototype.getContainers = function (cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
fs.readdir(self.root, function (err, files) {
|
fs.readdir(self.root, function (err, files) {
|
||||||
var dirs = [];
|
var containers = [];
|
||||||
var tasks = [];
|
var tasks = [];
|
||||||
files.forEach(function (f) {
|
files.forEach(function (f) {
|
||||||
tasks.push(fs.stat.bind(null, path.join(self.root, f)));
|
tasks.push(fs.stat.bind(null, path.join(self.root, f)));
|
||||||
|
@ -48,23 +50,31 @@ FileSystemProvider.prototype.getContainers = function (cb) {
|
||||||
} else {
|
} else {
|
||||||
stats.forEach(function (stat, index) {
|
stats.forEach(function (stat, index) {
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
dirs.push(files[index]);
|
var name = files[index];
|
||||||
|
var props = {name: name};
|
||||||
|
for (var p in stat) {
|
||||||
|
props[p] = stat[p];
|
||||||
|
}
|
||||||
|
var container = new Container(this, props);
|
||||||
|
containers.push(container);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cb && cb(err, dirs);
|
cb && cb(err, containers);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.createContainer = function (options, cb) {
|
FileSystemProvider.prototype.createContainer = function (options, cb) {
|
||||||
|
var self = this;
|
||||||
var name = options.name;
|
var name = options.name;
|
||||||
var mode = options.mode || 0777;
|
validateName(name, cb) && fs.mkdir(path.join(this.root, name), options, function (err) {
|
||||||
validateName(name, cb) && fs.mkdir(path.join(this.root, name), options, cb);
|
cb && cb(err, new Container(self, {name: name}));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
|
FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
|
||||||
if(!validateName(containerName, cb)) return;
|
if (!validateName(containerName, cb)) return;
|
||||||
|
|
||||||
var dir = path.join(this.root, containerName);
|
var dir = path.join(this.root, containerName);
|
||||||
fs.readdir(dir, function (err, files) {
|
fs.readdir(dir, function (err, files) {
|
||||||
|
@ -83,18 +93,29 @@ FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.getContainer = function (containerName, cb) {
|
FileSystemProvider.prototype.getContainer = function (containerName, cb) {
|
||||||
if(!validateName(containerName, cb)) return;
|
var self = this;
|
||||||
|
if (!validateName(containerName, cb)) return;
|
||||||
var dir = path.join(this.root, containerName);
|
var dir = path.join(this.root, containerName);
|
||||||
fs.stat(dir, cb);
|
fs.stat(dir, function (err, stat) {
|
||||||
|
var container = null;
|
||||||
|
if (!err) {
|
||||||
|
var props = {name: containerName};
|
||||||
|
for (var p in stat) {
|
||||||
|
props[p] = stat[p];
|
||||||
|
}
|
||||||
|
container = new Container(self, props);
|
||||||
|
}
|
||||||
|
cb && cb(err, container);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// File related functions
|
// File related functions
|
||||||
FileSystemProvider.prototype.upload = function (options, cb) {
|
FileSystemProvider.prototype.upload = function (options, cb) {
|
||||||
var container = options.container;
|
var container = options.container;
|
||||||
if(!validateName(container, cb)) return;
|
if (!validateName(container, cb)) return;
|
||||||
var file = options.remote;
|
var file = options.remote;
|
||||||
if(!validateName(file, cb)) return;
|
if (!validateName(file, cb)) return;
|
||||||
var filePath = path.join(this.root, container, file);
|
var filePath = path.join(this.root, container, file);
|
||||||
|
|
||||||
var fileOpts = {flags: 'w+',
|
var fileOpts = {flags: 'w+',
|
||||||
|
@ -106,9 +127,9 @@ FileSystemProvider.prototype.upload = function (options, cb) {
|
||||||
|
|
||||||
FileSystemProvider.prototype.download = function (options, cb) {
|
FileSystemProvider.prototype.download = function (options, cb) {
|
||||||
var container = options.container;
|
var container = options.container;
|
||||||
if(!validateName(container, cb)) return;
|
if (!validateName(container, cb)) return;
|
||||||
var file = options.remote;
|
var file = options.remote;
|
||||||
if(!validateName(file, cb)) return;
|
if (!validateName(file, cb)) return;
|
||||||
|
|
||||||
var filePath = path.join(this.root, container, file);
|
var filePath = path.join(this.root, container, file);
|
||||||
|
|
||||||
|
@ -120,12 +141,13 @@ FileSystemProvider.prototype.download = function (options, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.getFiles = function (container, cb) {
|
FileSystemProvider.prototype.getFiles = function (container, cb) {
|
||||||
if(!validateName(container, cb)) return;
|
var self = this;
|
||||||
|
if (!validateName(container, cb)) return;
|
||||||
var dir = path.join(this.root, container);
|
var dir = path.join(this.root, container);
|
||||||
fs.readdir(dir, function (err, files) {
|
fs.readdir(dir, function (err, entries) {
|
||||||
var dirs = [];
|
var files = [];
|
||||||
var tasks = [];
|
var tasks = [];
|
||||||
files.forEach(function (f) {
|
entries.forEach(function (f) {
|
||||||
tasks.push(fs.stat.bind(null, path.join(dir, f)));
|
tasks.push(fs.stat.bind(null, path.join(dir, f)));
|
||||||
});
|
});
|
||||||
async.parallel(tasks, function (err, stats) {
|
async.parallel(tasks, function (err, stats) {
|
||||||
|
@ -134,10 +156,15 @@ FileSystemProvider.prototype.getFiles = function (container, cb) {
|
||||||
} else {
|
} else {
|
||||||
stats.forEach(function (stat, index) {
|
stats.forEach(function (stat, index) {
|
||||||
if (stat.isFile()) {
|
if (stat.isFile()) {
|
||||||
dirs.push(files[index]);
|
var props = {container: container, name: entries[index]};
|
||||||
|
for (var p in stat) {
|
||||||
|
props[p] = stat[p];
|
||||||
|
}
|
||||||
|
var file = new File(self, props);
|
||||||
|
files.push(file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cb && cb(err, dirs);
|
cb && cb(err, files);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -145,15 +172,26 @@ FileSystemProvider.prototype.getFiles = function (container, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.getFile = function (container, file, cb) {
|
FileSystemProvider.prototype.getFile = function (container, file, cb) {
|
||||||
if(!validateName(container, cb)) return;
|
var self = this;
|
||||||
if(!validateName(file, cb)) return;
|
if (!validateName(container, cb)) return;
|
||||||
|
if (!validateName(file, cb)) return;
|
||||||
var filePath = path.join(this.root, container, file);
|
var filePath = path.join(this.root, container, file);
|
||||||
fs.stat(filePath, cb);
|
fs.stat(filePath, function (err, stat) {
|
||||||
|
var file = null;
|
||||||
|
if (!err) {
|
||||||
|
var props = {container: container, name: file};
|
||||||
|
for (var p in stat) {
|
||||||
|
props[p] = stat[p];
|
||||||
|
}
|
||||||
|
file = new File(self, props);
|
||||||
|
}
|
||||||
|
cb && cb(err, file);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemProvider.prototype.removeFile = function (container, file, cb) {
|
FileSystemProvider.prototype.removeFile = function (container, file, cb) {
|
||||||
if(!validateName(container, cb)) return;
|
if (!validateName(container, cb)) return;
|
||||||
if(!validateName(file, cb)) return;
|
if (!validateName(file, cb)) return;
|
||||||
|
|
||||||
var filePath = path.join(this.root, container, file);
|
var filePath = path.join(this.root, container, file);
|
||||||
fs.unlink(filePath, cb);
|
fs.unlink(filePath, cb);
|
|
@ -1,4 +1,4 @@
|
||||||
var FileSystemProvider = require('../lib/providers/file.js');
|
var FileSystemProvider = require('../lib/providers/filesystem/index.js');
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
Loading…
Reference in New Issue