veltrixcode commited on
Commit
33656b6
·
verified ·
1 Parent(s): cb6f208

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -18
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import http.server
2
  import socketserver
3
- import ssl
4
  import uuid
5
  import json
6
  import http.client
@@ -47,30 +46,33 @@ class Handler(http.server.BaseHTTPRequestHandler):
47
  self.send_error(404)
48
  return
49
 
50
- content_type = self.headers.get("Content-Type")
51
- boundary = content_type.split("boundary=")[1].encode()
 
 
52
 
53
- length = int(self.headers.get("Content-Length"))
 
54
  body = self.rfile.read(length)
55
 
 
56
  parts = body.split(b"--" + boundary)
57
- image_data = None
58
 
59
- for p in parts:
60
- if b"filename=" in p:
61
- image_data = p.split(b"\r\n\r\n", 1)[1].rsplit(b"\r\n", 1)[0]
62
  break
63
 
64
- if not image_data:
65
  self.send_error(400)
66
  return
67
 
68
- self.forward_to_imgbb(image_data)
69
 
70
  def forward_to_imgbb(self, image_bytes):
71
  boundary = "----WebKitFormBoundary" + uuid.uuid4().hex
72
 
73
- head = (
74
  f"--{boundary}\r\n"
75
  f"Content-Disposition: form-data; name=\"type\"\r\n\r\n"
76
  f"file\r\n"
@@ -82,29 +84,51 @@ class Handler(http.server.BaseHTTPRequestHandler):
82
  f"Content-Type: image/jpeg\r\n\r\n"
83
  ).encode()
84
 
85
- tail = f"\r\n--{boundary}--\r\n".encode()
86
 
87
  conn = http.client.HTTPSConnection("imgbb.com")
88
  conn.request(
89
  "POST",
90
  "/json",
91
- body=head + image_bytes + tail,
92
  headers={
93
  "Content-Type": f"multipart/form-data; boundary={boundary}",
94
- "Content-Length": str(len(head) + len(image_bytes) + len(tail)),
95
  "Accept": "application/json",
96
  "User-Agent": "Mozilla/5.0"
97
  }
98
  )
99
 
100
  resp = conn.getresponse()
101
- data = resp.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  self.send_response(200)
104
  self.send_header("Content-Type", "application/json")
105
  self.end_headers()
106
- self.wfile.write(data)
107
 
108
- with socketserver.TCPServer(("", PORT), Handler) as httpd:
109
  print(f"Running → http://localhost:{PORT}")
110
- httpd.serve_forever()
 
1
  import http.server
2
  import socketserver
 
3
  import uuid
4
  import json
5
  import http.client
 
46
  self.send_error(404)
47
  return
48
 
49
+ content_type = self.headers.get("Content-Type", "")
50
+ if "boundary=" not in content_type:
51
+ self.send_error(400)
52
+ return
53
 
54
+ boundary = content_type.split("boundary=")[1].encode()
55
+ length = int(self.headers.get("Content-Length", 0))
56
  body = self.rfile.read(length)
57
 
58
+ image_bytes = None
59
  parts = body.split(b"--" + boundary)
 
60
 
61
+ for part in parts:
62
+ if b"filename=" in part:
63
+ image_bytes = part.split(b"\r\n\r\n", 1)[1].rsplit(b"\r\n", 1)[0]
64
  break
65
 
66
+ if not image_bytes:
67
  self.send_error(400)
68
  return
69
 
70
+ self.forward_to_imgbb(image_bytes)
71
 
72
  def forward_to_imgbb(self, image_bytes):
73
  boundary = "----WebKitFormBoundary" + uuid.uuid4().hex
74
 
75
+ body_start = (
76
  f"--{boundary}\r\n"
77
  f"Content-Disposition: form-data; name=\"type\"\r\n\r\n"
78
  f"file\r\n"
 
84
  f"Content-Type: image/jpeg\r\n\r\n"
85
  ).encode()
86
 
87
+ body_end = f"\r\n--{boundary}--\r\n".encode()
88
 
89
  conn = http.client.HTTPSConnection("imgbb.com")
90
  conn.request(
91
  "POST",
92
  "/json",
93
+ body=body_start + image_bytes + body_end,
94
  headers={
95
  "Content-Type": f"multipart/form-data; boundary={boundary}",
96
+ "Content-Length": str(len(body_start) + len(image_bytes) + len(body_end)),
97
  "Accept": "application/json",
98
  "User-Agent": "Mozilla/5.0"
99
  }
100
  )
101
 
102
  resp = conn.getresponse()
103
+ raw = resp.read()
104
+
105
+ try:
106
+ imgbb = json.loads(raw.decode())
107
+ except:
108
+ self.send_error(500)
109
+ return
110
+
111
+ if imgbb.get("status_code") == 200:
112
+ image = imgbb.get("image", {})
113
+ output = {
114
+ "success": True,
115
+ "url": image.get("display_url") or image.get("url"),
116
+ "delete_url": image.get("delete_url"),
117
+ "width": image.get("display_width"),
118
+ "height": image.get("display_height"),
119
+ "size": image.get("size")
120
+ }
121
+ else:
122
+ output = {
123
+ "success": False,
124
+ "error": imgbb.get("status_txt", "Upload failed")
125
+ }
126
 
127
  self.send_response(200)
128
  self.send_header("Content-Type", "application/json")
129
  self.end_headers()
130
+ self.wfile.write(json.dumps(output).encode())
131
 
132
+ with socketserver.TCPServer(("", PORT), Handler) as server:
133
  print(f"Running → http://localhost:{PORT}")
134
+ server.serve_forever()