weedfs
Version:
A simple HTTP REST client for the weed-fs file database
210 lines (193 loc) • 6.58 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var WeedFS, fs, qs, _;
qs = require('querystring');
fs = require('fs');
_ = require('lodash');
WeedFS = (function() {
function WeedFS(config) {
var _ref;
this.config = config;
_.defaults(this.config, {
host: 'localhost',
port: 9333,
scheme: 'http'
});
this.client = (_ref = this.config.client) != null ? _ref : require('request');
this.address = "" + this.config.scheme + "://" + this.config.host + ":" + config.port;
return;
}
WeedFS.prototype._parse = function(callback) {
return function(err, response, body) {
if (err) {
return callback(err);
} else if (/application\/json/.test(response.headers['content-type'])) {
return callback(null, JSON.parse(body));
} else {
try {
return callback(null, JSON.parse(body));
} catch (_error) {
err = _error;
return callback(new Error("Unexpected content-type '" + response.headers['content-type'] + "' in response"));
}
}
};
};
WeedFS.prototype._assign = function(options, callback) {
this.client("" + this.address + "/dir/assign?" + (qs.stringify(options)), this._parse(callback));
};
WeedFS.prototype._write = function(file_url, buffer, callback) {
var form, req;
req = this.client.post(file_url, this._parse(function(err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
}));
form = req.form();
form.append("file", buffer);
};
WeedFS.prototype.clusterStatus = function(callback) {
this.client("" + this.address + "/dir/status", this._parse(callback));
};
WeedFS.prototype.volumeStatus = function() {};
WeedFS.prototype.find = function(file_id, callback) {
var volume;
volume = file_id.split(',')[0];
this.client("" + this.address + "/dir/lookup?volumeId=" + volume, this._parse((function(_this) {
return function(err, result) {
var location, locations, _i, _len, _ref;
if (err) {
return callback(err);
}
locations = [];
if (result.locations != null) {
_ref = result.locations;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
location = _ref[_i];
locations.push("" + _this.config.scheme + "://" + location.publicUrl + "/" + file_id);
}
}
return callback(null, locations);
};
})(this)));
};
WeedFS.prototype.read = function(file_id, stream, callback) {
if (_.isFunction(stream)) {
callback = stream;
stream = null;
}
this.find(file_id, (function(_this) {
return function(err, locations) {
if (err) {
return callback(new Error("file '" + file_id + "' not found"));
}
if (locations.length > 0) {
if (stream != null) {
return _this.client(locations[0]).pipe(stream);
} else {
return _this.client({
method: 'GET',
encoding: null,
url: locations[0]
}, function(err, response, body) {
if (response.statusCode === 404) {
return callback(new Error("file '" + file_id + "' not found"));
} else {
return callback(err, response, body);
}
});
}
} else {
return callback(new Error("file '" + file_id + "' not found"));
}
};
})(this));
};
WeedFS.prototype.write = function(files, callback) {
if (!_.isArray(files)) {
files = [files];
}
this._assign({
count: files.length
}, (function(_this) {
return function(err, file_info) {
var file, index, is_error, results, _callback, _results;
if (err) {
return callback(err);
}
is_error = false;
results = [];
_callback = function(err, result) {
if (err) {
is_error = true;
results.push(err);
} else {
results.push(result);
}
if (results.length === files.length) {
if (is_error) {
return callback(new Error("An error occured while upload files"), file_info, results);
} else {
return callback(null, file_info, results);
}
}
};
_results = [];
for (index in files) {
file = files[index];
file = file instanceof Buffer ? file : fs.createReadStream(file);
if (parseInt(index) === 0) {
_results.push(_this._write("" + _this.config.scheme + "://" + file_info.publicUrl + "/" + file_info.fid, file, _callback));
} else {
_results.push(_this._write("" + _this.config.scheme + "://" + file_info.publicUrl + "/" + file_info.fid + "_" + index, file, _callback));
}
}
return _results;
};
})(this));
};
WeedFS.prototype.remove = function(file_id, callback) {
this.find(file_id, (function(_this) {
return function(err, locations) {
var is_error, location, results, _i, _len, _results;
if (err) {
return callback(new Error("file '" + file_id + "' not found"));
}
if (locations.length > 0) {
is_error = false;
results = [];
_results = [];
for (_i = 0, _len = locations.length; _i < _len; _i++) {
location = locations[_i];
_results.push(_this.client.del(location, function(err, response, body) {
if (response.statusCode === 404) {
is_error = true;
results.push(new Error("file '" + file_id + "' not found"));
} else {
_this._parse(function(err, result) {
if (err) {
return results.push(err);
} else {
return results.push(result);
}
})(err, response, body);
}
if (results.length === locations.length) {
if (is_error) {
return callback(new Error("An error occured while removing files"), results);
} else {
return callback(null, results);
}
}
}));
}
return _results;
} else {
return callback(new Error("file '" + file_id + "' not found"));
}
};
})(this));
};
return WeedFS;
})();
module.exports = WeedFS;