-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
52 lines (42 loc) · 1.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, render_template, request, abort, send_file
from werkzeug import secure_filename
import os
app = Flask(__name__)
server_path = "/Users/manikant/Documents/blob_storage/server/"
local_path = "/Users/manikant/Documents/blob_storage/local/"
@app.route('/upload')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def uploader_file():
if request.method == 'POST':
f = request.files['file']
f.save(os.path.join(server_path, secure_filename(f.filename)))
return 'file uploaded successfully'
@app.route('/files/', defaults={'req_path': ''})
@app.route('/files/<path:req_path>')
def dir_listing(req_path):
BASE_DIR = server_path
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR, req_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
print(abs_path)
f = send_file(abs_path)
content = f.response.file.read()
print(str(content))
f = open(os.path.join(local_path, os.path.basename(abs_path)), "wb")
f.write((content))
f.close()
# print("MMMM" + str(f.response.file.read()))
dir = os.path.dirname(abs_path)
files = os.listdir(dir)
return render_template('files.html', files=files)
# Show directory contents
files = os.listdir(abs_path)
return render_template('files.html', files=files)
if __name__ == '__main__':
app.run(debug = True)