-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
75 lines (52 loc) · 1.75 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
from flask import Flask, request, send_file, redirect
from os import mkdir, path, getenv
from utils import authorization_is_valid, upload_image, shorten, get_original_url
# Create image folder if it doesn't exist
try:
mkdir("images")
except FileExistsError:
pass
app = Flask(__name__)
# Get MAX_UPLOAD_SIZE and set it to MAX_CONTENT_LENGTH for security reasons
app.config["MAX_CONTENT_LENGTH"] = (int(getenv("MAX_UPLOAD_SIZE")) if getenv("MAX_UPLOAD_SIZE") else 10) * 1024 * 1024
@app.route("/", methods=["GET"])
def home():
return "This is an instance of the ShareX Uploader made by Jasper, you can find the repository here: https://github.com/j4asper/ShareX-Uploader"
##################
# View Image #
##################
@app.route("/<text>", methods=["GET"])
def get_file_or_short_url(text):
if "." in text:
if path.exists(f"./images/{text}"):
return send_file(f"./images/{text}")
else:
return "Image not found", 404
else:
original_url = get_original_url(text)
if original_url:
return redirect(original_url)
else:
return "Short URL not found", 404
##################
# Upload Image #
##################
@app.route("/upload", methods=["POST"])
def upload():
if authorization_is_valid(request):
filename = upload_image(request)
return filename, 200
else:
return "Not Authorized", 401
###################
# URL Shortener #
###################
@app.route("/shorten", methods=["POST"])
def shorten_url():
if authorization_is_valid(request):
short_url_code = shorten(request)
return short_url_code, 200
else:
return "Not Authorized", 401
if __name__ == "__main__":
app.run(debug=True)