-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·173 lines (132 loc) · 4.13 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
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
168
169
170
171
172
## ShareX-Custom-Uploader
## v1.1
## by dev_sda1
## GitHub: https://github.com/dev-sda1/ShareX-Uploader
## License: GPL-3.0
## app.py
## Imports
import json
import os
import string
import random
from os import access
from flask import Flask,redirect
from flask import jsonify
from flask import request
from flask_restful import Api, Resource, reqparse
from werkzeug.utils import send_from_directory
## Vars
app = Flask(__name__)
api = Api(app)
def fileHandler(request):
uploadedFiles = request.files.getlist('sharex')
for file in uploadedFiles:
print(file.filename)
#file.save(file.filename)
print(uploadedFiles)
for file in uploadedFiles:
# Generate a string for the file name
# Get the file extension
fileExt = file.filename.split('.')[-1]
print(fileExt)
l = string.ascii_lowercase
fileName = file.filename
fileName = random_string(6)+"."+fileExt
file.save(os.path.join("./uploads/", fileName))
#print("Uploaded " + file.filename)
# Return the file name
return jsonify(
{"file": fileName}
)
def urlHandler(request):
url = request.form.get('url')
# Check if urls.json exists in uploads folder if not create it
if not os.path.exists("./uploads/urls.json"):
with open("./uploads/urls.json", "w") as f:
f.write("{}")
# Read the urls.json file
with open("./uploads/urls.json", "r") as f:
data = json.load(f)
id = random_string(6)
data[id] = url
# Write the urls.json file
with open("./uploads/urls.json", "w") as f:
json.dump(data, f)
# Return the id
return jsonify(
{"file": "go/"+id}
)
def random_string(length): # Generate a random string for use in URLs.
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
@app.post('/upload')
def uploadContent():
print("OwO an upload")
r = None
reqParams = request.form
#print(request.files)
with open('server/config.json') as json_file:
data = json.load(json_file)
secret = data['secret']
if reqParams['secret'] == secret: ## Secret has been matched, upload can commence
print("Secret Matched")
if(request.form['url']) != '':
print("URL Shortening")
r = urlHandler(request)
else:
print("File upload")
r = fileHandler(request)
else: ## Secret did not match, 403.
return(
jsonify(
{"error": "Invalid secret."}
)
),403
return(
r
)
@app.route("/<file_name>")
def callback(file_name):
fn = file_name
print("They are looking for: "+fn)
if fn == "":
return jsonify(
{"error": "No file specified."}
),400
else:
##Look for the file in folder set in config.json and return it
if access(os.path.join("./uploads/")+fn, os.F_OK):
print("Found file")
# Return the file as an attachment with environ
return send_from_directory("./uploads/", fn, environ=request.environ)
else:
return jsonify(
{"error": "File not found."}
),404
# Shortened URLs
@app.route("/go/<url_id>")
def url_route(url_id):
url = url_id
print("Looking for: "+url)
if url == "":
return jsonify({
"error": "No URL specified."
}),400
else:
## Look for the URL in the urls.json file and return the file
with open("./uploads/urls.json", "r") as f:
data = json.load(f)
if url in data:
print("Found URL")
# Redirect browser to the URL
return redirect(data[url], code=302)
else:
return jsonify(
{"error": "URL not found."}
),404
if __name__ == '__main__':
with open('server/config.json') as json_file:
data = json.load(json_file)
portNumber = data['webPort']
app.run(debug=True,host="0.0.0.0")