mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(format) mv to lib
This commit is contained in:
parent
b3d024f0da
commit
a30d187ec2
5 changed files with 171 additions and 5 deletions
|
|
@ -42,6 +42,7 @@
|
|||
client = 'client/',
|
||||
files = [
|
||||
'util',
|
||||
'format',
|
||||
'promise',
|
||||
'cloudfunc',
|
||||
client + 'dom',
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuIO;
|
||||
var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuIO, Format;
|
||||
(function(CloudCmd, Util, DOM, CloudFunc) {
|
||||
'use strict';
|
||||
|
||||
|
|
@ -450,6 +450,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
|
|||
|
||||
function onSave(text) {
|
||||
var ret,
|
||||
size = Format.size(Value.length),
|
||||
isError = Util.isContainStrAtBegin(text, 'error'),
|
||||
path = Info.path,
|
||||
msg = '\nShould I save file anyway?';
|
||||
|
|
@ -457,7 +458,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Zip, MenuI
|
|||
if (!isError) {
|
||||
Edit.showMessage(text);
|
||||
DOM.saveDataToStorage(path, Value);
|
||||
DOM.setCurrentSize(Value.length);
|
||||
DOM.setCurrentSize(size);
|
||||
} else {
|
||||
ret = Dialog.confirm(text + msg);
|
||||
|
||||
|
|
|
|||
164
lib/format.js
Normal file
164
lib/format.js
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
(function(scope) {
|
||||
'use strict';
|
||||
|
||||
var Scope = scope.window ? window : global;
|
||||
|
||||
if (typeof module === 'object' && module.exports)
|
||||
module.exports = new FormatProto();
|
||||
else
|
||||
Scope.Format = new FormatProto();
|
||||
|
||||
function FormatProto() {
|
||||
this.addSlashToEnd = function(path) {
|
||||
var length, isSlash;
|
||||
|
||||
if (path) {
|
||||
length = path.length - 1;
|
||||
isSlash = path[length] === '/';
|
||||
|
||||
if (!isSlash)
|
||||
path += '/';
|
||||
}
|
||||
|
||||
return path;
|
||||
};
|
||||
|
||||
/** Функция получает короткие размеры
|
||||
* конвертируя байт в килобайты, мегабойты,
|
||||
* гигайбайты и терабайты
|
||||
* @pSize - размер в байтах
|
||||
*/
|
||||
this.size = function(size) {
|
||||
var isNumber = typeof size === 'number',
|
||||
l1KB = 1024,
|
||||
l1MB = l1KB * l1KB,
|
||||
l1GB = l1MB * l1KB,
|
||||
l1TB = l1GB * l1KB,
|
||||
l1PB = l1TB * l1KB;
|
||||
|
||||
if (isNumber) {
|
||||
if (size < l1KB) size = size + 'b';
|
||||
else if (size < l1MB) size = (size/l1KB).toFixed(2) + 'kb';
|
||||
else if (size < l1GB) size = (size/l1MB).toFixed(2) + 'mb';
|
||||
else if (size < l1TB) size = (size/l1GB).toFixed(2) + 'gb';
|
||||
else if (size < l1PB) size = (size/l1TB).toFixed(2) + 'tb';
|
||||
else size = (size/l1PB).toFixed(2) + 'pb';
|
||||
}
|
||||
|
||||
return size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Функция переводит права из цыфрового вида в символьный
|
||||
* @param perms - строка с правами доступа
|
||||
* к файлу в 8-миричной системе
|
||||
*/
|
||||
this.permissions = {
|
||||
symbolic: function(perms) {
|
||||
var type, owner, group, all,
|
||||
permsStr = '',
|
||||
permissions = '';
|
||||
/*
|
||||
S_IRUSR 0000400 protection: readable by owner
|
||||
S_IWUSR 0000200 writable by owner
|
||||
S_IXUSR 0000100 executable by owner
|
||||
S_IRGRP 0000040 readable by group
|
||||
S_IWGRP 0000020 writable by group
|
||||
S_IXGRP 0000010 executable by group
|
||||
S_IROTH 0000004 readable by all
|
||||
S_IWOTH 0000002 writable by all
|
||||
S_IXOTH 0000001 executable by all
|
||||
*/
|
||||
|
||||
if (perms) {
|
||||
permsStr = perms.toString();
|
||||
/* тип файла */
|
||||
type = permsStr.charAt(0);
|
||||
|
||||
switch (type - 0) {
|
||||
case 1: /* обычный файл */
|
||||
type='-';
|
||||
break;
|
||||
case 2: /* байт-ориентированное (символьное) устройство*/
|
||||
type='c';
|
||||
break;
|
||||
case 4: /* каталог */
|
||||
type='d';
|
||||
break;
|
||||
default:
|
||||
type='-';
|
||||
}
|
||||
|
||||
/* оставляем последние 3 символа*/
|
||||
if (permsStr.length > 5)
|
||||
permsStr = permsStr.substr(3);
|
||||
else
|
||||
permsStr = permsStr.substr(2);
|
||||
|
||||
/* Рекомендации гугла советуют вместо string[3]
|
||||
* использовать string.charAt(3)
|
||||
*/
|
||||
/*
|
||||
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Standards_features#Standards_features
|
||||
|
||||
Always preferred over non-standards featuresFor
|
||||
maximum portability and compatibility, always
|
||||
prefer standards features over non-standards
|
||||
features (e.g., string.charAt(3) over string[3]
|
||||
and element access with DOM functions instead
|
||||
of using an application-specific shorthand).
|
||||
*/
|
||||
/* Переводим в двоичную систему */
|
||||
owner = (permsStr[0] - 0).toString(2),
|
||||
group = (permsStr[1] - 0).toString(2),
|
||||
all = (permsStr[2] - 0).toString(2),
|
||||
|
||||
/* переводим в символьную систему*/
|
||||
permissions =
|
||||
(owner[0] - 0 > 0 ? 'r' : '-') +
|
||||
(owner[1] - 0 > 0 ? 'w' : '-') +
|
||||
(owner[2] - 0 > 0 ? 'x' : '-') +
|
||||
' ' +
|
||||
(group[0] - 0 > 0 ? 'r' : '-') +
|
||||
(group[1] - 0 > 0 ? 'w' : '-') +
|
||||
(group[2] - 0 > 0 ? 'x' : '-') +
|
||||
' ' +
|
||||
(all[0]- 0 > 0 ? 'r' : '-') +
|
||||
(all[1]- 0 > 0 ? 'w' : '-') +
|
||||
(all[2]- 0 > 0 ? 'x' : '-');
|
||||
}
|
||||
|
||||
return permissions;
|
||||
},
|
||||
|
||||
/**
|
||||
* Функция конвертирует права доступа к файлам из символьного вида
|
||||
* в цыфровой
|
||||
*/
|
||||
numeric: function(perms) {
|
||||
var owner, group, all,
|
||||
length = perms && perms.length === 11;
|
||||
|
||||
if (length) {
|
||||
owner = (perms[0] === 'r' ? 4 : 0) +
|
||||
(perms[1] === 'w' ? 2 : 0) +
|
||||
(perms[2] === 'x' ? 1 : 0),
|
||||
|
||||
group = (perms[4] === 'r' ? 4 : 0) +
|
||||
(perms[5] === 'w' ? 2 : 0) +
|
||||
(perms[6] === 'x' ? 1 : 0),
|
||||
|
||||
all = (perms[8] === 'r' ? 4 : 0) +
|
||||
(perms[9] === 'w' ? 2 : 0) +
|
||||
(perms[10] === 'x' ? 1 : 0);
|
||||
|
||||
/* добавляем 2 цифры до 5 */
|
||||
perms = '00' + owner + group + all;
|
||||
}
|
||||
|
||||
return perms;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
})(this);
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
DIR_SERVER = DIR + 'server/',
|
||||
|
||||
Util = require(DIR + 'util'),
|
||||
format = require(DIR_SERVER + 'format'),
|
||||
format = require(DIR + 'format'),
|
||||
users = require(DIR_SERVER + 'users'),
|
||||
|
||||
WIN32 = process.platform === 'win32';
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@
|
|||
DIR = '../',
|
||||
DIR_SERVER = DIR + 'server/',
|
||||
|
||||
format = require(DIR_SERVER + 'format'),
|
||||
Util = require(DIR + 'util'),
|
||||
format = require(DIR + 'format'),
|
||||
Util = require(DIR + 'util'),
|
||||
|
||||
/* The lstat() function shall be equivalent to stat(),
|
||||
except when path refers to a symbolic link. In that case lstat()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue