Add Container/File classes

This commit is contained in:
Raymond Feng 2013-06-25 09:29:53 -07:00
parent eb1b942b88
commit 14af972336
6 changed files with 125 additions and 39 deletions

50
app.js
View File

@ -2,21 +2,45 @@ var storage = require('pkgcloud').storage;
var client = storage.createClient({
provider: 'rackspace',
username: 'your-user-name',
apiKey: 'your-api-key'
});
username: 'strongloop',
apiKey: 'your-rackspace-api-key'
});
// Container
client.getContainers(function (err, containers) { });
client.createContainer(options, function (err, container) { });
client.destroyContainer(containerName, function (err) { });
client.getContainer(containerName, function (err, container) { });
client.getContainers(function (err, containers) {
containers.forEach(function(c) {
console.log(c.name);
});
});
// File
/*
client.createContainer(options, function (err, container) { });
client.destroyContainer(containerName, function (err) { });
client.getContainer(containerName, function (err, container) { });
client.upload(options, function (err) { });
client.download(options, function (err) { });
client.getFiles(container, function (err, files) { });
client.getFile(container, file, function (err, server) { });
client.removeFile(container, file, function (err) { });
// File
client.upload(options, function (err) { });
client.download(options, 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);
});
});

View File

@ -1,6 +1,6 @@
exports.createClient = function (options) {
options = options || {};
var provider = options.provider || 'file';
var provider = options.provider || 'filesystem';
if ('function' !== typeof provider) {
try {

View File

@ -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) {
}

View File

@ -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) {
}

View File

@ -4,7 +4,9 @@
var fs = require('fs'),
path = require('path'),
async = require('async');
async = require('async'),
File = require('./file').File,
Container = require('./container').Container;
module.exports = FileSystemProvider;
@ -37,7 +39,7 @@ function validateName(name, cb) {
FileSystemProvider.prototype.getContainers = function (cb) {
var self = this;
fs.readdir(self.root, function (err, files) {
var dirs = [];
var containers = [];
var tasks = [];
files.forEach(function (f) {
tasks.push(fs.stat.bind(null, path.join(self.root, f)));
@ -48,23 +50,31 @@ FileSystemProvider.prototype.getContainers = function (cb) {
} else {
stats.forEach(function (stat, index) {
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) {
var self = this;
var name = options.name;
var mode = options.mode || 0777;
validateName(name, cb) && fs.mkdir(path.join(this.root, name), options, cb);
validateName(name, cb) && fs.mkdir(path.join(this.root, name), options, function (err) {
cb && cb(err, new Container(self, {name: name}));
});
}
FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
if(!validateName(containerName, cb)) return;
if (!validateName(containerName, cb)) return;
var dir = path.join(this.root, containerName);
fs.readdir(dir, function (err, files) {
@ -83,18 +93,29 @@ FileSystemProvider.prototype.destroyContainer = 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);
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
FileSystemProvider.prototype.upload = function (options, cb) {
var container = options.container;
if(!validateName(container, cb)) return;
if (!validateName(container, cb)) return;
var file = options.remote;
if(!validateName(file, cb)) return;
if (!validateName(file, cb)) return;
var filePath = path.join(this.root, container, file);
var fileOpts = {flags: 'w+',
@ -106,9 +127,9 @@ FileSystemProvider.prototype.upload = function (options, cb) {
FileSystemProvider.prototype.download = function (options, cb) {
var container = options.container;
if(!validateName(container, cb)) return;
if (!validateName(container, cb)) return;
var file = options.remote;
if(!validateName(file, cb)) return;
if (!validateName(file, cb)) return;
var filePath = path.join(this.root, container, file);
@ -120,12 +141,13 @@ FileSystemProvider.prototype.download = function (options, 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);
fs.readdir(dir, function (err, files) {
var dirs = [];
fs.readdir(dir, function (err, entries) {
var files = [];
var tasks = [];
files.forEach(function (f) {
entries.forEach(function (f) {
tasks.push(fs.stat.bind(null, path.join(dir, f)));
});
async.parallel(tasks, function (err, stats) {
@ -134,10 +156,15 @@ FileSystemProvider.prototype.getFiles = function (container, cb) {
} else {
stats.forEach(function (stat, index) {
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) {
if(!validateName(container, cb)) return;
if(!validateName(file, cb)) return;
var self = this;
if (!validateName(container, cb)) return;
if (!validateName(file, cb)) return;
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) {
if(!validateName(container, cb)) return;
if(!validateName(file, cb)) return;
if (!validateName(container, cb)) return;
if (!validateName(file, cb)) return;
var filePath = path.join(this.root, container, file);
fs.unlink(filePath, cb);

View File

@ -1,4 +1,4 @@
var FileSystemProvider = require('../lib/providers/file.js');
var FileSystemProvider = require('../lib/providers/filesystem/index.js');
var assert = require('assert');
var path = require('path');