Update app.py
Browse files
app.py
CHANGED
@@ -12,9 +12,17 @@ import time
|
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
-
# In-memory store: { id: { data,
|
16 |
SECRETS = {}
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
@app.route("/api/store", methods=["POST"])
|
20 |
def store():
|
@@ -22,28 +30,28 @@ def store():
|
|
22 |
data = form.get("data")
|
23 |
ttl = int(form.get("ttl", 300))
|
24 |
view_once = form.get("view_once") == "true"
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
if len(
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
image.save(output, format="JPEG", optimize=True, quality=70)
|
37 |
-
image_data = base64.b64encode(output.getvalue()).decode("utf-8")
|
38 |
-
else:
|
39 |
-
image_data = base64.b64encode(img_bytes).decode("utf-8")
|
40 |
|
41 |
sid = str(uuid.uuid4())
|
42 |
SECRETS[sid] = {
|
43 |
"data": data,
|
44 |
-
"
|
|
|
45 |
"expire_at": time.time() + ttl,
|
46 |
-
"view_once": view_once
|
|
|
|
|
47 |
}
|
48 |
return jsonify({"id": sid})
|
49 |
|
@@ -56,18 +64,28 @@ def fetch(sid):
|
|
56 |
del SECRETS[sid]
|
57 |
return jsonify({"error": "Expired"}), 410
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
|
63 |
if secret["view_once"]:
|
64 |
del SECRETS[sid]
|
65 |
|
66 |
return jsonify(response)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
@app.route("/")
|
69 |
def index():
|
70 |
return "Sharelock Flask backend is running."
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
+
# In-memory store: { id: { data, file (optional), expire_at, view_once, views, created_at, ip } }
|
16 |
SECRETS = {}
|
17 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
18 |
+
MAX_IMAGE_SIZE = 300 * 1024 # 300KB
|
19 |
+
|
20 |
+
def compress_image(img_bytes):
|
21 |
+
image = Image.open(io.BytesIO(img_bytes))
|
22 |
+
image.thumbnail((1024, 1024))
|
23 |
+
output = io.BytesIO()
|
24 |
+
image.save(output, format="JPEG", optimize=True, quality=70)
|
25 |
+
return output.getvalue()
|
26 |
|
27 |
@app.route("/api/store", methods=["POST"])
|
28 |
def store():
|
|
|
30 |
data = form.get("data")
|
31 |
ttl = int(form.get("ttl", 300))
|
32 |
view_once = form.get("view_once") == "true"
|
33 |
+
uploaded_file = request.files.get("image")
|
34 |
|
35 |
+
file_data = None
|
36 |
+
file_type = None
|
37 |
+
if uploaded_file:
|
38 |
+
file_bytes = uploaded_file.read()
|
39 |
+
if len(file_bytes) > MAX_FILE_SIZE:
|
40 |
+
return jsonify({"error": "File too large (5MB max)"}), 400
|
41 |
+
if uploaded_file.mimetype.startswith("image/") and len(file_bytes) > MAX_IMAGE_SIZE:
|
42 |
+
file_bytes = compress_image(file_bytes)
|
43 |
+
file_data = base64.b64encode(file_bytes).decode("utf-8")
|
44 |
+
file_type = uploaded_file.mimetype
|
|
|
|
|
|
|
|
|
45 |
|
46 |
sid = str(uuid.uuid4())
|
47 |
SECRETS[sid] = {
|
48 |
"data": data,
|
49 |
+
"file": file_data,
|
50 |
+
"file_type": file_type,
|
51 |
"expire_at": time.time() + ttl,
|
52 |
+
"view_once": view_once,
|
53 |
+
"created_at": time.time(),
|
54 |
+
"views": [],
|
55 |
}
|
56 |
return jsonify({"id": sid})
|
57 |
|
|
|
64 |
del SECRETS[sid]
|
65 |
return jsonify({"error": "Expired"}), 410
|
66 |
|
67 |
+
client_ip = request.remote_addr
|
68 |
+
secret["views"].append({"ip": client_ip, "ts": time.time()})
|
69 |
+
response = {"data": secret["data"], "created_at": secret["created_at"], "views": secret["views"]}
|
70 |
+
if secret.get("file"):
|
71 |
+
response["file"] = secret["file"]
|
72 |
+
response["file_type"] = secret["file_type"]
|
73 |
|
74 |
if secret["view_once"]:
|
75 |
del SECRETS[sid]
|
76 |
|
77 |
return jsonify(response)
|
78 |
|
79 |
+
@app.route("/api/burn/<sid>", methods=["DELETE"])
|
80 |
+
def burn(sid):
|
81 |
+
if sid in SECRETS:
|
82 |
+
del SECRETS[sid]
|
83 |
+
return jsonify({"status": "burned"})
|
84 |
+
return jsonify({"error": "Not found"}), 404
|
85 |
+
|
86 |
@app.route("/")
|
87 |
def index():
|
88 |
return "Sharelock Flask backend is running."
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
+
app.run(host="0.0.0.0", port=7860)
|