mike23415 commited on
Commit
beb9cca
·
verified ·
1 Parent(s): 61e6238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -12,17 +12,9 @@ import time
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,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
- 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,25 +56,15 @@ def fetch(sid):
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."
 
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."