cloudcmd/lib/server/commander.js

228 lines
No EOL
6.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function() {
'use strict';
if (!global.cloudcmd)
return console.log(
'# commander.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# used for getting dir content.' + '\n' +
'# and forming html content' + '\n' +
'# http://cloudcmd.io' + '\n');
var main = global.cloudcmd.main,
fs = main.fs,
Util = main.util,
users = main.users,
WIN32 = main.WIN32,
checkParams = main.checkCallBackParams;
exports.getDirContent = function(path, callback) {
var ret = Util.isString(path);
if (!ret)
Util.exec(callback, "First parameter should be a string");
else
fs.readdir(path, readDir.bind(null, {
callback : callback,
path : path
}));
return ret;
};
/**
* Функция читает ссылку или выводит информацию об ошибке
* @param pError
* @param pFiles
*/
function readDir(params, error, files) {
var i, n, stats, filesData, fill, name, fileParams,
p = params,
dirPath = getDirPath(p.path);
if (error)
Util.exec(p.callback, error.toString());
else {
/* Получаем информацию о файлах */
n = files.length,
stats = {},
filesData = {
files : files,
stats : stats,
callback : p.callback,
path : p.path
},
fill = fillJSON.bind(null, filesData);
if (n)
for (i = 0; i < n; i++) {
name = dirPath + files[i],
fileParams = {
callback : fill,
count : n,
name : files[i],
stats : stats,
};
fs.stat(name, onStat.bind(null, fileParams));
}
else
fillJSON(filesData);
}
}
/**
* async getting file states
* and putting it to stats object
*/
function onStat(params, error, stat) {
var n, keys, p = params;
if (!error)
p.stats[p.name] = stat;
else
p.stats[p.name] = {
'mode' : 0,
'size' : 0,
'isDirectory' : Util.retFalse
};
keys = Object.keys(p.stats);
n = keys.length;
if (p.count === n)
Util.exec(p.callback);
}
/**
* Function fill JSON by file stats
*
* @param stats - object, contain file stats.
* example {'1.txt': stat}
*
* @param files - array of files of current directory
*/
function fillJSON(pParams) {
var name, stat, mode, isDir, size, owner, modeStr,
p, i, n, file, path, json, files,
ret = Util.checkObjTrue(pParams, ['files', 'stats', 'path']);
if (ret) {
p = pParams;
n = p.files.length;
/* данные о файлах в формате JSON*/
file = {};
path = getDirPath(p.path);
json = {
path : path,
files : []
},
files = json.files;
for (i = 0; i < n; i++ ) {
name = p.files[i];
stat = p.stats[name];
owner = stat.uid;
if (stat) {
/* Переводим права доступа в 8-ричную систему */
modeStr = Number(stat.mode).toString(8);
mode = Number(modeStr);
isDir = stat.isDirectory();
size = isDir ? 'dir' : stat.size;
}
file = {
'name' : name,
'size' : size,
'owner' : owner,
'mode' : mode
};
files.push(file);
}
json.files = changeOrder(files);
changeUIDToName(json, function() {
Util.exec(p.callback, null, json);
});
}
}
function changeUIDToName(json, callback) {
Util.ifExec(WIN32, callback,
function(callback) {
users.getNames(function(error, names) {
var i, n, current, owner,
files = json.files;
Util.log(error);
n = files.length;
for (i = 0; i < n; i++) {
current = files[i];
owner = current.owner;
owner = names[owner];
if (owner)
current.owner = owner;
}
Util.exec(callback);
});
});
}
function changeOrder(json) {
var file, i, n,
files = [],
dirs = [],
current = [],
sorted = [];
n = json.length;
for (i = 0; i < n; i++) {
current = json[i];
if (current.size === "dir")
dirs.push(current);
else
files.push(current);
}
n = dirs.length;
for (i = 0; i < n; i++) {
current = dirs[i];
sorted.push(current);
}
n = files.length;
for (i = 0; i < n; i++) {
current = files[i];
sorted.push(current);
}
return sorted;
}
function getDirPath(pPath) {
var lRet = pPath;
if (lRet !== '/')
lRet += '/';
return lRet;
}
})();