|
1 | | -from flask import Flask, make_response, request, session, render_template, send_file, stream_with_context, Response |
| 1 | +from flask import Flask, make_response, request, session, render_template, send_file, Response |
| 2 | +from flask.views import MethodView |
2 | 3 | from datetime import datetime |
3 | 4 | import humanize |
4 | 5 | import os |
@@ -94,67 +95,68 @@ def get_range(request): |
94 | 95 | else: |
95 | 96 | return 0, None |
96 | 97 |
|
97 | | -@app.route('/') |
98 | | -@app.route('/<path:p>') |
99 | | -def v_get_path(p=''): |
100 | | - hide_dotfile = request.args.get('hide-dotfile', request.cookies.get('hide-dotfile', 'no')) |
101 | | - |
102 | | - path = os.path.join(root, p) |
103 | | - if os.path.isdir(path): |
104 | | - contents = [] |
105 | | - total = {'size': 0, 'dir': 0, 'file': 0} |
106 | | - for filename in os.listdir(path): |
107 | | - if filename in ignored: |
108 | | - continue |
109 | | - if hide_dotfile == 'yes' and filename[0] == '.': |
110 | | - continue |
111 | | - filepath = os.path.join(path, filename) |
112 | | - stat_res = os.stat(filepath) |
113 | | - info = {} |
114 | | - info['name'] = filename |
115 | | - info['mtime'] = stat_res.st_mtime |
116 | | - ft = get_type(stat_res.st_mode) |
117 | | - info['type'] = ft |
118 | | - total[ft] += 1 |
119 | | - sz = stat_res.st_size |
120 | | - info['size'] = sz |
121 | | - total['size'] += sz |
122 | | - contents.append(info) |
123 | | - page = render_template('index.html', path=p, contents=contents, total=total, hide_dotfile=hide_dotfile) |
124 | | - res = make_response(page, 200) |
125 | | - res.set_cookie('hide-dotfile', hide_dotfile, max_age=16070400) |
126 | | - elif os.path.isfile(path): |
127 | | - if 'Range' in request.headers: |
128 | | - start, end = get_range(request) |
129 | | - res = partial_response(path, start, end) |
130 | | - else: |
131 | | - res = send_file(path) |
132 | | - res.headers.add('Content-Disposition', 'attachment') |
133 | | - else: |
134 | | - res = make_response('Not found', 404) |
135 | | - return res |
136 | | - |
137 | | -@app.route('/', methods=['POST']) |
138 | | -@app.route('/<path:p>', methods=['POST']) |
139 | | -def save_files(p=''): |
140 | | - path = os.path.join(root, p) |
141 | | - info = {} |
142 | | - if os.path.isdir(path): |
143 | | - files = request.files.getlist('files[]') |
144 | | - for file in files: |
145 | | - try: |
146 | | - file.save(os.path.join(path, file.filename)) |
147 | | - except Exception as e: |
148 | | - info['status'] = 'error' |
149 | | - info['msg'] = str(e) |
| 98 | +class PathView(MethodView): |
| 99 | + def get(self, p=''): |
| 100 | + hide_dotfile = request.args.get('hide-dotfile', request.cookies.get('hide-dotfile', 'no')) |
| 101 | + |
| 102 | + path = os.path.join(root, p) |
| 103 | + if os.path.isdir(path): |
| 104 | + contents = [] |
| 105 | + total = {'size': 0, 'dir': 0, 'file': 0} |
| 106 | + for filename in os.listdir(path): |
| 107 | + if filename in ignored: |
| 108 | + continue |
| 109 | + if hide_dotfile == 'yes' and filename[0] == '.': |
| 110 | + continue |
| 111 | + filepath = os.path.join(path, filename) |
| 112 | + stat_res = os.stat(filepath) |
| 113 | + info = {} |
| 114 | + info['name'] = filename |
| 115 | + info['mtime'] = stat_res.st_mtime |
| 116 | + ft = get_type(stat_res.st_mode) |
| 117 | + info['type'] = ft |
| 118 | + total[ft] += 1 |
| 119 | + sz = stat_res.st_size |
| 120 | + info['size'] = sz |
| 121 | + total['size'] += sz |
| 122 | + contents.append(info) |
| 123 | + page = render_template('index.html', path=p, contents=contents, total=total, hide_dotfile=hide_dotfile) |
| 124 | + res = make_response(page, 200) |
| 125 | + res.set_cookie('hide-dotfile', hide_dotfile, max_age=16070400) |
| 126 | + elif os.path.isfile(path): |
| 127 | + if 'Range' in request.headers: |
| 128 | + start, end = get_range(request) |
| 129 | + res = partial_response(path, start, end) |
150 | 130 | else: |
151 | | - info['status'] = 'success' |
152 | | - info['msg'] = 'File Saved' |
153 | | - else: |
154 | | - info['status'] = 'error' |
155 | | - info['msg'] = 'Invalid Operation' |
156 | | - res = make_response(json.JSONEncoder().encode(info), 200) |
157 | | - res.headers.add('Content-type', 'application/json') |
158 | | - return res |
| 131 | + res = send_file(path) |
| 132 | + res.headers.add('Content-Disposition', 'attachment') |
| 133 | + else: |
| 134 | + res = make_response('Not found', 404) |
| 135 | + return res |
| 136 | + |
| 137 | + def post(self, p=''): |
| 138 | + path = os.path.join(root, p) |
| 139 | + info = {} |
| 140 | + if os.path.isdir(path): |
| 141 | + files = request.files.getlist('files[]') |
| 142 | + for file in files: |
| 143 | + try: |
| 144 | + file.save(os.path.join(path, file.filename)) |
| 145 | + except Exception as e: |
| 146 | + info['status'] = 'error' |
| 147 | + info['msg'] = str(e) |
| 148 | + else: |
| 149 | + info['status'] = 'success' |
| 150 | + info['msg'] = 'File Saved' |
| 151 | + else: |
| 152 | + info['status'] = 'error' |
| 153 | + info['msg'] = 'Invalid Operation' |
| 154 | + res = make_response(json.JSONEncoder().encode(info), 200) |
| 155 | + res.headers.add('Content-type', 'application/json') |
| 156 | + return res |
| 157 | + |
| 158 | +path_view = PathView.as_view('path_view') |
| 159 | +app.add_url_rule('/', view_func=path_view) |
| 160 | +app.add_url_rule('/<path:p>', view_func=path_view) |
159 | 161 |
|
160 | 162 | app.run('0.0.0.0', 8000, threaded=True, debug=False) |
0 commit comments