-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleFileServer.py
More file actions
167 lines (144 loc) · 5.63 KB
/
SimpleFileServer.py
File metadata and controls
167 lines (144 loc) · 5.63 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
from flask import abort
from FILEIO.TreeRead import *
from flask import make_response, jsonify
# We'll render HTML templates and access data sent by POST
# using the request object from flask. Redirect and url_for
# will be used to redirect the user once the upload is done
# and send_from_directory will help us to send/show on the
# browser the file that the user just uploaded
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s (%(threadName)-2s) %(message)s',
)
# Initialize the Flask application
app = Flask(__name__)
# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = 'uploads/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','zip'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/', methods=['GET'])
def index():
return listDir("")
@app.route('/list/<relpath>', methods=['GET'])
def listDir(relpath):
relativePath=relpath
#Can only list the root now
directory = os.path.dirname(os.path.abspath(__file__))+"/" + app.config['UPLOAD_FOLDER']
#list directory in unix form is safer
directory = directory.replace("\\","/")
logging.debug("show directory:"+directory)
if("region" in request.form ):
region = request.form["region"]
else:
region=""
print ("region:"+region)
#else:
# region=""
if(len(relpath) == 0 ):
uploadURL=url_for('upload')
else:
uploadURL=url_for('upload_Path', relpath=relpath, _external=True)
try:
dlist = listPath(directory,relativePath)
except:
abort (404)
for node in dlist:
if node.isFile:
url=url_for('get_file', filename=node.name, _external=True)
else:
url=url_for('listDir', relpath=node.name, _external=True)
if(len(relativePath)>0) :
url=url +"?relpath="+relativePath
node.setURLPath(url)
return render_template('index.html',CurrentPath=relativePath,uploadurl=uploadURL,filelst=dlist,SelectedRegion=region)
@app.route('/upload', methods=['POST'])
def upload():
if("region" in request.form ):
region = request.form["region"]
else:
region=""
print ("regionupload:"+region)
return upload_Path("")
# Route that will process the file upload
@app.route('/uploadpath/<relpath>', methods=['POST'])
def upload_Path(relpath):
abspath=app.config['UPLOAD_FOLDER']
if( len(relpath)>0):
abspath = os.path.join(abspath, relpath)
logging.debug("upload to:"+abspath)
# Get the name of the uploaded file
file = request.files['file']
if("region" in request.form ):
region = request.form["region"]
else:
region=""
print ("regionupload:"+region)
directory = os.path.dirname(os.path.abspath(__file__))+"/" + app.config['UPLOAD_FOLDER']
# Check if the file is one of the allowed types/extensions
#if file and allowed_file(file.filename):
if file :
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(directory, filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
#return redirect(url_for('uploaded_file', filename=filename))
return listDir(relpath)
@app.route('/uploadPlain', methods=['POST'])
def uploadPlain():
abspath=app.config['UPLOAD_FOLDER']
logging.debug("upload to:"+abspath)
# Get the name of the uploaded file
file = request.files['file']
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(abspath, filename))
return "OK"
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
@app.route('/getfile/<filename>', methods=['GET'])
def get_file(filename):
relpath=""
abspath=app.config['UPLOAD_FOLDER']
if('relpath' in request.args ):
#logging.debug("Get File Relative path:"+request.args["relpath"])
relpath=request.args["relpath"]
abspath=os.path.join(abspath,relpath)
directory = os.path.dirname(os.path.abspath(__file__))+"/" + app.config['UPLOAD_FOLDER']
#list directory in unix form is safer
directory = directory.replace("\\","/")
file_path=os.path.join(directory,filename);
logging.debug(file_path)
if (os.path.exists(file_path) ):
return send_from_directory(directory,filename)
else:
abort (404)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
app.run(
host= '0.0.0.0',
port=int("8080"),
debug=True
)