def listApi(self): _list = public.M('crontab').where('', ()).field( self.field).limit('0,5').order('id desc').select() data = [] for i in range(len(_list)): tmp = _list[i] if _list[i]['type'] == "day": tmp['type'] = '每天' tmp['cycle'] = public.getInfo('每天, {1}点{2}分 执行', (str( _list[i]['where_hour']), str(_list[i]['where_minute']))) elif _list[i]['type'] == "day-n": tmp['type'] = public.getInfo('每{1}天', (str(_list[i]['where1']), )) tmp['cycle'] = public.getInfo( '每隔{1}天, {2}点{3}分 执行', (str(_list[i]['where1']), str(_list[i]['where_hour']), str(_list[i]['where_minute']))) elif _list[i]['type'] == "hour": tmp['type'] = '每小时' tmp['cycle'] = public.getInfo( '每小时, 第{1}分钟 执行', (str(_list[i]['where_minute']), )) elif _list[i]['type'] == "hour-n": tmp['type'] = public.getInfo('每{1}小时', (str(_list[i]['where1']), )) tmp['cycle'] = public.getInfo( '每{1}小时, 第{2}分钟 执行', (str(_list[i]['where1']), str(_list[i]['where_minute']))) elif _list[i]['type'] == "minute-n": tmp['type'] = public.getInfo('每{1}分钟', (str(_list[i]['where1']), )) tmp['cycle'] = public.getInfo('每隔{1}分钟执行', (str(_list[i]['where1']), )) elif _list[i]['type'] == "week": tmp['type'] = '每周' if not _list[i]['where1']: _list[i]['where1'] = '0' tmp['cycle'] = public.getInfo( '每周{1}, {2}点{3}分执行', (self.toWeek(int( _list[i]['where1'])), str(_list[i]['where_hour']), str(_list[i]['where_minute']))) elif _list[i]['type'] == "month": tmp['type'] = '每月' tmp['cycle'] = public.getInfo( '每月, {1}日 {2}点{3}分执行', (str(_list[i]['where1']), str(_list[i]['where_hour']), str(_list[i]['where_minute']))) data.append(tmp) _ret = {} _ret['data'] = data count = public.M('crontab').where('', ()).count() _page = {} _page['count'] = count _page['tojs'] = 'remind' _ret['page'] = public.getPage(_page) return public.getJson(_ret)
def batchPasteApi(self): path = request.form.get('path', '').encode('utf-8') stype = request.form.get('type', '').encode('utf-8') # filename = request.form.get('filename', '').encode('utf-8') import shutil if not self.checkDir(path): return public.returnJson(False, '请不要花样作死!') i = 0 myfiles = json.loads(session['selected']['data']) l = len(myfiles) if stype == '1': for key in myfiles: i += 1 public.writeSpeed(key, i, l) try: sfile = session['selected'][ 'path'] + '/' + key.encode('utf-8') dfile = path + '/' + key.encode('utf-8') if os.path.isdir(sfile): shutil.copytree(sfile, dfile) else: shutil.copyfile(sfile, dfile) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) except: continue msg = public.getInfo('从[{1}]批量复制到[{2}]成功', (session['selected']['path'], path,)) public.writeLog('文件管理', msg) else: for key in myfiles: try: i += 1 public.writeSpeed(key, i, l) sfile = session['selected'][ 'path'] + '/' + key.encode('utf-8') dfile = path + '/' + key.encode('utf-8') shutil.move(sfile, dfile) except: continue msg = public.getInfo('从[{1}]批量移动到[{2}]成功', (session['selected']['path'], path,)) public.writeLog('文件管理', msg) public.writeSpeed(None, 0, 0) errorCount = len(myfiles) - i del(session['selected']) msg = public.getInfo('批量操作成功[{1}],失败[{2}]', (str(i), str(errorCount))) return public.returnJson(True, msg)
def reRecycleBinApi(self): rPath = self.rPath path = request.form.get('path', '').encode('utf-8') dFile = path.replace('_mw_', '/').split('_t_')[0] try: import shutil shutil.move(rPath + path, dFile) msg = public.getInfo('移动文件[{1}]到回收站成功!', (dFile,)) public.writeLog('文件管理', msg) return public.returnJson(True, '恢复成功!') except Exception as e: msg = public.getInfo('从回收站恢复[{1}]失败!', (dFile,)) public.writeLog('文件管理', msg) return public.returnJson(False, '恢复失败!')
def setFileAccessApi(self): if public.isAppleSystem(): return public.returnJson(True, '开发机不设置!') filename = request.form.get('filename', '').encode('utf-8') user = request.form.get('user', '').encode('utf-8') access = request.form.get('access', '755') sall = '-R' try: if not self.checkDir(filename): return public.returnJson(False, '请不要花样作死') if not os.path.exists(filename): return public.returnJson(False, '指定文件不存在!') os.system('chmod ' + sall + ' ' + access + " '" + filename + "'") os.system('chown ' + sall + ' ' + user + ':' + user + " '" + filename + "'") msg = public.getInfo( '设置[{1}]权限为[{2}]所有者为[{3}]', (filename, access, user,)) public.writeLog('文件管理', msg) return public.returnJson(True, '设置成功!') except: return public.returnJson(False, '设置失败!')
def mvRecycleBin(self, path): rPath = self.rPath if not os.path.exists(rPath): os.system('mkdir -p ' + rPath) rFile = rPath + path.replace('/', '_mw_') + '_t_' + str(time.time()) try: import shutil shutil.move(path, rFile) public.writeLog('文件管理', public.getInfo( '移动文件[{1}]到回收站成功!', (path))) return True except: public.writeLog('文件管理', public.getInfo( '移动文件[{1}]到回收站失败!', (path))) return False
def createDirApi(self): path = request.form.get('path', '').encode('utf-8') try: if not self.checkFileName(path): return public.returnJson(False, '目录名中不能包含特殊字符!') if os.path.exists(path): return public.returnJson(False, '指定目录已存在!') os.makedirs(path) self.setFileAccept(path) msg = public.getInfo('创建目录[{1}]成功!', (path,)) public.writeLog('文件管理', msg) return public.returnJson(True, '目录创建成功!') except Exception as e: return public.returnJson(False, '目录创建失败!')
def copyDir(self, sfile, dfile): if not os.path.exists(sfile): return public.returnJson(False, '指定目录不存在!') if os.path.exists(dfile): return public.returnJson(False, '指定目录已存在!') import shutil try: shutil.copytree(sfile, dfile) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) msg = public.getInfo('复制目录[{1}]到[{2}]成功!', (sfile, dfile)) public.writeLog('文件管理', msg) return public.returnJson(True, '目录复制成功!') except: return public.returnJson(False, '目录复制失败!')
def createFileApi(self): file = request.form.get('path', '').encode('utf-8') try: if not self.checkFileName(file): return public.returnJson(False, '文件名中不能包含特殊字符!') if os.path.exists(file): return public.returnJson(False, '指定文件已存在!') _path = os.path.dirname(file) if not os.path.exists(_path): os.makedirs(_path) open(file, 'w+').close() self.setFileAccept(file) msg = public.getInfo('创建文件[{1}]成功!', (file,)) public.writeLog('文件管理', msg) return public.returnJson(True, '文件创建成功!') except Exception as e: # print str(e) return public.returnJson(True, '文件创建失败!')
def delete(self, path): if not os.path.exists(path): return public.returnJson(False, '指定文件不存在!') # 检查是否为.user.ini if path.find('.user.ini') >= 0: os.system("chattr -i '" + path + "'") try: if os.path.exists('data/recycle_bin.pl'): if self.mvRecycleBin(path): return public.returnJson(True, '已将文件移动到回收站!') os.remove(path) public.writeLog('文件管理', public.getInfo( '删除文件[{1}]成功!', (path))) return public.returnJson(True, '删除文件成功!') except: return public.returnJson(False, '删除文件失败!')
def mvFileApi(self): sfile = request.form.get('sfile', '').encode('utf-8') dfile = request.form.get('dfile', '').encode('utf-8') if not self.checkFileName(dfile): return public.returnJson(False, '文件名中不能包含特殊字符!') if not os.path.exists(sfile): return public.returnJson(False, '指定文件不存在!') if not self.checkDir(sfile): return public.returnJson(False, 'FILE_DANGER') import shutil try: shutil.move(sfile, dfile) msg = public.getInfo('移动文件或目录[{1}]到[{2}]成功!', (sfile, dfile,)) public.writeLog('文件管理', msg) return public.returnJson(True, '移动文件或目录成功!') except: return public.returnJson(False, '移动文件或目录失败!')
def copyFileApi(self): sfile = request.form.get('sfile', '').encode('utf-8') dfile = request.form.get('dfile', '').encode('utf-8') if not os.path.exists(sfile): return public.returnJson(False, '指定文件不存在!') if os.path.isdir(sfile): return self.copyDir(sfile, dfile) import shutil try: shutil.copyfile(sfile, dfile) msg = public.getInfo('复制文件[{1}]到[{2}]成功!', (sfile, dfile,)) public.writeLog('文件管理', msg) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) return public.returnJson(True, '文件复制成功!') except: return public.returnJson(False, '文件复制失败!')
def delRecycleBinApi(self): rPath = self.rPath path = request.form.get('path', '').encode('utf-8') empty = request.form.get('empty', '').encode('utf-8') dFile = path.split('_t_')[0] if not self.checkDir(path): return public.returnJson(False, '敏感目录,请不要花样作死!') os.system('which chattr && chattr -R -i ' + rPath + path) if os.path.isdir(rPath + path): import shutil shutil.rmtree(rPath + path) else: os.remove(rPath + path) tfile = path.replace('_mw_', '/').split('_t_')[0] msg = public.getInfo('已彻底从回收站删除{1}!', (tfile,)) public.writeLog('文件管理', msg) return public.returnJson(True, msg)
def getCrondCycle(self, params): cuonConfig = '' name = '' if params['type'] == "day": cuonConfig = self.getDay(params) name = '每天' elif params['type'] == "day-n": cuonConfig = self.getDay_N(params) name = public.getInfo('每{1}天', (params['where1'], )) elif params['type'] == "hour": cuonConfig = self.getHour(params) name = '每小时' elif params['type'] == "hour-n": cuonConfig = self.getHour_N(params) name = '每小时' elif params['type'] == "minute-n": cuonConfig = self.minute_N(params) elif params['type'] == "week": params['where1'] = params['week'] cuonConfig = self.week(params) elif params['type'] == "month": cuonConfig = self.month(params) return cuonConfig, params, name
def delApi(self): sid = request.form.get('id', '') try: find = public.M('crontab').where( "id=?", (sid, )).field('name,echo').find() if not self.removeForCrond(find['echo']): return public.returnJson(False, '无法写入文件,请检查是否开启了系统加固功能!') cronPath = public.getServerDir() + '/cron' sfile = cronPath + '/' + find['echo'] if os.path.exists(sfile): os.remove(sfile) sfile = cronPath + '/' + find['echo'] + '.log' if os.path.exists(sfile): os.remove(sfile) public.M('crontab').where("id=?", (sid, )).delete() public.writeLog('计划任务', public.getInfo('删除计划任务[{1}]成功!', (find['name'], ))) return public.returnJson(True, '删除成功') except Exception as e: return public.returnJson(False, '删除失败:' + str(e))
def uploadFileApi(self): from werkzeug.utils import secure_filename from flask import request path = request.args.get('path', '').encode('utf-8') if not os.path.exists(path): os.makedirs(path) f = request.files['zunfile'] filename = os.path.join(path, f.filename) if sys.version_info[0] == 2: filename = filename.encode('utf-8') s_path = path if os.path.exists(filename): s_path = filename p_stat = os.stat(s_path) f.save(filename) os.chown(filename, p_stat.st_uid, p_stat.st_gid) os.chmod(filename, p_stat.st_mode) msg = public.getInfo('上传文件[{1}] 到 [{2}]成功!', (filename, path)) public.writeLog('文件管理', msg) return public.returnMsg(True, '上传成功!')