Update app.py
Browse files
app.py
CHANGED
@@ -12,17 +12,9 @@ import time
|
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
-
# In-memory store: { id: { data,
|
16 |
SECRETS = {}
|
17 |
-
|
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,28 +22,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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
sid = str(uuid.uuid4())
|
47 |
SECRETS[sid] = {
|
48 |
"data": data,
|
49 |
-
"
|
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,25 +56,15 @@ def fetch(sid):
|
|
64 |
del SECRETS[sid]
|
65 |
return jsonify({"error": "Expired"}), 410
|
66 |
|
67 |
-
|
68 |
-
secret
|
69 |
-
|
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."
|
|
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
14 |
|
15 |
+
# In-memory store: { id: { data, image (optional), expire_at, view_once } }
|
16 |
SECRETS = {}
|
17 |
+
MAX_IMAGE_SIZE = 300 * 1024 # 300 KB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
@app.route("/api/store", methods=["POST"])
|
20 |
def store():
|
|
|
22 |
data = form.get("data")
|
23 |
ttl = int(form.get("ttl", 300))
|
24 |
view_once = form.get("view_once") == "true"
|
|
|
25 |
|
26 |
+
# Handle image if present
|
27 |
+
image_file = request.files.get("image")
|
28 |
+
image_data = None
|
29 |
+
|
30 |
+
if image_file:
|
31 |
+
img_bytes = image_file.read()
|
32 |
+
if len(img_bytes) > MAX_IMAGE_SIZE:
|
33 |
+
image = Image.open(io.BytesIO(img_bytes))
|
34 |
+
image.thumbnail((1024, 1024)) # Resize for safety
|
35 |
+
output = io.BytesIO()
|
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 |
+
"image": image_data,
|
|
|
45 |
"expire_at": time.time() + ttl,
|
46 |
+
"view_once": view_once
|
|
|
|
|
47 |
}
|
48 |
return jsonify({"id": sid})
|
49 |
|
|
|
56 |
del SECRETS[sid]
|
57 |
return jsonify({"error": "Expired"}), 410
|
58 |
|
59 |
+
response = {"data": secret["data"]}
|
60 |
+
if secret.get("image"):
|
61 |
+
response["image"] = secret["image"]
|
|
|
|
|
|
|
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."
|