file.py 13.6 KB
Newer Older
wanli's avatar
wanli committed
1 2 3
'''
Author: your name
Date: 2021-07-09 12:39:40
4
LastEditTime: 2021-07-19 09:32:48
wanli's avatar
wanli committed
5 6 7 8
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\views\file.py
'''
9

10
import traceback
11
from pathlib import Path
12
from flask import current_app, jsonify, request, make_response, Response
13 14
from flask_restful import Resource, fields
from marshmallow import Schema, fields
15
from werkzeug.datastructures import FileStorage
wanli's avatar
wanli committed
16 17 18 19 20 21 22 23 24 25 26
from flask_restful.reqparse import RequestParser
from flask_jwt_extended import ( jwt_required, get_jwt_identity )
from application.signal_manager import signalManager
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result

class FileInit(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

27
    @jwt_required(locations=["headers"])
wanli's avatar
wanli committed
28 29 30 31 32 33 34
    def get(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
35 36
            jwt = get_jwt_identity()
            result, message = signalManager.actionGetFileInit.emit(jwt)
wanli's avatar
wanli committed
37
            if result:
38
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
39
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
40 41
        except Exception as e:
            current_app.logger.error(e)
42
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
43 44 45 46 47


class FileContent(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
48
        self.parser = RequestParser()
wanli's avatar
wanli committed
49 50

    @jwt_required(locations=["headers"])
51
    def get(self):
wanli's avatar
wanli committed
52
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
53 54 55
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=False, default=None)
        args = self.parser.parse_args()
wanli's avatar
wanli committed
56 57

        try:
58 59
            result, message = signalManager.actionGetFileContent.emit(args.disk, args.path)
            if result:
60 61 62
                response = { 'result': { 'message': None, 'status': "success" } }
                response.update(result)
                return response
63
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
64
        except Exception as e:
65
            traceback.print_exc()
wanli's avatar
wanli committed
66
            current_app.logger.error(e)
67
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
68

69
class FileTree(Resource):
wanli's avatar
wanli committed
70 71
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
72
        self.parser = RequestParser()
wanli's avatar
wanli committed
73

74
    @jwt_required(locations=["headers"])
wanli's avatar
wanli committed
75 76
    def get(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
77
        self.parser.add_argument("disk", type=str, location="args", required=True)
78
        self.parser.add_argument("path", type=str, location="args", required=False)
79
        args = self.parser.parse_args()
wanli's avatar
wanli committed
80 81

        try:
82
            result, message = signalManager.actionGetFileTree.emit(args.disk, args.path)
83
            if result:
84
                logger.info(result)
85 86 87
                response = { 'result': { 'message': None, 'status': "success" }, 'directories': None }
                response.update({ 'directories': result })
                return response
88
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
89
        except Exception as e:
90
            traceback.print_exc()
wanli's avatar
wanli committed
91
            current_app.logger.error(e)
92
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
93

94
class FileDisk(Resource):
wanli's avatar
wanli committed
95 96
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
97
        self.parser = RequestParser()
wanli's avatar
wanli committed
98

99
    def get(self):
wanli's avatar
wanli committed
100
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=False)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFileDisk.emit(args.disk)
            if result:
                return { 'information': list(message), 'result': { 'message': "Disk selected!", 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FileUpdate(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

    def post(self):
120 121 122
        # logger.info(request.form)
        self.parser.add_argument("disk", type=str, location="form", required=True)
        self.parser.add_argument("path", type=str, location="form", nullable=False, required=True)
123 124 125 126
        self.parser.add_argument("file", type=FileStorage, location="files", required=True)
        args = self.parser.parse_args()

        try:
127
            # logger.info(args.path)
128 129 130
            file = request.files.get("file") # args.get('file')
            result, message = signalManager.actionPostFileUpdate.emit(args.disk, args.path, file)
            if result:
131
                return { 'config': result, 'result': { 'message': "file update success", 'status': "success" } }
132 133 134 135 136 137 138 139 140 141
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FileUpload(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

142 143 144 145 146
    def post(self):
        self.parser.add_argument("disk", type=str, location="form", required=True)
        self.parser.add_argument("path", type=str, location="form", required=True)
        self.parser.add_argument("overwrite", type=int, location="form", default=0, required=True)
        self.parser.add_argument("files", type=FileStorage, location="files", required=True, action='append')
147
        args = self.parser.parse_args()
wanli's avatar
wanli committed
148 149

        try:
150
            fileList = request.files.getlist('files')
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            result, message = signalManager.actionPostFileUpload.emit(args.disk, args.path, args.overwrite, fileList)
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FileDelete(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

    def post(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="json", required=True)
        # self.parser.add_argument("items", type=fields.Raw, location="json", required=True) # items: [{path: "code/app.php", type: "file"}]
        args = self.parser.parse_args()

        try:
            json_payload = request.json
            logger.info(json_payload)
            if not json_payload:
                return False, ResponseCode.HTTP_INVAILD_REQUEST
            result, message = signalManager.actionPostFileDelete.emit(**json_payload)
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            traceback.print_exc()
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FileCreate(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

    def post(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="json", required=True)
        self.parser.add_argument("path", type=str, location="json", required=True)
        self.parser.add_argument("name", type=str, location="json", required=True)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionPostFileCreate.emit(args.disk, args.path, args.name)
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FileCreateDir(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

    def post(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="json", required=True)
        self.parser.add_argument("path", type=str, location="json", required=True)
        self.parser.add_argument("name", type=str, location="json", required=True)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionPostFileCreateDir.emit(args.disk, args.path, args.name)
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

class FilePaste(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser = RequestParser()

    def post(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="json", required=True)
        self.parser.add_argument("clipboard", type=dict, location="json", required=True)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionPostFilePaste.emit(args.disk, args.items)
239
            if result:
240 241
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
242 243
        except Exception as e:
            current_app.logger.error(e)
244
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
245 246 247 248

class FileDownload(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
249
        self.parser = RequestParser()
wanli's avatar
wanli committed
250 251 252

    def get(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
253 254 255
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        args = self.parser.parse_args()
wanli's avatar
wanli committed
256 257

        try:
258 259 260 261 262
            result, message = signalManager.actionGetFileDown.emit(args.disk, args.path)
            if result:
                resp, mime = result
                # return Response(resp, mimetype='text/xml')
                return Response(resp, content_type='{}; charset=utf-8'.format(mime))
263
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
264
        except Exception as e:
265
            traceback.print_exc()
wanli's avatar
wanli committed
266
            current_app.logger.error(e)
267
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
268 269 270 271 272


class FilePrview(Resource):
    def __init__(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
273
        self.parser = RequestParser()
wanli's avatar
wanli committed
274 275

    @jwt_required(locations=["headers"])
276
    def get(self):
wanli's avatar
wanli committed
277
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
278 279 280
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        args = self.parser.parse_args()
wanli's avatar
wanli committed
281 282

        try:
283 284 285 286 287 288
            result, message = signalManager.actionGetFilePreview.emit(args.disk, args.path)
            if result:
                content, mime = result
                resp = make_response(content)
                resp.mimetype = mime
                return resp
289
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
wanli's avatar
wanli committed
290
        except Exception as e:
291
            traceback.print_exc()
wanli's avatar
wanli committed
292
            current_app.logger.error(e)
293
            return response_result(ResponseCode.HTTP_SERVER_ERROR)