kopia lustrzana https://github.com/c9/core
add debugging code for readFile returning metadata c9/core+71
rodzic
ce6eb41304
commit
e775e0c03e
|
@ -14,38 +14,49 @@ return function(_request) {
|
||||||
}, callback);
|
}, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFile(path, encoding, callback, progress, metadata) {
|
function _readFile(path, encoding, callback, progress, metadata) {
|
||||||
if (typeof encoding == "function") {
|
if (typeof encoding == "function") {
|
||||||
progress = callback;
|
progress = callback;
|
||||||
callback = encoding;
|
callback = encoding;
|
||||||
encoding = null;
|
encoding = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path == "/") {
|
|
||||||
var err = new Error("Cannot read root as file");
|
|
||||||
err.code = "EISDIR";
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the path doesn't have a trailing /
|
// Make sure the path doesn't have a trailing /
|
||||||
// It would then be interpreted as a directory
|
// It would then be interpreted as a directory
|
||||||
if (path.substr(-1) == "/")
|
if (path.substr(-1) == "/")
|
||||||
path = path.substr(0, path.length - 1);
|
path = path.substr(0, path.length - 1);
|
||||||
|
|
||||||
|
if (!path) {
|
||||||
|
var err = new Error("Cannot read root as file");
|
||||||
|
err.code = "EISDIR";
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
var headers = metadata ? { "x-request-metadata" : "true" } : null;
|
var headers = metadata ? { "x-request-metadata" : "true" } : null;
|
||||||
return request("GET", path, "", callback, progress, false, headers);
|
return request("GET", path, "", function(err, data, res) {
|
||||||
|
if (err)
|
||||||
|
return metadata ? callback(err, null, null, res) : callback(err, null, res);
|
||||||
|
var hasMetadata = res.headers["x-metadata-length"] != undefined;
|
||||||
|
if (metadata) {
|
||||||
|
var ml = parseInt(res.headers["x-metadata-length"], 10) || 0;
|
||||||
|
var ln = data.length;
|
||||||
|
callback(err, data.substr(0, ln - ml), ml && data.substr(-1 * ml) || "", res);
|
||||||
|
} else {
|
||||||
|
if (hasMetadata)
|
||||||
|
throw new Error("Unexpected metadata");
|
||||||
|
callback(err, data, res);
|
||||||
|
}
|
||||||
|
}, progress, false, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
function readFile(path, encoding, callback, progress, _metadata) {
|
||||||
|
// TODO remove this debugging code once we find why metadata was shown as file contents
|
||||||
|
if (_metadata != undefined)
|
||||||
|
throw new Error("attempt to call readfile with metadata");
|
||||||
|
return _readFile(path, encoding, callback, progress, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFileWithMetadata(path, encoding, callback, progress) {
|
function readFileWithMetadata(path, encoding, callback, progress) {
|
||||||
return readFile(path, encoding, function(err, data, res) {
|
return _readFile(path, encoding, callback, progress, true);
|
||||||
if (err) return callback(err, null, null, res);
|
|
||||||
|
|
||||||
// var cl = parseInt(res.headers["x-content-length"], 10) || data.length;
|
|
||||||
var ml = parseInt(res.headers["x-metadata-length"], 10) || 0;
|
|
||||||
var ln = data.length;
|
|
||||||
|
|
||||||
callback(err, data.substr(0, ln - ml), ml && data.substr(-1 * ml) || "", res);
|
|
||||||
}, progress, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeFile(path, data, sync, callback, progress) {
|
function writeFile(path, data, sync, callback, progress) {
|
||||||
|
|
Ładowanie…
Reference in New Issue