This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (79 loc) · 2.52 KB
/
main.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
from flask import Flask, redirect, send_file, request, jsonify
from models import InstallID
import requests
import boto3
import random
import string
import botocore
# Imports go here.
app = Flask(__name__)
# Defines the app.
@app.route("/")
def go_to_website():
"""Returns to magiccap.me"""
return redirect("https://magiccap.me")
@app.route("/upload", methods=["POST"])
def upload():
"""Used to upload to i.magiccap"""
try:
auth_header = request.headers['Authorization']
except KeyError:
resp = jsonify({
"error": "No authorization header present."
})
resp.status_code = 400
return resp
auth_header_split = auth_header.split(" ")
if len(auth_header_split) != 2:
resp = jsonify({
"error": "Invalid authorization header present."
})
resp.status_code = 400
return resp
try:
InstallID.get(auth_header_split[1])
except InstallID.DoesNotExist:
resp = jsonify({
"error": "Invalid installation ID."
})
resp.status_code = 400
return resp
try:
file = request.files['data']
except KeyError:
resp = jsonify({
"error": "No data found."
})
resp.status_code = 400
return resp
filename = ''.join([random.choice(string.ascii_lowercase) for i in range(8)])
ext = file.filename.split(".").pop().lower()
boto3.resource("s3").Object("i.magiccap.me", f"{filename}.{ext}").put(Body=file, ContentType=file.content_type)
return jsonify({
"url": f"https://i.magiccap.me/{filename}.{ext}"
})
@app.route("/<path:subpath>")
def image_view(subpath):
"""Loads a image view."""
s3 = boto3.resource("s3")
try:
obj = s3.Object("i.magiccap.me", subpath)
obj = obj.get()
except botocore.exceptions.ClientError:
r = requests.get(
"https://hosting.novuscommunity.co/" + subpath,
headers={
"Host": "i.magiccap.me"
},
verify=False
)
try:
r.raise_for_status()
except requests.exceptions.HTTPError:
return "Not found.", 404
obj.put(Body=r.content, ContentType=r.headers['Content-Type'])
obj = s3.Object("i.magiccap.me", subpath).get()
return send_file(obj['Body'], attachment_filename=subpath.split("/").pop(), mimetype=obj['ContentType'])
if __name__ == "__main__":
app.run(port=7575, debug=True)
# Starts the app if this isn't a Lambda instance.