mdserver-web/class/core/files_api.py

1161 lines
41 KiB
Python
Raw Normal View History

2018-12-11 00:29:27 -05:00
# coding: utf-8
2022-11-17 21:34:18 -05:00
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# 文件类操作
# ---------------------------------------------------------------------------------
2018-12-11 00:29:27 -05:00
import psutil
import time
import os
import sys
2020-07-10 03:57:25 -04:00
import mw
2018-12-11 00:29:27 -05:00
import re
import json
import pwd
2019-02-17 01:42:51 -05:00
import shutil
2018-12-11 00:29:27 -05:00
2018-12-25 03:56:57 -05:00
from flask import request
from flask import send_file, send_from_directory
from flask import make_response
from flask import session
2018-12-11 00:29:27 -05:00
2018-12-25 03:56:57 -05:00
class files_api:
2018-12-11 00:29:27 -05:00
2019-02-16 07:40:58 -05:00
rPath = None
2018-12-11 00:29:27 -05:00
def __init__(self):
2021-11-23 13:32:27 -05:00
self.rPath = mw.getRootDir() + '/recycle_bin/'
2018-12-11 00:29:27 -05:00
2018-12-25 03:56:57 -05:00
##### ----- start ----- ###
def getBodyApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2018-12-25 03:56:57 -05:00
return self.getBody(path)
2019-01-10 12:45:04 -05:00
def getLastBodyApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2019-01-10 13:13:40 -05:00
line = request.form.get('line', '100')
2019-01-10 12:45:04 -05:00
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件不存在', (path,))
2019-01-10 12:45:04 -05:00
try:
2022-09-19 23:30:04 -04:00
data = mw.getLastLine(path, int(line))
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', data)
2019-01-10 12:45:04 -05:00
except Exception as ex:
2022-11-13 02:30:01 -05:00
return mw.returnJson(False, '无法正确读取文件!' + str(ex))
2019-01-10 12:45:04 -05:00
2018-12-25 03:56:57 -05:00
def saveBodyApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
data = request.form.get('data', '')
encoding = request.form.get('encoding', '')
2018-12-25 03:56:57 -05:00
return self.saveBody(path, data, encoding)
def downloadApi(self):
2021-05-01 22:23:02 -04:00
filename = request.args.get('filename', '')
2018-12-25 03:56:57 -05:00
if not os.path.exists(filename):
return ''
2023-08-26 10:23:13 -04:00
is_attachment = True
if filename.endswith(".svg"):
is_attachment = False
2018-12-25 03:56:57 -05:00
response = make_response(send_from_directory(
2023-08-26 10:23:13 -04:00
os.path.dirname(filename), os.path.basename(filename), as_attachment=is_attachment))
2018-12-25 03:56:57 -05:00
return response
def zipApi(self):
2021-05-01 22:23:02 -04:00
sfile = request.form.get('sfile', '')
dfile = request.form.get('dfile', '')
stype = request.form.get('type', '')
path = request.form.get('path', '')
2018-12-25 03:56:57 -05:00
return self.zip(sfile, dfile, stype, path)
2022-06-14 23:41:14 -04:00
def unzipApi(self):
sfile = request.form.get('sfile', '')
dfile = request.form.get('dfile', '')
stype = request.form.get('type', '')
path = request.form.get('path', '')
return self.unzip(sfile, dfile, stype, path)
2023-09-14 12:01:37 -04:00
def uncompressApi(self):
sfile = request.form.get('sfile', '')
dfile = request.form.get('dfile', '')
path = request.form.get('path', '')
return self.uncompress(sfile, dfile, path)
2019-03-25 09:42:49 -04:00
# 移动文件或目录
def mvFileApi(self):
2021-05-01 22:23:02 -04:00
sfile = request.form.get('sfile', '')
dfile = request.form.get('dfile', '')
2019-03-25 09:46:57 -04:00
if not self.checkFileName(dfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件名中不能包含特殊字符!')
2019-03-25 09:46:57 -04:00
if not os.path.exists(sfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件不存在!')
2019-03-25 09:42:49 -04:00
2019-03-25 09:46:57 -04:00
if not self.checkDir(sfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, 'FILE_DANGER')
2019-03-25 09:42:49 -04:00
import shutil
try:
shutil.move(sfile, dfile)
2022-12-07 09:56:18 -05:00
msg = mw.getInfo('移动或重名命文件[{1}]到[{2}]成功!', (sfile, dfile,))
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', msg)
2022-12-07 09:56:18 -05:00
return mw.returnJson(True, '移动或重名命文件成功!')
2019-03-25 09:42:49 -04:00
except:
2022-12-07 09:56:18 -05:00
return mw.returnJson(False, '移动或重名命文件失败!')
2019-03-25 09:42:49 -04:00
2018-12-25 03:56:57 -05:00
def deleteApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2018-12-25 03:56:57 -05:00
return self.delete(path)
def fileAccessApi(self):
2021-05-01 22:23:02 -04:00
filename = request.form.get('filename', '')
2018-12-25 03:56:57 -05:00
data = self.getAccess(filename)
2024-05-11 13:10:04 -04:00
data['sys_users'] = self.getSysUserList()
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2018-12-25 03:56:57 -05:00
2019-03-18 02:42:04 -04:00
def setFileAccessApi(self):
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
return mw.returnJson(True, '开发机不设置!')
2019-03-18 02:42:04 -04:00
2021-05-01 22:23:02 -04:00
filename = request.form.get('filename', '')
user = request.form.get('user', '')
2019-03-18 02:42:04 -04:00
access = request.form.get('access', '755')
sall = '-R'
try:
2019-03-25 09:42:49 -04:00
if not self.checkDir(filename):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '请不要花样作死')
2019-03-18 02:42:04 -04:00
if not os.path.exists(filename):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件不存在!')
2019-03-18 02:42:04 -04:00
os.system('chmod ' + sall + ' ' + access + " '" + filename + "'")
2019-03-25 09:42:49 -04:00
os.system('chown ' + sall + ' ' + user +
':' + user + " '" + filename + "'")
2020-07-10 03:57:25 -04:00
msg = mw.getInfo(
2019-03-25 09:42:49 -04:00
'设置[{1}]权限为[{2}]所有者为[{3}]', (filename, access, user,))
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', msg)
return mw.returnJson(True, '设置成功!')
2019-03-18 02:42:04 -04:00
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '设置失败!')
2019-03-18 02:42:04 -04:00
2018-12-25 03:56:57 -05:00
def getDirSizeApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2019-02-20 22:23:01 -05:00
tmp = self.getDirSize(path)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, tmp[0].split()[0])
2018-12-25 03:56:57 -05:00
def getDirApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2018-12-25 03:56:57 -05:00
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
path = mw.getRootDir() + "/wwwroot"
2018-12-25 03:56:57 -05:00
search = request.args.get('search', '').strip().lower()
2022-12-03 11:57:03 -05:00
search_all = request.args.get('all', '').strip().lower()
2018-12-25 03:56:57 -05:00
page = request.args.get('p', '1').strip().lower()
row = request.args.get('row', '10')
order = request.form.get('order', '')
2019-03-19 05:26:14 -04:00
2022-12-03 11:57:03 -05:00
if search_all == 'yes' and search != '':
return self.getAllDir(path, int(page), int(row), order, search)
return self.getDir(path, int(page), int(row), order, search)
2019-01-18 04:11:29 -05:00
def createFileApi(self):
2021-05-01 22:23:02 -04:00
file = request.form.get('path', '')
2019-01-18 04:11:29 -05:00
try:
if not self.checkFileName(file):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件名中不能包含特殊字符!')
2019-01-18 04:11:29 -05:00
if os.path.exists(file):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件已存在!')
2019-01-18 04:11:29 -05:00
_path = os.path.dirname(file)
if not os.path.exists(_path):
os.makedirs(_path)
open(file, 'w+').close()
self.setFileAccept(file)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('创建文件[{1}]成功!', (file,))
mw.writeLog('文件管理', msg)
return mw.returnJson(True, '文件创建成功!')
2019-01-18 04:11:29 -05:00
except Exception as e:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '文件创建失败!')
2019-01-18 04:11:29 -05:00
def createDirApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2019-01-18 04:11:29 -05:00
try:
if not self.checkFileName(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '目录名中不能包含特殊字符!')
2019-01-18 04:11:29 -05:00
if os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定目录已存在!')
2019-01-18 04:11:29 -05:00
os.makedirs(path)
self.setFileAccept(path)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('创建目录[{1}]成功!', (path,))
mw.writeLog('文件管理', msg)
return mw.returnJson(True, '目录创建成功!')
2019-01-18 04:11:29 -05:00
except Exception as e:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '目录创建失败!')
2019-01-18 04:11:29 -05:00
def downloadFileApi(self):
import db
import time
2021-05-01 22:23:02 -04:00
url = request.form.get('url', '')
path = request.form.get('path', '')
filename = request.form.get('filename', '')
2019-02-19 04:29:07 -05:00
execstr = url + '|mw|' + path + '/' + filename
2022-07-08 11:40:14 -04:00
execstr = execstr.strip()
2020-07-10 03:57:25 -04:00
mw.M('tasks').add('name,type,status,addtime,execstr',
2022-07-08 11:40:14 -04:00
('下载文件[' + filename + ']', 'download', '-1', time.strftime('%Y-%m-%d %H:%M:%S'), execstr))
2019-02-19 04:29:07 -05:00
# self.setFileAccept(path + '/' + filename)
2022-07-08 11:40:14 -04:00
mw.triggerTask()
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '已将下载任务添加到队列!')
2019-02-16 05:49:23 -05:00
2022-06-28 08:05:45 -04:00
# 删除进程下的所有进程
def removeTaskRecursion(self, pid):
cmd = "ps -ef|grep " + pid + \
" | grep -v grep |sed -n '2,1p' | awk '{print $2}'"
sub_pid = mw.execShell(cmd)
if sub_pid[0].strip() == '':
return 'ok'
self.removeTaskRecursion(sub_pid[0].strip())
mw.execShell('kill -9 ' + sub_pid[0].strip())
return sub_pid[0].strip()
2019-02-19 05:59:27 -05:00
def removeTaskApi(self):
2022-06-28 08:05:45 -04:00
import system_api
2021-05-01 22:23:02 -04:00
mid = request.form.get('id', '')
2019-02-19 05:59:27 -05:00
try:
2020-07-10 03:57:25 -04:00
name = mw.M('tasks').where('id=?', (mid,)).getField('name')
status = mw.M('tasks').where('id=?', (mid,)).getField('status')
mw.M('tasks').delete(mid)
2019-02-19 05:59:27 -05:00
if status == '-1':
2022-06-28 08:05:45 -04:00
task_pid = mw.execShell(
"ps aux | grep 'task.py' | grep -v grep |awk '{print $2}'")
task_list = task_pid[0].strip().split("\n")
for i in range(len(task_list)):
self.removeTaskRecursion(task_list[i])
2022-06-30 07:19:55 -04:00
mw.triggerTask()
2022-06-28 08:05:45 -04:00
system_api.system_api().restartTask()
2019-02-19 05:59:27 -05:00
except:
2022-06-28 08:05:45 -04:00
system_api.system_api().restartTask()
2022-12-08 23:16:32 -05:00
# 删除日志
task_log = mw.getRunDir() + "/tmp/panelTask.pl"
if os.path.exists(task_log):
os.remove(task_log)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '任务已删除!')
2019-02-19 05:59:27 -05:00
2019-02-20 00:27:13 -05:00
def uploadFileApi(self):
2023-02-21 08:10:34 -05:00
# 上传文件
2019-02-20 00:27:13 -05:00
from werkzeug.utils import secure_filename
from flask import request
2021-05-01 22:23:02 -04:00
path = request.args.get('path', '')
2019-02-20 00:27:13 -05:00
if not os.path.exists(path):
os.makedirs(path)
f = request.files['zunfile']
filename = os.path.join(path, f.filename)
2021-05-01 22:23:02 -04:00
2019-02-20 00:27:13 -05:00
s_path = path
if os.path.exists(filename):
s_path = filename
p_stat = os.stat(s_path)
2023-02-21 08:10:34 -05:00
2023-02-22 01:28:01 -05:00
# print(filename)
2019-02-20 00:27:13 -05:00
f.save(filename)
os.chown(filename, p_stat.st_uid, p_stat.st_gid)
os.chmod(filename, p_stat.st_mode)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('上传文件[{1}] 到 [{2}]成功!', (filename, path))
mw.writeLog('文件管理', msg)
return mw.returnMsg(True, '上传成功!')
2019-02-20 00:27:13 -05:00
2023-02-22 01:28:01 -05:00
# 设置文件和目录权限
def setMode(self, path):
s_path = os.path.dirname(path)
p_stat = os.stat(s_path)
os.chown(path, p_stat.st_uid, p_stat.st_gid)
os.chmod(path, p_stat.st_mode)
2023-02-21 08:10:34 -05:00
def uploadSegmentApi(self):
# 分段上传
path = request.form.get('path', '')
name = request.form.get('name', '')
size = request.form.get('size')
start = request.form.get('start')
dir_mode = request.form.get('dir_mode', '')
file_mode = request.form.get('file_mode', '')
if not mw.fileNameCheck(name):
return mw.returnJson(False, '文件名中不能包含特殊字符!')
if path == '/':
return mw.returnJson(False, '不能直接上传文件到系统根目录!')
if name.find('./') != -1 or path.find('./') != -1:
return mw.returnJson(False, '错误的参数')
if not os.path.exists(path):
os.makedirs(path, 493)
if not dir_mode != '' or not file_mode != '':
mw.setMode(path)
save_path = os.path.join(
path, name + '.' + str(int(size)) + '.upload.tmp')
d_size = 0
if os.path.exists(save_path):
d_size = os.path.getsize(save_path)
if d_size != int(start):
2023-02-22 01:28:01 -05:00
return str(d_size)
2023-02-21 08:10:34 -05:00
f = open(save_path, 'ab')
b64_data = request.form.get('b64_data', '0')
if b64_data == '1':
import base64
b64_data = base64.b64decode(args.b64_data)
f.write(b64_data)
else:
upload_files = request.files.getlist("blob")
for tmp_f in upload_files:
f.write(tmp_f.read())
f.close()
f_size = os.path.getsize(save_path)
if f_size != int(size):
2023-02-22 01:28:01 -05:00
return str(f_size)
2023-02-21 08:10:34 -05:00
new_name = os.path.join(path, name)
if os.path.exists(new_name):
if new_name.find('.user.ini') != -1:
mw.execShell("chattr -i " + new_name)
try:
os.remove(new_name)
except:
mw.execShell("rm -f %s" % new_name)
os.renames(save_path, new_name)
if dir_mode != '' and dir_mode != '':
mode_tmp1 = dir_mode.split(',')
mw.setMode(path, mode_tmp1[0])
mw.setOwn(path, mode_tmp1[1])
mode_tmp2 = file_mode.split(',')
mw.setMode(new_name, mode_tmp2[0])
mw.setOwn(new_name, mode_tmp2[1])
else:
2023-02-22 01:28:01 -05:00
self.setMode(new_name)
2023-02-21 08:10:34 -05:00
2023-02-22 01:59:40 -05:00
msg = mw.getInfo('上传文件[{1}] 到 [{2}]成功!', (new_name, path))
2023-02-21 08:10:34 -05:00
mw.writeLog('文件管理', msg)
return mw.returnMsg(True, '上传成功!')
2019-02-16 05:49:23 -05:00
def getRecycleBinApi(self):
2019-02-16 07:40:58 -05:00
rPath = self.rPath
2019-02-16 05:49:23 -05:00
if not os.path.exists(rPath):
os.system('mkdir -p ' + rPath)
data = {}
data['dirs'] = []
data['files'] = []
data['status'] = os.path.exists('data/recycle_bin.pl')
data['status_db'] = os.path.exists('data/recycle_bin_db.pl')
for file in os.listdir(rPath):
try:
tmp = {}
fname = rPath + file
2019-02-16 07:40:58 -05:00
tmp1 = file.split('_mw_')
2019-02-16 05:49:23 -05:00
tmp2 = tmp1[len(tmp1) - 1].split('_t_')
tmp['rname'] = file
tmp['dname'] = file.replace('_mw_', '/').split('_t_')[0]
tmp['name'] = tmp2[0]
tmp['time'] = int(float(tmp2[1]))
if os.path.islink(fname):
filePath = os.readlink(fname)
link = ' -> ' + filePath
if os.path.exists(filePath):
tmp['size'] = os.path.getsize(filePath)
else:
tmp['size'] = 0
else:
tmp['size'] = os.path.getsize(fname)
if os.path.isdir(fname):
data['dirs'].append(tmp)
else:
data['files'].append(tmp)
2021-05-01 02:17:42 -04:00
except Exception as e:
2019-02-16 05:49:23 -05:00
continue
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', data)
2019-02-16 05:49:23 -05:00
# 回收站开关
def recycleBinApi(self):
c = 'data/recycle_bin.pl'
2021-05-01 22:23:02 -04:00
db = request.form.get('db', '')
2019-02-16 05:49:23 -05:00
if db != '':
c = 'data/recycle_bin_db.pl'
if os.path.exists(c):
os.remove(c)
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', '已关闭回收站功能!')
return mw.returnJson(True, '已关闭回收站功能!')
2019-02-16 05:49:23 -05:00
else:
2020-07-10 03:57:25 -04:00
mw.writeFile(c, 'True')
mw.writeLog('文件管理', '已开启回收站功能!')
return mw.returnJson(True, '已开启回收站功能!')
2019-02-16 05:49:23 -05:00
2019-02-16 07:40:58 -05:00
def reRecycleBinApi(self):
rPath = self.rPath
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2019-02-16 07:40:58 -05:00
dFile = path.replace('_mw_', '/').split('_t_')[0]
try:
import shutil
shutil.move(rPath + path, dFile)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('移动文件[{1}]到回收站成功!', (dFile,))
mw.writeLog('文件管理', msg)
return mw.returnJson(True, '恢复成功!')
2019-02-16 07:40:58 -05:00
except Exception as e:
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('从回收站恢复[{1}]失败!', (dFile,))
mw.writeLog('文件管理', msg)
return mw.returnJson(False, '恢复失败!')
2019-02-16 07:40:58 -05:00
def delRecycleBinApi(self):
rPath = self.rPath
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
empty = request.form.get('empty', '')
2019-02-16 07:40:58 -05:00
dFile = path.split('_t_')[0]
if not self.checkDir(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '敏感目录,请不要花样作死!')
2019-02-16 07:40:58 -05:00
2022-11-14 05:07:25 -05:00
mw.execShell('which chattr && chattr -R -i ' + rPath + path)
2019-02-16 07:40:58 -05:00
if os.path.isdir(rPath + path):
import shutil
shutil.rmtree(rPath + path)
else:
os.remove(rPath + path)
tfile = path.replace('_mw_', '/').split('_t_')[0]
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('已彻底从回收站删除{1}!', (tfile,))
mw.writeLog('文件管理', msg)
return mw.returnJson(True, msg)
2019-02-16 07:40:58 -05:00
2019-02-17 01:42:51 -05:00
# 获取进度
def getSpeedApi(self):
2020-07-10 03:57:25 -04:00
data = mw.getSpeed()
return mw.returnJson(True, '已清空回收站!', data)
2019-02-17 01:42:51 -05:00
def closeRecycleBinApi(self):
rPath = self.rPath
2022-11-14 05:07:09 -05:00
mw.execShell('which chattr && chattr -R -i ' + rPath)
2019-02-17 01:42:51 -05:00
rlist = os.listdir(rPath)
i = 0
l = len(rlist)
for name in rlist:
i += 1
path = rPath + name
2020-07-10 03:57:25 -04:00
mw.writeSpeed(name, i, l)
2019-02-17 01:42:51 -05:00
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
2020-07-10 03:57:25 -04:00
mw.writeSpeed(None, 0, 0)
mw.writeLog('文件管理', '已清空回收站!')
return mw.returnJson(True, '已清空回收站!')
2019-02-17 01:42:51 -05:00
2019-02-16 05:49:23 -05:00
def deleteDirApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
2019-02-16 05:49:23 -05:00
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件不存在!')
2019-02-16 05:49:23 -05:00
# 检查是否为.user.ini
if path.find('.user.ini'):
2022-06-15 00:31:15 -04:00
os.system("which chattr && chattr -i '" + path + "'")
2019-02-16 05:49:23 -05:00
try:
if os.path.exists('data/recycle_bin.pl'):
if self.mvRecycleBin(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '已将文件移动到回收站!')
mw.execShell('rm -rf ' + path)
mw.writeLog('文件管理', '删除文件成功!', (path,))
return mw.returnJson(True, '删除文件成功!')
2019-02-16 05:49:23 -05:00
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '删除文件失败!')
2019-02-16 05:49:23 -05:00
2019-02-20 22:23:01 -05:00
def closeLogsApi(self):
2020-07-10 03:57:25 -04:00
logPath = mw.getLogsDir()
2019-02-20 22:23:01 -05:00
os.system('rm -f ' + logPath + '/*')
2020-07-10 03:57:25 -04:00
os.system('kill -USR1 `cat ' + mw.getServerDir() +
2019-02-20 22:23:01 -05:00
'openresty/nginx/logs/nginx.pid`')
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', '网站日志已被清空!')
2019-02-20 22:23:01 -05:00
tmp = self.getDirSize(logPath)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, tmp[0].split()[0])
2019-02-20 22:23:01 -05:00
def setBatchDataApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
stype = request.form.get('type', '')
access = request.form.get('access', '')
user = request.form.get('user', '')
data = request.form.get('data')
if stype == '1' or stype == '2':
session['selected'] = {
2019-03-26 12:14:53 -04:00
'path': path,
'type': stype,
'access': access,
'user': user,
'data': data
}
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '标记成功,请在目标目录点击粘贴所有按钮!')
elif stype == '3':
for key in json.loads(data):
try:
2019-03-26 12:14:53 -04:00
filename = path + '/' + key
if not self.checkDir(filename):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, 'FILE_DANGER')
2019-03-26 12:14:53 -04:00
os.system('chmod -R ' + access + " '" + filename + "'")
os.system('chown -R ' + user + ':' +
user + " '" + filename + "'")
except:
2019-03-26 12:14:53 -04:00
continue
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', '批量设置权限成功!')
return mw.returnJson(True, '批量设置权限成功!')
else:
import shutil
isRecyle = os.path.exists('data/recycle_bin.pl')
data = json.loads(data)
2019-03-26 12:14:53 -04:00
l = len(data)
i = 0
for key in data:
try:
2021-05-01 22:23:02 -04:00
filename = path + '/' + key
2019-03-26 12:14:53 -04:00
topath = filename
if not os.path.exists(filename):
continue
2019-03-26 12:14:53 -04:00
i += 1
2020-07-10 03:57:25 -04:00
mw.writeSpeed(key, i, l)
if os.path.isdir(filename):
2019-03-26 12:14:53 -04:00
if not self.checkDir(filename):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '请不要花样作死!')
if isRecyle:
self.mvRecycleBin(topath)
else:
shutil.rmtree(filename)
else:
2019-03-26 12:14:53 -04:00
if key == '.user.ini':
os.system('which chattr && chattr -i ' + filename)
if isRecyle:
self.mvRecycleBin(topath)
else:
os.remove(filename)
except:
2019-03-26 12:14:53 -04:00
continue
2020-07-10 03:57:25 -04:00
mw.writeSpeed(None, 0, 0)
mw.writeLog('文件管理', '批量删除成功!')
return mw.returnJson(True, '批量删除成功!')
def checkExistsFilesApi(self):
2021-05-01 22:23:02 -04:00
dfile = request.form.get('dfile', '')
filename = request.form.get('filename', '')
2019-03-26 12:14:53 -04:00
data = []
filesx = []
if filename == '':
2019-03-26 12:14:53 -04:00
filesx = json.loads(session['selected']['data'])
else:
2019-03-26 12:14:53 -04:00
filesx.append(filename)
for fn in filesx:
2019-03-26 12:14:53 -04:00
if fn == '.':
continue
filename = dfile + '/' + fn
if os.path.exists(filename):
tmp = {}
stat = os.stat(filename)
tmp['filename'] = fn
2019-03-26 12:14:53 -04:00
tmp['size'] = os.path.getsize(filename)
tmp['mtime'] = str(int(stat.st_mtime))
data.append(tmp)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', data)
def batchPasteApi(self):
2021-05-01 22:23:02 -04:00
path = request.form.get('path', '')
stype = request.form.get('type', '')
# filename = request.form.get('filename', '')
import shutil
if not self.checkDir(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '请不要花样作死!')
2019-03-26 12:14:53 -04:00
i = 0
myfiles = json.loads(session['selected']['data'])
2019-03-26 12:14:53 -04:00
l = len(myfiles)
if stype == '1':
for key in myfiles:
i += 1
2020-07-10 03:57:25 -04:00
mw.writeSpeed(key, i, l)
try:
2019-03-26 12:14:53 -04:00
sfile = session['selected'][
2021-05-01 22:23:02 -04:00
'path'] + '/' + key
dfile = path + '/' + key
if os.path.isdir(sfile):
2019-03-26 12:14:53 -04:00
shutil.copytree(sfile, dfile)
else:
2019-03-26 12:14:53 -04:00
shutil.copyfile(sfile, dfile)
stat = os.stat(sfile)
2019-03-26 12:14:53 -04:00
os.chown(dfile, stat.st_uid, stat.st_gid)
except:
2019-03-26 12:14:53 -04:00
continue
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('从[{1}]批量复制到[{2}]成功',
(session['selected']['path'], path,))
mw.writeLog('文件管理', msg)
else:
for key in myfiles:
try:
i += 1
2020-07-10 03:57:25 -04:00
mw.writeSpeed(key, i, l)
2019-03-26 12:14:53 -04:00
sfile = session['selected'][
2021-05-01 22:23:02 -04:00
'path'] + '/' + key
dfile = path + '/' + key
2019-03-26 12:14:53 -04:00
shutil.move(sfile, dfile)
except:
2019-03-26 12:14:53 -04:00
continue
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('从[{1}]批量移动到[{2}]成功',
(session['selected']['path'], path,))
mw.writeLog('文件管理', msg)
mw.writeSpeed(None, 0, 0)
errorCount = len(myfiles) - i
del(session['selected'])
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('批量操作成功[{1}],失败[{2}]', (str(i), str(errorCount)))
return mw.returnJson(True, msg)
def copyFileApi(self):
2021-05-01 22:23:02 -04:00
sfile = request.form.get('sfile', '')
dfile = request.form.get('dfile', '')
if sfile == dfile:
return mw.returnJson(False, '源与目的一致!')
if not os.path.exists(sfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件不存在!')
2019-03-26 12:14:53 -04:00
if os.path.isdir(sfile):
2019-03-29 02:59:03 -04:00
return self.copyDir(sfile, dfile)
2019-03-26 12:14:53 -04:00
try:
import shutil
shutil.copyfile(sfile, dfile)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('复制文件[{1}]到[{2}]成功!', (sfile, dfile,))
mw.writeLog('文件管理', msg)
stat = os.stat(sfile)
os.chown(dfile, stat.st_uid, stat.st_gid)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '文件复制成功!')
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件复制失败!')
2019-01-10 12:45:04 -05:00
##### ----- end ----- ###
2018-12-25 03:56:57 -05:00
def copyDir(self, sfile, dfile):
if not os.path.exists(sfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定目录不存在!')
2019-03-26 12:14:53 -04:00
if os.path.exists(dfile):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定目录已存在!')
import shutil
try:
shutil.copytree(sfile, dfile)
stat = os.stat(sfile)
2019-03-26 12:14:53 -04:00
os.chown(dfile, stat.st_uid, stat.st_gid)
2020-07-10 03:57:25 -04:00
msg = mw.getInfo('复制目录[{1}]到[{2}]成功!', (sfile, dfile))
mw.writeLog('文件管理', msg)
return mw.returnJson(True, '目录复制成功!')
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '目录复制失败!')
2019-02-16 07:40:58 -05:00
# 检查敏感目录
def checkDir(self, path):
# path = str(path, encoding='utf-8')
2019-02-16 07:40:58 -05:00
path = path.replace('//', '/')
if path[-1:] == '/':
path = path[:-1]
nDirs = ('',
'/',
'/*',
'/www',
'/root',
'/boot',
'/bin',
'/etc',
'/home',
'/dev',
'/sbin',
'/var',
'/usr',
'/tmp',
'/sys',
'/proc',
'/media',
'/mnt',
'/opt',
'/lib',
'/srv',
'/selinux',
'/www/server',
2020-07-10 03:57:25 -04:00
mw.getRootDir())
2019-02-16 07:40:58 -05:00
return not path in nDirs
2019-02-20 22:23:01 -05:00
def getDirSize(self, path):
2023-08-22 06:48:07 -04:00
tmp = mw.execShell('du -sh ' + path)
2019-02-20 22:23:01 -05:00
return tmp
2019-01-18 04:11:29 -05:00
def checkFileName(self, filename):
# 检测文件名
nots = ['\\', '&', '*', '|', ';']
if filename.find('/') != -1:
filename = filename.split('/')[-1]
for n in nots:
if n in filename:
return False
return True
2018-12-11 01:49:26 -05:00
def setFileAccept(self, filename):
auth = 'www:www'
2020-07-10 03:57:25 -04:00
if mw.getOs() == 'darwin':
user = mw.execShell(
2018-12-11 01:49:26 -05:00
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
auth = user + ':staff'
2022-11-21 08:15:10 -05:00
2018-12-11 01:49:26 -05:00
os.system('chown -R ' + auth + ' ' + filename)
os.system('chmod -R 755 ' + filename)
# 移动到回收站
def mvRecycleBin(self, path):
2019-02-16 07:40:58 -05:00
rPath = self.rPath
2018-12-11 01:49:26 -05:00
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)
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', mw.getInfo(
2018-12-11 01:49:26 -05:00
'移动文件[{1}]到回收站成功!', (path)))
return True
except:
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', mw.getInfo(
2018-12-11 01:49:26 -05:00
'移动文件[{1}]到回收站失败!', (path)))
return False
2018-12-11 00:29:27 -05:00
def getBody(self, path):
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件不存在', (path,))
2018-12-11 00:29:27 -05:00
if os.path.getsize(path) > 2097152:
return mw.returnJson(False, '不能在线编辑大于2MB的文件!')
2018-12-11 00:29:27 -05:00
2023-01-29 11:50:10 -05:00
if os.path.isdir(path):
return mw.returnJson(False, '这不是一个文件!')
2018-12-11 00:29:27 -05:00
fp = open(path, 'rb')
data = {}
data['status'] = True
2023-01-29 11:50:10 -05:00
if fp:
srcBody = fp.read()
fp.close()
2023-01-29 11:50:10 -05:00
encoding_list = ['utf-8', 'GBK', 'BIG5']
for el in encoding_list:
2018-12-11 00:29:27 -05:00
try:
2023-01-29 11:50:10 -05:00
data['encoding'] = el
data['data'] = srcBody.decode(data['encoding'])
break
except Exception as ex:
if el == 'BIG5':
return mw.returnJson(False, '文件编码不被兼容,无法正确读取文件!' + str(ex))
else:
2023-01-29 12:17:09 -05:00
return mw.returnJson(False, '文件未正常打开!')
2018-12-11 00:29:27 -05:00
2023-01-29 11:50:10 -05:00
return mw.returnJson(True, 'OK', data)
2018-12-11 00:29:27 -05:00
def saveBody(self, path, data, encoding='utf-8'):
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件不存在')
2018-12-11 00:29:27 -05:00
try:
if encoding == 'ascii':
encoding = 'utf-8'
2023-01-29 11:50:10 -05:00
data = data.encode(
encoding, errors='ignore').decode(encoding)
fp = open(path, 'w+', encoding=encoding)
2018-12-11 00:29:27 -05:00
fp.write(data)
fp.close()
2021-11-12 07:25:08 -05:00
if path.find("web_conf") > 0:
mw.restartWeb()
2023-10-11 13:46:01 -04:00
mw.writeLog('文件管理', '文件[{1}]保存成功', (path,))
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '文件保存成功')
2018-12-11 00:29:27 -05:00
except Exception as ex:
return mw.returnJson(False, '文件保存错误:' + str(ex))
2018-12-11 00:29:27 -05:00
2018-12-11 01:25:12 -05:00
def zip(self, sfile, dfile, stype, path):
if sfile.find(',') == -1:
if not os.path.exists(path + '/' + sfile):
2020-07-10 03:57:25 -04:00
return mw.returnMsg(False, '指定文件不存在!')
2018-12-11 01:25:12 -05:00
try:
2020-07-10 03:57:25 -04:00
tmps = mw.getRunDir() + '/tmp/panelExec.log'
2018-12-11 01:25:12 -05:00
if stype == 'zip':
2022-11-21 08:15:10 -05:00
mw.execShell("cd '" + path + "' && zip '" + dfile +
"' -r '" + sfile + "' > " + tmps + " 2>&1")
2018-12-11 01:25:12 -05:00
else:
sfiles = ''
for sfile in sfile.split(','):
if not sfile:
continue
sfiles += " '" + sfile + "'"
2022-11-21 08:15:10 -05:00
mw.execShell("cd '" + path + "' && tar -zcvf '" +
dfile + "' " + sfiles + " > " + tmps + " 2>&1")
2018-12-11 01:25:12 -05:00
self.setFileAccept(dfile)
2023-10-11 13:46:32 -04:00
mw.writeLog("文件管理", '文件[{1}]压缩[{2}]成功!', (sfile, dfile))
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '文件压缩成功!')
2018-12-11 01:25:12 -05:00
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '文件压缩失败!')
2018-12-11 01:25:12 -05:00
2023-09-14 12:01:37 -04:00
def uncompress(self, sfile, dfile, path):
if not os.path.exists(sfile):
return mw.returnJson(False, '指定文件不存在!')
filename = os.path.basename(sfile)
extension = os.path.splitext(filename)[-1]
extension = extension.strip('.')
2022-06-15 00:31:15 -04:00
2023-09-14 12:37:23 -04:00
tar_gz = 'tar.gz'
tar_gz_len = len(tar_gz)
suffix_gz = sfile[-tar_gz_len:]
if suffix_gz == tar_gz:
extension = suffix_gz
if not extension in ['tar.gz', 'gz', 'zip', 'rar']:
2023-09-14 12:13:05 -04:00
return mw.returnJson(False, '现在仅支持gz,zip,rar格式解压!')
if mw.isAppleSystem() and extension == 'rar':
return mw.returnJson(False, 'macosx暂时不支持rar格式解压')
2023-09-14 12:01:37 -04:00
try:
tmps = mw.getRunDir() + '/tmp/panelExec.log'
if extension == 'zip':
cmd = "cd " + path + " && unzip -o -d '" + dfile + \
"' '" + sfile + "' > " + tmps + " 2>&1 &"
mw.execShell(cmd)
2023-09-14 12:37:23 -04:00
if extension == 'tar.gz':
2023-09-14 12:01:37 -04:00
cmd = "cd " + path + " && tar -zxvf " + sfile + \
" -C " + dfile + " > " + tmps + " 2>&1 &"
mw.execShell(cmd)
2023-09-14 12:37:23 -04:00
if extension == 'gz':
cmd = "cd " + path + " && gunzip -k " + sfile + " > " + tmps + " 2>&1 &"
mw.execShell(cmd)
2023-09-14 12:01:37 -04:00
if extension == 'rar':
2023-09-14 12:08:13 -04:00
cmd = "cd " + path + " && unrar x " + sfile + \
" " + dfile + " > " + tmps + " 2>&1 &"
2023-09-14 12:01:37 -04:00
mw.execShell(cmd)
if os.path.exists(dfile):
self.setFileAccept(dfile)
2023-10-11 13:46:55 -04:00
mw.writeLog("文件管理", '文件[{1}]解压[{2}]成功!', (sfile, dfile))
2023-09-14 12:01:37 -04:00
return mw.returnJson(True, '文件解压成功!')
except Exception as e:
return mw.returnJson(False, '文件解压失败!:' + str(e))
def unzip(self, sfile, dfile, stype, path):
2022-06-15 00:31:15 -04:00
if not os.path.exists(sfile):
2023-09-14 12:01:37 -04:00
return mw.returnJson(False, '指定文件不存在!')
2022-06-14 23:41:14 -04:00
try:
tmps = mw.getRunDir() + '/tmp/panelExec.log'
if stype == 'zip':
2022-11-21 08:19:05 -05:00
mw.execShell("cd " + path + " && unzip -o -d '" + dfile +
"' '" + sfile + "' > " + tmps + " 2>&1 &")
2022-06-14 23:41:14 -04:00
else:
sfiles = ''
for sfile in sfile.split(','):
if not sfile:
continue
sfiles += " '" + sfile + "'"
2022-11-21 08:19:05 -05:00
mw.execShell("cd " + path + " && tar -zxvf " + sfiles +
" -C " + dfile + " > " + tmps + " 2>&1 &")
2022-11-21 08:15:10 -05:00
2022-06-14 23:41:14 -04:00
self.setFileAccept(dfile)
2023-10-11 13:47:21 -04:00
mw.writeLog("文件管理", '文件[{1}]解压[{2}]成功!', (sfile, dfile))
2022-06-14 23:41:14 -04:00
return mw.returnJson(True, '文件解压成功!')
except:
return mw.returnJson(False, '文件解压失败!')
2018-12-11 01:49:26 -05:00
def delete(self, path):
if not os.path.exists(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '指定文件不存在!')
2018-12-11 01:49:26 -05:00
# 检查是否为.user.ini
if path.find('.user.ini') >= 0:
2022-11-14 05:06:52 -05:00
mw.execShell("which chattr && chattr -i '" + path + "'")
2018-12-11 01:49:26 -05:00
try:
if os.path.exists('data/recycle_bin.pl'):
if self.mvRecycleBin(path):
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '已将文件移动到回收站!')
2018-12-11 01:49:26 -05:00
os.remove(path)
2020-07-10 03:57:25 -04:00
mw.writeLog('文件管理', mw.getInfo(
2018-12-11 01:49:26 -05:00
'删除文件[{1}]成功!', (path)))
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, '删除文件成功!')
2018-12-11 01:49:26 -05:00
except:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '删除文件失败!')
2018-12-11 01:49:26 -05:00
2018-12-11 21:31:01 -05:00
def getAccess(self, filename):
data = {}
try:
stat = os.stat(filename)
data['chmod'] = str(oct(stat.st_mode)[-3:])
data['chown'] = pwd.getpwuid(stat.st_uid).pw_name
except:
data['chmod'] = 755
data['chown'] = 'www'
return data
2024-05-11 13:10:04 -04:00
def getSysUserList(self):
pwd_file = '/etc/passwd'
if os.path.exists(pwd_file):
content = mw.readFile(pwd_file)
clist = content.split('\n')
sys_users = []
for line in clist:
if line.find(":")<0:
continue
lines = line.split(":",1)
sys_users.append(lines[0])
return sys_users
return ['root','mysql','www']
2018-12-11 01:49:26 -05:00
# 计算文件数量
2018-12-11 21:31:01 -05:00
def getCount(self, path, search):
2018-12-11 00:29:27 -05:00
i = 0
for name in os.listdir(path):
if search:
if name.lower().find(search) == -1:
continue
2022-12-03 11:57:03 -05:00
if name == '.':
continue
2018-12-11 00:29:27 -05:00
i += 1
return i
def getAllDir(self, path, page=1, page_size=10, order='', search=None):
2022-12-03 11:57:03 -05:00
# print("search:", search)
data = {}
dirnames = []
filenames = []
info = {}
i = 0
n = 0
2022-12-03 11:57:03 -05:00
count = 0
max_limit = 3000
order_arr = order.split(' ')
if len(order_arr) < 2:
plist = mw.sortAllFileList(path, order_arr[0],'',search, max_limit)
else:
plist = mw.sortAllFileList(path, order_arr[0], order_arr[1], search,max_limit)
info['count'] = len(plist)
info['row'] = page_size
info['p'] = page
info['tojs'] = 'getFiles'
pageObj = mw.getPageObject(info, '1,2,3,4,5,6,7,8')
data['PAGE'] = pageObj[0]
for dst_file in plist:
if not os.path.exists(dst_file):
continue
i += 1
if n >= pageObj[1].ROW:
break
if i < pageObj[1].SHIFT:
continue
if os.path.isdir(dst_file):
dirnames.append(self.__get_stats(dst_file, path))
else:
filenames.append(self.__get_stats(dst_file, path))
n += 1
data['DIR'] = dirnames
data['FILES'] = filenames
data['PATH'] = path.replace('//', '/')
return mw.getJson(data)
#备份
def getAllDirBk(self, path, page=1, page_size=10, order='', search=None):
data = {}
dirnames = []
filenames = []
2022-12-03 11:57:03 -05:00
count = 0
max_limit = 3000
2022-12-03 11:57:03 -05:00
for d_list in os.walk(path):
2022-12-03 11:57:03 -05:00
if count >= max_limit:
break
for d in d_list[1]:
if count >= max_limit:
break
if d.lower().find(search) != -1:
filename = d_list[0] + '/' + d
if not os.path.exists(filename):
continue
dirnames.append(self.__get_stats(filename, path))
count += 1
for f in d_list[2]:
if count >= max_limit:
break
if f.lower().find(search) != -1:
filename = d_list[0] + '/' + f
if not os.path.exists(filename):
continue
filenames.append(self.__get_stats(filename, path))
count += 1
data['DIR'] = sorted(dirnames)
data['FILES'] = sorted(filenames)
data['PATH'] = path.replace('//', '/')
info = {}
info['count'] = len(dirnames) + len(filenames)
info['row'] = page_size
info['p'] = page
info['tojs'] = 'getFiles'
pageObj = mw.getPageObject(info, '1,2,3,4,5,6,7,8')
data['PAGE'] = pageObj[0]
return mw.getJson(data)
def getDir(self, path, page=1, page_size=10, order = '', search=None):
2018-12-11 00:29:27 -05:00
data = {}
dirnames = []
filenames = []
info = {}
2018-12-11 21:31:01 -05:00
info['count'] = self.getCount(path, search)
2018-12-11 00:29:27 -05:00
info['row'] = page_size
info['p'] = page
info['tojs'] = 'getFiles'
2020-07-10 03:57:25 -04:00
pageObj = mw.getPageObject(info, '1,2,3,4,5,6,7,8')
2018-12-11 00:29:27 -05:00
data['PAGE'] = pageObj[0]
i = 0
n = 0
order_arr = order.split(' ')
if len(order_arr) < 2:
plist = mw.sortFileList(path, order_arr[0],'')
else:
plist = mw.sortFileList(path, order_arr[0],order_arr[1])
for filename in plist:
2018-12-11 00:29:27 -05:00
if search:
if filename.lower().find(search) == -1:
continue
i += 1
if n >= pageObj[1].ROW:
break
if i < pageObj[1].SHIFT:
continue
try:
2021-05-01 22:23:02 -04:00
filePath = path + '/' + filename
2022-12-03 11:57:03 -05:00
if not os.path.exists(filePath):
continue
2021-05-01 07:06:21 -04:00
2022-12-03 11:57:03 -05:00
file_stats = self.__get_stats(filePath, path)
2018-12-11 00:29:27 -05:00
if os.path.isdir(filePath):
2022-12-03 11:57:03 -05:00
dirnames.append(file_stats)
2018-12-11 00:29:27 -05:00
else:
2022-12-03 11:57:03 -05:00
filenames.append(file_stats)
2018-12-11 00:29:27 -05:00
n += 1
2021-05-01 02:17:42 -04:00
except Exception as e:
2018-12-11 00:29:27 -05:00
continue
data['DIR'] = dirnames
data['FILES'] = filenames
2021-05-01 22:23:02 -04:00
data['PATH'] = path.replace('//', '/')
2021-05-01 07:06:21 -04:00
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2022-12-03 11:57:03 -05:00
2023-02-22 07:54:54 -05:00
def execShellApi(self):
2023-02-22 07:52:12 -05:00
# 执行SHELL命令
shell = request.form.get('shell', '').strip()
path = request.form.get('path', '').strip()
disabled = ['vi', 'vim', 'top', 'passwd', 'su']
tmp = shell.split(' ')
if tmp[0] in disabled:
return mw.returnJson(False, '禁止执行[{}]'.format(tmp[0]))
shellStr = '''#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cd %s
%s
''' % (path, shell)
mw.writeFile('/tmp/panelShell.sh', shellStr)
mw.execShell(
'nohup bash /tmp/panelShell.sh > /tmp/panelShell.pl 2>&1 &')
return mw.returnJson(True, 'ok')
2023-02-22 07:54:54 -05:00
def getExecShellMsgApi(self):
2023-02-22 07:52:12 -05:00
# 取SHELL执行结果
fileName = '/tmp/panelShell.pl'
if not os.path.exists(fileName):
return ''
status = not mw.processExists('bash', None, '/tmp/panelShell.sh')
return mw.returnJson(status, mw.getNumLines(fileName, 200))
2022-12-03 11:57:03 -05:00
def __get_stats(self, filename, path=None):
# print(filename,path)
2022-12-06 07:37:07 -05:00
filename = filename.replace('//', '/')
2022-12-03 11:57:03 -05:00
try:
stat = os.stat(filename)
accept = str(oct(stat.st_mode)[-3:])
mtime = str(int(stat.st_mtime))
user = ''
try:
user = str(pwd.getpwuid(stat.st_uid).pw_name)
except:
user = str(stat.st_uid)
size = str(stat.st_size)
link = ''
if os.path.islink(filename):
link = ' -> ' + os.readlink(filename)
2022-12-06 07:19:06 -05:00
2022-12-03 11:57:03 -05:00
if path:
tmp_path = (path + '/').replace('//', '/')
2022-12-06 07:37:07 -05:00
filename = filename.replace(tmp_path, '', 1)
2022-12-06 07:19:06 -05:00
2022-12-03 11:57:03 -05:00
return filename + ';' + size + ';' + mtime + ';' + accept + ';' + user + ';' + link
except Exception as e:
# print(e)
return ';;;;;'