-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (74 loc) · 2.21 KB
/
main.py
File metadata and controls
81 lines (74 loc) · 2.21 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
from flask import *
import boto3
import os
from dotenv import load_dotenv
from botocore.config import Config
load_dotenv()
url = os.environ.get("URL")
cdn_name = "cdnv2"
uploads_url = url + "uploads/"
dd_key = os.environ.get("DD_KEY")
data = {
"Version": "14.0.0",
"Name": "PrimittCDN",
"DestinationType": "ImageUploader, FileUploader",
"RequestMethod": "POST",
"Parameters": {
"apikey": "[REPLACE WITH API KEY]"
},
"RequestURL": url + "upload",
"Body": "MultipartFormData",
"FileFormName": "file",
"URL": "{json:data.url}"
}
s3 = boto3.client(
service_name="s3",
endpoint_url='<your own was URL>',
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"),
region_name="auto", # Must be one of: wnam, enam, weur, eeur, apac, auto
config=Config(
s3={
'request_checksum_calculation': 'WHEN_REQUIRED',
'response_checksum_validation': 'WHEN_REQUIRED'
}
)
)
def gen_filename():
import uuid
return str(uuid.uuid4())
app = Flask(__name__)
if not os.path.exists("uploads"):
os.mkdir("uploads")
if not os.path.exists("files"):
os.mkdir("files")
@app.route('/')
def index():
return "Hello World!"
@app.route("/upload", methods=['POST'])
def upload():
if request.method == 'POST':
get_dd_key = request.args.get("apikey")
if not get_dd_key or get_dd_key != dd_key:
return "Invalid API Key", 403
f = request.files['file']
uid = gen_filename()
file_name = uid + "." + f.filename.split(".")[-1]
s3.upload_fileobj(f, "cdnvstore", file_name)
return {"data": {"url": uploads_url + file_name}}
else:
return "Upload Failed"
@app.route("/config")
def config():
return Response(
json.dumps(data),
mimetype='application/json',
headers={"Content-disposition":f"attachment; filename={cdn_name}.sxcu"}
)
@app.route("/uploads/<filename>")
def get_image(filename):
if not os.path.exists("uploads/" + filename):
s3.download_file("cdnvstore", filename, "uploads/" + filename)
return send_file("uploads/" + filename)
if __name__ == '__main__':
app.run(debug=True, port=5002)