Skip to content

Commit c3d4b67

Browse files
committed
minor changes
1 parent fe6dafe commit c3d4b67

File tree

2 files changed

+66
-64
lines changed

2 files changed

+66
-64
lines changed

file_server.py

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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
23
from datetime import datetime
34
import humanize
45
import os
@@ -94,67 +95,68 @@ def get_range(request):
9495
else:
9596
return 0, None
9697

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)
150130
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)
159161

160162
app.run('0.0.0.0', 8000, threaded=True, debug=False)

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<div class="row">
1515
<div class="col-xs-12">
1616
<ol class="breadcrumb" dir="ltr">
17-
<li class="breadcrumb-item"><a href="{{url_for('v_get_path')}}"><i class="fa fa-fw fa-home fa-lg"></i> </a></li>
17+
<li class="breadcrumb-item"><a href="{{url_for('path_view')}}"><i class="fa fa-fw fa-home fa-lg"></i> </a></li>
1818
{% for part in path.strip('/').split('/') %}
19-
<li class="breadcrumb-item"><a href="{{url_for('v_get_path', p='/'+path[:path.find(part)+part|length]+'/')}}"><strong>{{ part }}</strong></a></li>
19+
<li class="breadcrumb-item"><a href="{{url_for('path_view', p='/'+path[:path.find(part)+part|length]+'/')}}"><strong>{{ part }}</strong></a></li>
2020
{% endfor %}
2121
</ol>
2222
</div>

0 commit comments

Comments
 (0)