Commit 9a0fbbd7 authored by antoor's avatar antoor

Fix call core module

修复调用核心模块
parent b6092dde
......@@ -33,9 +33,7 @@ const antSword = window.antSword = {
};
// 加载模板代码
['php', 'asp', 'aspx', 'custom'].map((_) => {
antSword['core'][_] = require(`./core/${_}/index`);
});
antSword['core'] = require('./core/index');
// 加载显示语言
let _lang = localStorage.getItem('language') || navigator.language;
......
//
//
// 数据库驱动::ASP
// 支持数据库:access,sqlserver,mysql
//
//
class ASP {
......@@ -9,9 +9,9 @@ class ASP {
this.opt = opt;
this.core = this.opt.core;
this.manager = this.opt.super;
//
//
// * 数据库驱动列表
//
//
this.conns = {
'dsn': 'Dsn=DsnName;',
'mysql': 'Driver={MySQL};Server=localhost;database=mysql;UID=root;PWD=',
......@@ -254,11 +254,15 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_databases(
{
conn: conf['conn'],
dbname: ['access', 'microsoft_jet_oledb_4_0'].indexOf(conf['type']) > -1 ? conf['conn'].match(/[\w]+.mdb$/) : 'database'
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_databases(
{
conn: conf['conn'],
dbname: ['access', 'microsoft_jet_oledb_4_0'].indexOf(conf['type']) > -1 ? conf['conn'].match(/[\w]+.mdb$/) : 'database'
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
if (arr.length === 1 && ret === '') {
toastr.warning('执行完毕,没有结果返回')
......@@ -279,7 +283,7 @@ class ASP {
this.manager.list.imgs[1]);
});
this.manager.list.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error('获取数据库列表失败!' + err['status'] || JSON.stringify(err), 'ERROR');
this.manager.list.layout.progressOff();
});
......@@ -293,11 +297,15 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_tables(
{
conn: conf['conn'],
dbname: db
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_tables(
{
conn: conf['conn'],
dbname: db
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
// 删除子节点
......@@ -328,11 +336,15 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_columns(
{
conn: conf['conn'],
table: conf['type'] === 'oracle' ? `SELECT * FROM (SELECT A.*,ROWNUM N FROM ${table} A) WHERE N=1` : `SELECT TOP 1 * FROM ${table}`
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_columns(
{
conn: conf['conn'],
table: conf['type'] === 'oracle' ? `SELECT * FROM (SELECT A.*,ROWNUM N FROM ${table} A) WHERE N=1` : `SELECT TOP 1 * FROM ${table}`
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
const _table = new Buffer(table).toString('base64');
......@@ -363,14 +375,18 @@ class ASP {
// 执行SQL
execSQL(sql) {
this.manager.query.layout.progressOn();
this.core[`database_${this.dbconf['type']}`].query({
conn: this.dbconf['conn'],
sql: sql
}, (ret) => {
this.core.request(
this.core[`database_${this.dbconf['type']}`].query({
conn: this.dbconf['conn'],
sql: sql
})
).then((res) => {
let ret = res['text'];
// 更新执行结果
this.updateResult(ret);
this.manager.query.layout.progressOff();
}, (err) => {
}).catch((err) => {
console.error(err);
});
}
......@@ -455,4 +471,4 @@ class ASP {
}
module.exports = ASP;
\ No newline at end of file
module.exports = ASP;
......@@ -250,12 +250,14 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_databases(
{
conn: conf['conn'],
encode: this.manager.opt.encode,
db: ['access', 'microsoft_jet_oledb_4_0'].indexOf(conf['type']) > -1 ? conf['conn'].match(/[\w]+.mdb$/) : 'database'
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_databases({
conn: conf['conn'],
encode: this.manager.opt.encode,
db: ['access', 'microsoft_jet_oledb_4_0'].indexOf(conf['type']) > -1 ? conf['conn'].match(/[\w]+.mdb$/) : 'database'
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
if (arr.length === 1 && ret === '') {
toastr.warning('执行完毕,没有结果返回')
......@@ -276,7 +278,7 @@ class ASP {
this.manager.list.imgs[1]);
});
this.manager.list.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error('获取数据库列表失败!' + err['status'] || JSON.stringify(err), 'ERROR');
this.manager.list.layout.progressOff();
});
......@@ -290,12 +292,16 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_tables(
{
conn: conf['conn'],
encode: this.manager.opt.encode,
db: db
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_tables(
{
conn: conf['conn'],
encode: this.manager.opt.encode,
db: db
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
// 删除子节点
......@@ -326,13 +332,17 @@ class ASP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_columns(
{
conn: conf['conn'],
encode: this.manager.opt.encode,
db: db,
table: table
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_columns(
{
conn: conf['conn'],
encode: this.manager.opt.encode,
db: db,
table: table
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
const _table = new Buffer(table).toString('base64');
......@@ -363,15 +373,19 @@ class ASP {
// 执行SQL
execSQL(sql) {
this.manager.query.layout.progressOn();
this.core[`database_${this.dbconf['type']}`].query({
conn: this.dbconf['conn'],
encode: this.manager.opt.encode,
sql: sql
}, (ret) => {
this.core.request(
this.core[`database_${this.dbconf['type']}`].query({
conn: this.dbconf['conn'],
encode: this.manager.opt.encode,
sql: sql
})
).then((res) => {
let ret = res['text'];
// 更新执行结果
this.updateResult(ret);
this.manager.query.layout.progressOff();
}, (err) => {
}).catch((err) => {
console.error(err);
});
}
......
//
//
// 数据库驱动::PHP
// 支持数据库:mysql,mssql,oracle,informix
//
//
const LANG = antSword['language']['database'];
const LANG_T = antSword['language']['toastr'];
......@@ -271,12 +271,14 @@ class PHP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_databases(
{
host: conf['host'],
user: conf['user'],
passwd: conf['passwd']
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_databases({
host: conf['host'],
user: conf['user'],
passwd: conf['passwd']
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
if (arr.length === 1 && ret === '') {
toastr.warning(LANG['result']['warning'], LANG_T['warning']);
......@@ -297,7 +299,7 @@ class PHP {
this.manager.list.imgs[1]);
});
this.manager.list.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error(LANG['result']['error']['database'](err['status'] || JSON.stringify(err)), LANG_T['error']);
this.manager.list.layout.progressOff();
});
......@@ -311,13 +313,16 @@ class PHP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_tables(
{
host: conf['host'],
user: conf['user'],
passwd: conf['passwd'],
db: db
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_tables({
host: conf['host'],
user: conf['user'],
passwd: conf['passwd'],
db: db
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
// 删除子节点
......@@ -337,7 +342,7 @@ class PHP {
);
});
this.manager.list.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error(LANG['result']['error']['table'](err['status'] || JSON.stringify(err)), LANG_T['error']);
this.manager.list.layout.progressOff();
});
......@@ -351,14 +356,17 @@ class PHP {
_id: this.manager.opt['_id'],
id: id
});
this.core[`database_${conf['type']}`].show_columns(
{
host: conf['host'],
user: conf['user'],
passwd: conf['passwd'],
db: db,
table: table
}, (ret) => {
this.core.request(
this.core[`database_${conf['type']}`].show_columns({
host: conf['host'],
user: conf['user'],
passwd: conf['passwd'],
db: db,
table: table
})
).then((res) => {
let ret = res['text'];
const arr = ret.split('\t');
const _db = new Buffer(db).toString('base64');
const _table = new Buffer(table).toString('base64');
......@@ -380,7 +388,7 @@ class PHP {
// 更新编辑器SQL语句
this.manager.query.editor.session.setValue(`SELECT * FROM \`${table}\` ORDER BY 1 DESC LIMIT 0,20;`);
this.manager.list.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error(LANG['result']['error']['column'](err['status'] || JSON.stringify(err)), LANG_T['error']);
this.manager.list.layout.progressOff();
});
......@@ -389,18 +397,22 @@ class PHP {
// 执行SQL
execSQL(sql) {
this.manager.query.layout.progressOn();
this.core[`database_${this.dbconf['type']}`].query({
host: this.dbconf['host'],
user: this.dbconf['user'],
passwd: this.dbconf['passwd'],
db: this.dbconf['database'],
sql: sql,
encode: this.dbconf['encode'] || 'utf8'
}, (ret) => {
this.core.request(
this.core[`database_${this.dbconf['type']}`].query({
host: this.dbconf['host'],
user: this.dbconf['user'],
passwd: this.dbconf['passwd'],
db: this.dbconf['database'],
sql: sql,
encode: this.dbconf['encode'] || 'utf8'
})
).then((res) => {
let ret = res['text'];
// 更新执行结果
this.updateResult(ret);
this.manager.query.layout.progressOff();
}, (err) => {
}).catch((err) => {
toastr.error(LANG['result']['error']['query'](err['status'] || JSON.stringify(err)), LANG_T['error']);
this.manager.query.layout.progressOff();
});
......@@ -484,4 +496,4 @@ class PHP {
}
module.exports = PHP;
\ No newline at end of file
module.exports = PHP;
......@@ -194,6 +194,7 @@ class Files {
grid.setInitWidths("40,*,150,100,100");
grid.setColAlign("center,left,left,right,center");
grid.enableMultiselect(true);
grid.enableDragAndDrop(true);
// grid.enableMultiline(true);
// grid右键
......@@ -368,6 +369,23 @@ class Files {
// 剪贴板
this.Clipboard = {};
// 文件拖拽上传
$(this.cell.cell).on({
dragleave: (e) => { e.preventDefault() },
drop: (e) => {
e.preventDefault();
let filePaths = [];
let files = e.originalEvent['dataTransfer']['files'] || {};
for (let i = 0; i < files.length; i++) {
let f = files.item(i);
filePaths.push(f['path']);
}
this.manager.uploadFile(filePaths);
},
dragenter: (e) => { e.preventDefault() },
dragover: (e) => { e.preventDefault() }
});
}
// 刷新当前目录
......
......@@ -11,7 +11,6 @@ class Folder {
// 1.cell: 左侧layout.cell对象
constructor(cell, manager) {
cell.setWidth(250);
cell.setText(`<i class="fa fa-folder-o"></i> ${LANG['title']}`);
// 创建tree
let tree = cell.attachTree();
// tree事件
......@@ -23,6 +22,7 @@ class Folder {
this.cell = cell;
this.cache = {};
this.manager = manager;
this.setTitle(0);
}
......@@ -44,12 +44,15 @@ class Folder {
self.cache[curPath] = 0;
});
// 2. 解析当前子目录
let folderNum = 0;
files.map((f) => {
let _ = f['name'];
if (!_.endsWith('/') || ['./', '../'].indexOf(_) !== -1) {return};
self.cache[`${curPath}${_}`] = 0;
folderNum ++;
});
// 设置标题
this.setTitle(folderNum);
// 3. 解析缓存为树形菜单虚拟对象
// /var/www/html 根据/分割为数组,循环,相加,增加到虚拟缓存
......@@ -100,6 +103,14 @@ class Folder {
this.cell.progressOff();
}
/**
* 设置标题
* @param {Number} num 当前目录数
*/
setTitle(num) {
this.cell.setText(`<i class="fa fa-folder-o"></i> ${LANG['title']} (${num})`);
}
}
export default Folder;
//
// 文件管理模块
//
/**
* 文件管理模板
* 更新:2016/04/13
* 作者:蚁逅 <https://github.com/antoor>
*/
'use strict';
import Files from './files';
import Tasks from './tasks';
......@@ -25,13 +28,15 @@ class FileManager {
tabbar.addTab(
`tab_filemanager_${hash}`,
// `<i class="fa fa-folder-o"></i> ${LANG['title']} \/\/ ${opts['ip']}`,
`<i class="fa fa-folder-o"></i> ${opts['ip']}`,
null, null, true, true
);
// 创建框架
const cell = tabbar.cells(`tab_filemanager_${hash}`);
// 增加到全局变量方便调试
antSword['modules']['filemanager'] = antSword['modules']['filemanager'] || {};
antSword['modules']['filemanager'][hash] = this;
this.isWin = true;
this.path = '/';
......@@ -51,14 +56,24 @@ class FileManager {
this.initUI(cache_info);
}else{
this.cell.progressOn();
this.core.base.info((ret) => {
this.initUI(ret);
this.core.request(
this.core.base.info()
).then((ret) => {
this.initUI(ret['text']);
this.cell.progressOff();
}, (err) => {
}).catch((err) => {
this.cell.progressOff();
this.cell.close();
toastr.error((typeof(err) === 'object') ? JSON.stringify(err) : String(err), LANG_T['error']);
});
// this.core.base.info((ret) => {
// this.initUI(ret);
// this.cell.progressOff();
// }, (err) => {
// this.cell.progressOff();
// this.cell.close();
// toastr.error((typeof(err) === 'object') ? JSON.stringify(err) : String(err), LANG_T['error']);
// });
}
}
......@@ -131,9 +146,12 @@ class FileManager {
return callback(JSON.parse(cache));
};
this.core.filemanager.dir({
path: path
}, (ret) => {
this.core.request(
this.core.filemanager.dir({
path: path
})
).then((res) => {
let ret = res['text'];
// 判断是否出错
if (ret.startsWith('ERROR://')) {
callback([]);
......@@ -167,9 +185,49 @@ class FileManager {
// 增加缓存
// self.cache[path] = data;
this.cache.set(cache_tag, JSON.stringify(data));
}, (err) => {
}).catch((err) => {
toastr.error((err instanceof Object) ? JSON.stringify(err) : String(err), LANG_T['error']);
});
})
// this.core.filemanager.dir({
// path: path
// }, (ret) => {
// // 判断是否出错
// if (ret.startsWith('ERROR://')) {
// callback([]);
// return toastr.error(ret.substr(9), LANG_T['error']);
// };
// let tmp = ret.split('\n');
//
// tmp.sort();
//
// let folders = [];
// let files = [];
//
// tmp.map( (t) => {
// let _ = t.split('\t');
// let d = {
// name: _[0],
// time: _[1],
// size: _[2],
// attr: _[3]
// }
// if (_[0].endsWith('/')) {
// folders.push(d);
// }else{
// files.push(d);
// }
// } );
//
// let data = folders.concat(files);
// callback(data);
//
// // 增加缓存
// // self.cache[path] = data;
// this.cache.set(cache_tag, JSON.stringify(data));
// }, (err) => {
// toastr.error((err instanceof Object) ? JSON.stringify(err) : String(err), LANG_T['error']);
// });
}
// 更改目录,返回最终绝对路径
......@@ -225,9 +283,12 @@ class FileManager {
((p) => {
const path = this.path + p;
this.files.cell.progressOn();
this.core.filemanager.delete({
path: path
}, (ret) => {
this.core.request(
this.core.filemanager.delete({
path: path
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
toastr.success(LANG['delete']['success'](path), LANG_T['success']);
......@@ -235,10 +296,24 @@ class FileManager {
}else{
toastr.error(LANG['delete']['error'](path, ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
this.files.cell.progressOff();
toastr.error(LANG['delete']['error'](path, err), LANG_T['error']);
})
});
// this.core.filemanager.delete({
// path: path
// }, (ret) => {
// this.files.cell.progressOff();
// if (ret === '1') {
// toastr.success(LANG['delete']['success'](path), LANG_T['success']);
// this.files.refreshPath();
// }else{
// toastr.error(LANG['delete']['error'](path, ret === '0' ? false : ret), LANG_T['error']);
// }
// }, (err) => {
// this.files.cell.progressOff();
// toastr.error(LANG['delete']['error'](path, err), LANG_T['error']);
// })
})(p);
});
}
......@@ -254,10 +329,13 @@ class FileManager {
const target = this.path + name;
this.files.cell.progressOn();
this.core.filemanager.copy({
path: source,
target: target
}, (ret) => {
this.core.request(
this.core.filemanager.copy({
path: source,
target: target
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
// 刷新目录
......@@ -268,7 +346,7 @@ class FileManager {
}else{
toastr.error(LANG['paste']['error'](name, ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
toastr.error(LANG['paste']['error'](name, err), LANG_T['error']);
});
}
......@@ -281,10 +359,13 @@ class FileManager {
title: `<i class="fa fa-fa fa-font"></i> ${LANG['rename']['title']} (${antSword.noxss(name)})`
}, (value, index, elem) => {
this.files.cell.progressOn();
this.core.filemanager.rename({
path: this.path + name,
name: this.path + value + ((isDir && !value.endsWith('/')) ? '/' : '')
}, (ret) => {
this.core.request(
this.core.filemanager.rename({
path: this.path + name,
name: this.path + value + ((isDir && !value.endsWith('/')) ? '/' : '')
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
this.files.refreshPath();
......@@ -292,7 +373,7 @@ class FileManager {
}else{
toastr.error(LANG['rename']['error'](ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
toastr.error(LANG['rename']['error'](err), LANG_T['error']);
});
layer.close(index);
......@@ -306,9 +387,12 @@ class FileManager {
title: `<i class="fa fa-folder"></i> ${LANG['createFolder']['title']}`
}, (value, i, e) => {
this.files.cell.progressOn();
this.core.filemanager.mkdir({
path: this.path + value
}, (ret) => {
this.core.request(
this.core.filemanager.mkdir({
path: this.path + value
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
this.files.refreshPath();
......@@ -316,7 +400,7 @@ class FileManager {
}else{
toastr.error(LANG['createFolder']['error'](value, ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
toastr.error(LANG['createFolder']['error'](value, err), LANG_T['error']);
});
layer.close(i);
......@@ -330,10 +414,15 @@ class FileManager {
title: `<i class="fa fa-file"></i> ${LANG['createFile']['title']}`
}, (value, i, e) => {
this.files.cell.progressOn();
this.core.filemanager.create_file({
path: this.path + value,
content: '\0'
}, (ret) => {
// 发起http请求
this.core.request(
this.core.filemanager.create_file({
path: this.path + value,
content: 'Halo ANT!'
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
this.files.refreshPath();
......@@ -341,7 +430,7 @@ class FileManager {
}else{
toastr.error(LANG['createFile']['error'](value, ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
toastr.error(LANG['createFile']['error'](value, err), LANG_T['error']);
});
layer.close(i);
......@@ -356,10 +445,15 @@ class FileManager {
content: `<input type="text" class="layui-layer-input" onClick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'});" value="${oldtime}">`
}, (value, i, e) => {
this.files.cell.progressOn();
this.core.filemanager.retime({
path: this.path + name,
time: value
}, (ret) => {
// http request
this.core.request(
this.core.filemanager.retime({
path: this.path + name,
time: value
})
).then((res) => {
let ret = res['text'];
this.files.cell.progressOff();
if (ret === '1') {
this.files.refreshPath();
......@@ -367,7 +461,7 @@ class FileManager {
}else{
toastr.error(LANG['retime']['error'](name, ret === '0' ? false : ret), LANG_T['error']);
}
}, (err) => {
}).catch((err) => {
toastr.error(LANG['retime']['error'](name, err), LANG_T['error']);
});
layer.close(i);
......@@ -384,16 +478,18 @@ class FileManager {
win.cell.lastChild['style']['textAlign'] = 'center';
let data = 'data:image/png;base64,';
let buff = '';
this.core.filemanager.download_file({
path: path
}, (ret, buff) => {
let imgData = new Buffer(buff).toString('base64');
this.core.request(
this.core.filemanager.download_file()
, (chunk) => {
buff += chunk;
let imgData = data + new Buffer(buff).toString('base64');
}
).then((res) => {
let imgData = new Buffer(res['buff']).toString('base64');
win.attachHTMLString(`<img src="data:/image/png;base64,${imgData}"/>`);
}, (err) => {
}).catch((err) => {
}, (chunk) => {
buff += chunk;
let imgData = data + new Buffer(buff).toString('base64');
});
}
......@@ -408,41 +504,39 @@ class FileManager {
}, (filePath) => {
if (!filePath) { return task.end(LANG['download']['task']['cancel']) };
task.update(LANG['download']['task']['start']);
// setTimeout(() => {
let down_size = 0;
this.core.filemanager.download_file({
let down_size = 0;
// 删除旧文件(如果存在
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
this.core.download(
filePath
, this.core.filemanager.download_file({
path: path
}, (ret, buff) => {
// 保存文件
// fs.writeFileSync(filePath+'_bak', new global.Buffer(buff));
// 检测文件大小是否一致
if (buff.length === size) {
task.success(LANG['download']['task']['success']);
toastr.success(LANG['download']['success'](name), LANG_T['success']);
}else if (buff.length === 21) {
task.failed(buff.toString());
}else{
// task.failed(`FileSize(${buff.length} != ${size}`);
task.failed(LANG['download']['task']['error'](`SizeErr: ${buff.length} != ${size}`))
}
}, (err) => {
task.failed(LANG['download']['task']['error'](err));
toastr.error(LANG['download']['error'](name, err), LANG_T['error']);
}, (chunk) => {
// 写入文件
fs.writeFileSync(filePath, new global.Buffer(chunk), {
flag: 'a'
});
})
, (_size) => {
// 计算进度百分比
down_size += chunk.length;
down_size += _size;
let down_progress = parseInt(parseFloat(down_size / size).toFixed(2) * 100);
if (!(down_progress % 5)) {
task.update(down_progress + '%');
};
// task.update(`${parseFloat(down_size/size).toFixed(0) * 100} %`);
});
// }, 200);
}
).then((_size) => {
if (_size === size) {
task.success(LANG['download']['task']['success']);
toastr.success(LANG['download']['success'](name), LANG_T['success']);
// }else if (_size === 21) {
// task.failed('len=' + _size);
}else{
throw Error(`SizeErr: ${_size} != ${size}`);
// task.failed(LANG['download']['task']['error']())
}
}).catch((err) => {
task.failed(LANG['download']['task']['error'](err));
toastr.error(LANG['download']['error'](name, err), LANG_T['error']);
});
});
}
......@@ -472,19 +566,24 @@ class FileManager {
};
const task = this.tasks.new(LANG['wget']['task']['name'], `${url} -> ${path}`);
task.update(LANG['wget']['task']['start']);
this.core.filemanager.wget({
url: url,
path: path
}, (ret) => {
// 下载成功?当前目录?刷新:删除缓存
if (ret === '1') {
task.success(LANG['wget']['task']['success']);
let _ = path.substr(0, path.lastIndexOf('/') + 1);
this.files.refreshPath((_ === self.path) ? false : _);
}else{
task.failed(LANG['wget']['task']['failed'](ret));
}
}, (err) => {
// http request
this.core.request(
this.core.filemanager.wget({
url: url,
path: path
})
).then((res) => {
let ret = res['text'];
// 下载成功?当前目录?刷新:删除缓存
if (ret === '1') {
task.success(LANG['wget']['task']['success']);
let _ = path.substr(0, path.lastIndexOf('/') + 1);
this.files.refreshPath((_ === self.path) ? false : _);
}else{
task.failed(LANG['wget']['task']['failed'](ret));
}
}).catch((err) => {
task.failed(LANG['wget']['task']['error'](err));
});
layer.close(i);
......@@ -493,83 +592,109 @@ class FileManager {
$(`#layui-layer${_index}`).css('width', '400px');
}
// 上传文件
uploadFile() {
const path = this.path;
/**
* 上传文件
* @param {Array} _filePaths 要上传的本地文件路径(可选,如未指定,则调用文件选择框
* @return {None} [description]
*/
uploadFile(_filePaths) {
// 任务列表
let tasks = {};
const upload = (filePaths) => {
if (!filePaths[0]) {
return false;
};
const filePath = filePaths[0];
let i = 0;
let buff = [];
// 数据分段上传,一次上传512kb=1024*512
let split = 1024 * 512;
const task = tasks[filePath];
// 获取文件名
const fileName = filePath.substr(filePath.lastIndexOf('/') + 1);
let fileBuff;
// 读取文件buffer
try{
fileBuff = fs.readFileSync(filePath);
}catch(e) {
return task.failed(e);
// 上传路径
let path = this.path;
new Promise((res, rej) => {
// 获取要上传的文件列表
if (Array.isArray(_filePaths) && _filePaths.length > 0) {
return res(_filePaths);
}
while (true) {
const _ = fileBuff.slice(i, i + split);
i += split;
if (_.length === 0) { break };
buff.push(_);
}
const all_num = buff.length;
const upload_func = (arr) => {
if (!arr[0]) {
// 上传完毕
task.success('100%');
// 刷新目录
toastr.success(LANG['upload']['success'](fileName), LANG_T['success']);
// 刷新目录缓存
this.files.refreshPath(path === this.path ? '' : path);
return upload(filePaths);
};
// 更新进度条
task.update(`${parseInt((all_num - arr.length) / all_num * 100)}%`);
// 开始上传分段数据
this.core.filemanager.upload_file({
path: path + fileName,
content: arr[0]
}, (ret) => {
if (ret === '1') {
// 继续上传
arr.shift();
upload_func(arr);
}else{
task.failed(LANG['upload']['task']['failed'](ret));
toastr.error(LANG['upload']['error'](fileName, ret === '0' ? '' : '<br/>' + ret), LANG_T['error']);
dialog.showOpenDialog({
properties: [ 'openFile', 'multiSelections' ]
}, (_filePaths) => {
if (!_filePaths) { return };
return res(_filePaths);
})
}).then((filePaths) => {
// 初始化任务
filePaths.map((f) => {
const fileName = f.substr(f.lastIndexOf('/') + 1);
tasks[f] = this.tasks.new(LANG['upload']['task']['name'], `${fileName} => ${path}`, 'Waiting for uploading..');
});
return filePaths;
}).then((filePaths) => {
// 文件上传(逐个队列上传
const upload = () => {
new Promise((res, rej) => {
// 获取单个上传文件
let filePath = filePaths.shift();
if (filePath) {
res(filePath);
}
}).then((filePath) => {
// 上传单个
let buffIndex = 0;
let buff = [];
// 分段上传大小,默认1M
let dataSplit = 1024 * 1024;
let task = tasks[filePath];
// 获取文件名
let fileName = filePath.substr(filePath.lastIndexOf('/') + 1);
// 读取文件buff
let fileBuff;
try {
fileBuff = fs.readFileSync(filePath);
} catch (e) {
return task.failed(e);
}
// 文件数据分段
let buffLength = fileBuff.length;
while (buffIndex <= buffLength) {
let buffSplit = fileBuff.slice(buffIndex, buffIndex + dataSplit);
buffIndex += dataSplit;
buff.push(buffSplit);
}
// 开始上传
const uploadBuffFunc = (_buff) => {
new Promise((res, rej) => {
let _b = _buff.shift();
if (_b) {
res(_b);
}else{
// 上传完毕
task.success('100%');
toastr.success(LANG['upload']['success'](fileName), LANG_T['success']);
// 刷新缓存
this.files.refreshPath(path === this.path ? '' : path);
// 继续上传
return upload();
}
}).then((b) => {
// 更新进度条
task.update(`${parseInt((buffLength - _buff.length) / buffLength * 100)}%`);
this.core.request(
this.core.filemanager.upload_file({
path: path + fileName,
content: b
})
).then((res) => {
let ret = res['text'];
if (ret === '1') {
return uploadBuffFunc(_buff);
}
task.failed(LANG['upload']['task']['failed'](ret));
toastr.error(LANG['upload']['error'](
fileName,
ret === '0' ? '' : `<br/>${ret}`
), LANG_T['error']);
}).catch((err) => {
task.failed(LANG['upload']['task']['error'](err));
toastr.error(LANG['upload']['error'](fileName, err), LANG_T['error']);
});
})
}
}, (err) => {
task.failed(LANG['upload']['task']['error'](err));
toastr.error(LANG['upload']['error'](fileName, err), LANG_T['error']);
uploadBuffFunc(buff);
});
}
upload_func(buff);
filePaths.shift();
}
dialog.showOpenDialog({
properties: [ 'openFile', 'multiSelections' ]
}, (filePaths) => {
if (!filePaths) { return };
// 逐个文件上传
filePaths.map((_) => {
const fileName = _.substr(_.lastIndexOf('/') + 1);
tasks[_] = this.tasks.new(LANG['upload']['task']['name'], `${fileName} => ${path}`, '准备上传..');
});
upload(filePaths);
};
upload();
});
}
......@@ -590,34 +715,13 @@ class FileManager {
// 检测文件后缀
let ext = name.substr(name.lastIndexOf('.') + 1);
let ext_dict = {
'php': 'php',
'c': 'c_cpp',
'cpp': 'c_cpp',
'h': 'c_cpp',
'coffee': 'coffee',
'cfm': 'coldfusion',
'css': 'css',
// 'ejs': 'ejs',
'go': 'golang',
'html': 'html',
'ini': 'ini',
'conf': 'ini',
'jade': 'jade',
'java': 'java',
'js': 'javascript',
'json': 'json',
'jsp': 'jsp',
'jsx': 'jsx',
'less': 'less',
'lua': 'lua',
'md': 'markdown',
'sql': 'sql',
'pl': 'perl',
'py': 'python',
'rb': 'ruby',
'sh': 'sh',
'txt': 'text',
'xml': 'xml'
'php': 'php', 'c': 'c_cpp', 'cpp': 'c_cpp', 'h': 'c_cpp',
'coffee': 'coffee', 'cfm': 'coldfusion', 'css': 'css',
'go': 'golang', 'html': 'html', 'ini': 'ini', 'conf': 'ini',
'jade': 'jade', 'java': 'java', 'js': 'javascript', 'json': 'json',
'jsp': 'jsp', 'jsx': 'jsx', 'less': 'less', 'lua': 'lua', 'md': 'markdown',
'sql': 'sql', 'pl': 'perl', 'py': 'python', 'rb': 'ruby',
'sh': 'sh', 'txt': 'text', 'xml': 'xml'
}
if (!(ext in ext_dict)) { ext = 'txt' };
// 创建窗口工具栏
......@@ -665,21 +769,24 @@ class FileManager {
if (id === 'save') {
// 保存代码
win.progressOn();
// self.ajax(
self.core.filemanager.create_file({
path: path,
content: editor.session.getValue() || '\0'
}, (ret) => {
win.progressOff();
if (ret === '1') {
toastr.success(LANG['editor']['success'](path), LANG_T['success']);
// 刷新目录(显示更改时间、大小等)
self.files.refreshPath();
}else{
toastr.error(LANG['editor']['error'](path, ret === '0' ? '' : '<br/>' + ret), LANG_T['error']);
}
self.core.request(
self.core.filemanager.create_file({
path: path,
content: editor.session.getValue() || 'Halo ANT!'
})
).then((res) => {
let ret = res['text'];
win.progressOff();
if (ret === '1') {
toastr.success(LANG['editor']['success'](path), LANG_T['success']);
// 刷新目录(显示更改时间、大小等)
self.files.refreshPath();
}else{
toastr.error(LANG['editor']['error'](path, ret === '0' ? '' : '<br/>' + ret), LANG_T['error']);
}
)
}).catch((err) => {
});
}else if (id.startsWith('mode_')) {
let mode = id.split('_')[1];
editor.session.setMode(`ace/mode/${mode}`);
......@@ -692,9 +799,12 @@ class FileManager {
});
// 获取文件代码
this.core.filemanager.read_file({
path: path
}, (ret) => {
this.core.request(
this.core.filemanager.read_file({
path: path
})
).then((res) => {
let ret = res['text'];
codes = ret;
win.progressOff();
......@@ -732,7 +842,7 @@ class FileManager {
clearInterval(inter);
return true;
});
}, (err) => {
}).catch((err) => {
toastr.error(LANG['editor']['loadErr'](err), LANG_T['error']);
win.close();
});
......@@ -761,4 +871,4 @@ class FileManager {
}
export default FileManager;
\ No newline at end of file
export default FileManager;
//
// 虚拟终端模块
//
/**
* 虚拟终端模块
* 更新:2016/04/13
* 作者:蚁逅 <https://github.com/antoor>
*/
// import React from 'react';
// import ReactDOM from 'react-dom';
const LANG_T = antSword['language']['toastr'];
const LANG = antSword['language']['terminal'];
// const ipcRenderer = antSword['ipcRenderer'];
const LANG_T = antSword['language']['toastr'];
class Terminal {
constructor(opts) {
const tabbar = antSword['tabbar'];
// 生存一个随机ID,用于标识多个窗口dom
const hash = String(Math.random()).substr(2, 10);
// 初始化UI::tabbar
const tabbar = antSword['tabbar'];
tabbar.addTab(
`tab_terminal_${hash}`,
// `<i class="fa fa-terminal"></i> ${LANG['title']} \/\/ ${opts['ip']}`,
`<i class="fa fa-terminal"></i> ${opts['ip']}`,
null, null, true, true
);
tabbar.attachEvent('onTabClick', (id,lid) => {
if (id === `tab_terminal_${hash}`) {
this.term ? this.term.focus() : 0;
};
if (id !== `tab_terminal_${hash}`) { return };
this.term ? this.term.focus() : 0;
});
// 初始化UI::cell
const cell = tabbar.cells(`tab_terminal_${hash}`);
// ReactDOM.render(
// <div id={`div_terminal_${hash}`} style={{
// height: '100%',
// margin: '0',
// padding: '0px 5px 1px 5px',
// overflow: 'scroll'
// }} />
// , cell.cell
// );
cell.attachHTMLString(`
<div id="div_terminal_${hash}" style="height:100%;margin:0;padding:0 5px 1px 5px;overflow:scroll"></div>
<div
id="div_terminal_${hash}"
style="height:100%;margin:0;padding:0 5px 1px 5px;overflow:scroll"
></div>
`);
this.path = '';
......@@ -48,160 +40,187 @@ class Terminal {
this.cell = cell;
this.isWin = true;
this.core = new antSword['core'][opts['type']](opts);
this.cache = new antSword['CacheManager'](this.opts['_id']);
// this.initTerminal($(`#div_terminal_${hash}`));
// 获取缓存::info
const dom = $(`#div_terminal_${hash}`);
const cache_info = this.cache.get('info');
if (cache_info) {
this.initTerminal(cache_info, dom);
}else{
this.cell.progressOn();
this.core.base.info((ret) => {
this.cell.progressOff();
// 判断获取数据是否正确
// let info = ret.split('\t');
// if (info.length !== 4) {
// toastr.error('Loading infomations failed!', LANG_T['error']);
// return this.cell.close();
// };
// 更新缓存
this.cache.set('info', ret);
// ipcRenderer.sendSync('cache-add', {
// id: this.opts['id'],
// tag: 'info',
// cache: ret
// });
// 初始化终端
this.initTerminal(ret, dom);
}, (err) => {
this
.getInformation()
.then((ret) => {
this.initTerminal(ret['info'], ret['dom']);
})
.catch((err) => {
toastr.error((typeof(err) === 'object') ? JSON.stringify(err) : String(err), LANG_T['error']);
this.cell.progressOff();
this.cell.close();
})
}
/**
* 获取目标信息
* @return {Promise} 返回一个Promise操作对象//{dom,info}
*/
getInformation() {
return new Promise((ret, rej) => {
// 获取DOM
const dom = $(`#div_terminal_${this.hash}`);
// 获取缓存
let infoCache = this.cache.get('info');
// 如果有缓存?初始化终端:获取信息&&保存缓存&&初始化终端
if (infoCache) {
return ret({
dom: dom,
info: infoCache
});
}
// 开始获取信息
this.cell.progressOn();
this.core.request(
this.core.base.info()
).then((_ret) => {
this.cell.progressOff();
this.cache.set('info', _ret['text']);
return ret({
dom: dom,
info: _ret['text']
});
}).catch((e) => {
rej(e);
});
}
});
}
// 初始化终端
/**
* 初始化终端
* @param {String} ret 目标信息
* @param {Object} dom 要操作的dom元素
* @return {None} [description]
*/
initTerminal(ret, dom) {
// this.cell.progressOn();
// // 获取缓存info
// const _info = ipcRenderer.sendSync('cache-get', {
// id: this.opts['id'],
// tag: 'info'
// });
// this.core.base.info((ret) => {
// this.cell.progressOff();
let info = ret.split('\t');
let info_user;
let info_path;
let info_drive;
let info_system;
// 解析banner
let banner = `[[b;cyan;](*) ${LANG['banner']['title']}]`;
// 判断获取数据是否正确
if (info.length === 4) {
info_user = info[3];
info_path = info[0];
info_drive = info[1];
info_system = info[2];
}else if (info.length === 2) {
info_path = info[0];
info_drive = info[1];
}else{
toastr.error('Loading infomations failed!<br/>' + ret, LANG_T['error']);
this.cache.del('info');
return this.cell.close();
};
info_path = info_path.replace(/\\/g, '/').replace(/\.$/, '');
// 判断是否为linux
if (info_path.substr(0, 1) === '/') {
this.isWin = false;
let info = ret.split('\t');
let infoUser, infoPath, infoDrive, infoSystem;
let banner = `[[b;cyan;](*) ${LANG['banner']['title']}]`;
// 判断数据是否正确
if (info.length === 4) {
infoPath = info[0];
infoDrive = info[1];
infoSystem = info[2];
infoUser = info[3];
} else if (info.length === 2) {
infoPath = info[0];
infoDrive = info[1];
} else {
toastr.error('Loading infomations failed!<br/>' + ret, LANG_T['error']);
this.cache.del('info');
return this.cell.close();
}
// 转换路径特殊字符
infoPath = infoPath.replace(/\\/g, '/').replace(/\.$/, '');
// 判断是否为!win
this.isWin = !(infoPath.substr(0, 1) === '/')
this.path = infoPath;
// 组合banner
banner += `\n[[b;#99A50D;]${LANG['banner']['path']}]: [[;#C3C3C3;]${infoPath}]`;
banner += `\n[[b;#99A50D;]${LANG['banner']['drive']}]: [[;#C3C3C3;]${infoDrive}]`;
if (info.length === 4) {
banner += `\n[[b;#99A50D;]${LANG['banner']['system']}]: [[;#C3C3C3;]${infoSystem}]`;
banner += `\n[[b;#99A50D;]${LANG['banner']['user']}]: [[;#C3C3C3;]${infoUser}]`;
}
// 初始化终端
this.term = dom.terminal( (cmd, term) => {
if (!cmd) { return false };
// 如果为exit||quit则关闭窗口
if (cmd === 'exit' || cmd === 'quit') { return this.cell.close() };
term.pause();
// 是否有缓存
let cacheTag = 'command-' + new Buffer(this.path + cmd).toString('base64');
let cacheCmd;
if (cacheCmd = this.cache.get(cacheTag)) {
term.echo(
antSword.noxss(cacheCmd)
);
return term.resume();
};
this.path = info_path;
banner += `\n[[b;#99A50D;]${LANG['banner']['path']}]: [[;#C3C3C3;]${info_path}]`;
banner += `\n[[b;#99A50D;]${LANG['banner']['drive']}]: [[;#C3C3C3;]${info_drive}]`;
if (info.length === 4) {
banner += `\n[[b;#99A50D;]${LANG['banner']['system']}]: [[;#C3C3C3;]${info_system}]`;
banner += `\n[[b;#99A50D;]${LANG['banner']['user']}]: [[;#C3C3C3;]${info_user}]`;
}
// 初始化终端
this.term = dom.terminal( (cmd, term) => {
if (!cmd) { return false };
// 如果为exit||quit则关闭窗口
if (cmd === 'exit' || cmd === 'quit') { return this.cell.close() };
term.pause();
// 是否有缓存
let cache_tag = 'command-' + new Buffer(this.path + cmd).toString('base64');
let cache_cmd;
if (cache_cmd = this.cache.get(cache_tag)) {
term.echo(antSword.noxss(cache_cmd));
return term.resume();
};
// 开始执行命令
this.core.request(
this.core.command.exec({
cmd: this.parseCmd(cmd, this.path),
bin: this.isWin ? 'cmd' : '/bin/sh'
}, (_) => {
// 解析出命令执行路径
const index_s = _.indexOf('[S]');
const index_e = _.lastIndexOf('[E]');
let _path = _.substr(index_s + 3, index_e - index_s - 3);
// let _sindex = _.indexOf('[S]') + 3;
// let _eindex = _.indexOf('[E]');
// let _path = _.substr(_sindex, _eindex - _sindex);
let output = _.replace(`[S]${_path}[E]`, '');
_path = _path.replace(/\n/g, '').replace(/\r/g, '');
this.path = _path || this.path;
term.set_prompt(this.parsePrompt(info_user));
// let output = _.substr(0, _sindex - 3).replace(/\n$/, '');
// 去除换行符
[/\n\n$/, /^\n\n/, /\r\r$/, /^\r\r/, /\r\n$/, /^\r\n/, /\n\r$/, /^\n\r/, /\r$/, /^\r/, /\n$/, /^\n/].map((_) => {
output = output.replace(_, '');
});
// output = output.replace(/\n$/, '').replace(/^\n/, '').replace(/^\r/, '').replace(/\r$/, '').;
if (output.length > 0) {
term.echo(antSword.noxss(output));
// 保存最大100kb数据
if (output.length < (1024 * 1024)) {
this.cache.set(cache_tag, output);
};
};
term.resume();
}, (_) => {
term.error('ERR: ' + (_ instanceof Object) ? JSON.stringify(_) : String(_));
term.resume();
}
)
}, {
greetings: banner,
name: `terminal_${this.hash}`,
prompt: this.parsePrompt(info_user),
exit: false
cmd: this.parseCmd(cmd, this.path),
bin: this.isWin ? 'cmd' : '/bin/sh'
})
).then((ret) => {
let _ = ret['text'];
// 解析出命令执行路径
const indexS = _.indexOf('[S]');
const indexE = _.lastIndexOf('[E]');
let _path = _.substr(indexS + 3, indexE - indexS - 3);
let output = _.replace(`[S]${_path}[E]`, '');
_path = _path.replace(/\n/g, '').replace(/\r/g, '');
this.path = _path || this.path;
term.set_prompt(this.parsePrompt(infoUser));
// 去除换行符
[
/\n\n$/, /^\n\n/, /\r\r$/,
/^\r\r/, /\r\n$/, /^\r\n/,
/\n\r$/, /^\n\r/, /\r$/,
/^\r/, /\n$/, /^\n/
].map((_) => {
output = output.replace(_, '');
});
if (output.length > 0) {
term.echo(
antSword.noxss(output)
);
// 保存最大100kb数据
if (output.length < (1024 * 1024)) {
this.cache.set(cacheTag, output);
};
};
term.resume();
}).catch((e) => {
term.error('ERR: ' + (_ instanceof Object) ? JSON.stringify(_) : String(_));
term.resume();
});
// }, (err) => {
// toastr.error(err, LANG_T['error']);
// this.cell.progressOff();
// this.cell.close();
// });
}, {
greetings: banner,
name: `terminal_${this.hash}`,
prompt: this.parsePrompt(infoUser),
exit: false
});
}
// 生成CMD代码
/**
* 根据执行命令&&路径生成最终command
* @param {String} cmd 要执行的命令
* @param {String} path 当前路径
* @return {String} 最终执行命令
*/
parseCmd(cmd, path) {
path = path.replace(/\\\\/g, '\\').replace(/"/g, '\\"').replace(/\\/g, '\\\\');
return this.isWin ? `cd /d "${path}"&${cmd}&echo [S]&cd&echo [E]` : `cd "${path}";${cmd};echo [S];pwd;echo [E]`;
return (this.isWin
? `cd /d "${path}"&${cmd}&echo [S]&cd&echo [E]`
: `cd "${path}";${cmd};echo [S];pwd;echo [E]`
);
}
// 生成路径提示
/**
* 生成路径提示符
* @param {String} user 当前用户名
* @return {String} term输出字符串
*/
parsePrompt(user) {
let ret = this.isWin ? '[[b;white;]' + this.path.replace(/\//g, '\\') + '> ]' : (user ? ('([[b;#E80000;]' + user + ']:[[;#0F93D2;]') : '[[;0F93D2;]') + this.path + ']) $ ';
return antSword.noxss(ret);
return antSword.noxss(this.isWin
? `[[b;white;]${this.path.replace(/\//g, '\\')}> ]`
: (user ? `([[b;#E80000;]${user}]:[[;#0F93D2;]` : '[[;0F93D2;]') + this.path + ']) $ '
);
}
}
export default Terminal;
\ No newline at end of file
export default Terminal;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment