Spaces:
Running
Running
Commit
·
159d092
1
Parent(s):
8b5c234
[Cursor] Simplify serverless handler
Browse files- api/index.py +174 -112
api/index.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
-
from http.server import BaseHTTPRequestHandler
|
2 |
import json
|
3 |
import os
|
4 |
import tempfile
|
5 |
import magic
|
6 |
import requests
|
7 |
-
from werkzeug.utils import secure_filename
|
8 |
import datetime
|
9 |
|
10 |
def is_valid_file(file_data):
|
@@ -60,102 +58,164 @@ def download_file(url):
|
|
60 |
except Exception as e:
|
61 |
raise ValueError(f"Failed to download file: {str(e)}")
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
}
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
}
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
fd, temp_path = tempfile.mkstemp()
|
109 |
-
os.close(fd)
|
110 |
-
|
111 |
-
try:
|
112 |
-
with open(temp_path, 'wb') as f:
|
113 |
-
f.write(post_data)
|
114 |
-
|
115 |
-
self.send_response(200)
|
116 |
-
self.send_header('Content-type', 'application/json')
|
117 |
-
self.end_headers()
|
118 |
-
response = {
|
119 |
"status": "success",
|
120 |
"message": "File processed successfully",
|
121 |
"metadata": {
|
122 |
"size": os.path.getsize(temp_path),
|
123 |
"mime_type": magic.from_file(temp_path, mime=True)
|
124 |
}
|
125 |
-
}
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
os.unlink(temp_path)
|
130 |
-
except:
|
131 |
-
pass
|
132 |
-
|
133 |
-
except Exception as e:
|
134 |
-
self.send_error(500, str(e))
|
135 |
-
|
136 |
-
elif self.path == '/parse/url':
|
137 |
-
try:
|
138 |
try:
|
139 |
-
|
140 |
except:
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
"status": "success",
|
160 |
"message": "URL processed successfully",
|
161 |
"metadata": {
|
@@ -163,29 +223,31 @@ class Handler(BaseHTTPRequestHandler):
|
|
163 |
"content_type": content_type,
|
164 |
"size": os.path.getsize(temp_path)
|
165 |
}
|
166 |
-
}
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
self.send_error(400, str(e))
|
176 |
-
except Exception as e:
|
177 |
-
self.send_error(500, str(e))
|
178 |
-
|
179 |
else:
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
import os
|
3 |
import tempfile
|
4 |
import magic
|
5 |
import requests
|
|
|
6 |
import datetime
|
7 |
|
8 |
def is_valid_file(file_data):
|
|
|
58 |
except Exception as e:
|
59 |
raise ValueError(f"Failed to download file: {str(e)}")
|
60 |
|
61 |
+
def handler(event, context):
|
62 |
+
"""Serverless handler for Vercel"""
|
63 |
+
|
64 |
+
# Add CORS headers to all responses
|
65 |
+
cors_headers = {
|
66 |
+
"Access-Control-Allow-Origin": "*",
|
67 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
68 |
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
69 |
+
"Content-Type": "application/json"
|
70 |
+
}
|
71 |
+
|
72 |
+
# Handle OPTIONS request for CORS
|
73 |
+
if event.get('httpMethod') == 'OPTIONS':
|
74 |
+
return {
|
75 |
+
"statusCode": 204,
|
76 |
+
"headers": cors_headers,
|
77 |
+
"body": ""
|
78 |
+
}
|
79 |
+
|
80 |
+
# Get path and method
|
81 |
+
path = event.get('path', '/').rstrip('/')
|
82 |
+
method = event.get('httpMethod', 'GET')
|
83 |
+
|
84 |
+
try:
|
85 |
+
# Route request
|
86 |
+
if path == '' or path == '/':
|
87 |
+
return {
|
88 |
+
"statusCode": 200,
|
89 |
+
"headers": cors_headers,
|
90 |
+
"body": json.dumps({
|
91 |
+
"status": "ok",
|
92 |
+
"message": "Document Processing API",
|
93 |
+
"version": "1.0.0"
|
94 |
+
})
|
95 |
}
|
96 |
+
|
97 |
+
elif path == '/health':
|
98 |
+
return {
|
99 |
+
"statusCode": 200,
|
100 |
+
"headers": cors_headers,
|
101 |
+
"body": json.dumps({
|
102 |
+
"status": "healthy",
|
103 |
+
"timestamp": str(datetime.datetime.now(datetime.UTC))
|
104 |
+
})
|
105 |
}
|
106 |
+
|
107 |
+
elif path == '/parse/file' and method == 'POST':
|
108 |
+
# Check if there's a file in the request
|
109 |
+
if 'body' not in event or not event['body']:
|
110 |
+
return {
|
111 |
+
"statusCode": 400,
|
112 |
+
"headers": cors_headers,
|
113 |
+
"body": json.dumps({
|
114 |
+
"error": "No file provided",
|
115 |
+
"details": "Please provide a file in the request body"
|
116 |
+
})
|
117 |
+
}
|
118 |
+
|
119 |
+
# Get file data
|
120 |
+
file_data = event['body']
|
121 |
+
if event.get('isBase64Encoded', False):
|
122 |
+
import base64
|
123 |
+
file_data = base64.b64decode(file_data)
|
124 |
+
|
125 |
+
# Validate file type
|
126 |
+
if not is_valid_file(file_data):
|
127 |
+
return {
|
128 |
+
"statusCode": 400,
|
129 |
+
"headers": cors_headers,
|
130 |
+
"body": json.dumps({
|
131 |
+
"error": "Invalid file type",
|
132 |
+
"details": "Supported formats: PDF, TXT, HTML, MD, DOCX"
|
133 |
+
})
|
134 |
+
}
|
135 |
+
|
136 |
+
# Save to temp file
|
137 |
+
fd, temp_path = tempfile.mkstemp()
|
138 |
+
os.close(fd)
|
139 |
+
|
140 |
try:
|
141 |
+
with open(temp_path, 'wb') as f:
|
142 |
+
f.write(file_data)
|
143 |
+
|
144 |
+
# Process file here
|
145 |
+
return {
|
146 |
+
"statusCode": 200,
|
147 |
+
"headers": cors_headers,
|
148 |
+
"body": json.dumps({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
"status": "success",
|
150 |
"message": "File processed successfully",
|
151 |
"metadata": {
|
152 |
"size": os.path.getsize(temp_path),
|
153 |
"mime_type": magic.from_file(temp_path, mime=True)
|
154 |
}
|
155 |
+
})
|
156 |
+
}
|
157 |
+
finally:
|
158 |
+
# Clean up temp file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
try:
|
160 |
+
os.unlink(temp_path)
|
161 |
except:
|
162 |
+
pass
|
163 |
+
|
164 |
+
elif path == '/parse/url' and method == 'POST':
|
165 |
+
# Get request body
|
166 |
+
if 'body' not in event or not event['body']:
|
167 |
+
return {
|
168 |
+
"statusCode": 400,
|
169 |
+
"headers": cors_headers,
|
170 |
+
"body": json.dumps({
|
171 |
+
"error": "No request body",
|
172 |
+
"details": "Please provide a URL in the request body"
|
173 |
+
})
|
174 |
+
}
|
175 |
+
|
176 |
+
# Parse JSON body
|
177 |
+
try:
|
178 |
+
body = json.loads(event['body'])
|
179 |
+
except:
|
180 |
+
return {
|
181 |
+
"statusCode": 400,
|
182 |
+
"headers": cors_headers,
|
183 |
+
"body": json.dumps({
|
184 |
+
"error": "Invalid JSON",
|
185 |
+
"details": "Request body must be valid JSON"
|
186 |
+
})
|
187 |
+
}
|
188 |
+
|
189 |
+
# Get URL from body
|
190 |
+
url = body.get('url')
|
191 |
+
if not url:
|
192 |
+
return {
|
193 |
+
"statusCode": 400,
|
194 |
+
"headers": cors_headers,
|
195 |
+
"body": json.dumps({
|
196 |
+
"error": "No URL provided",
|
197 |
+
"details": "Please provide a URL in the request body"
|
198 |
+
})
|
199 |
+
}
|
200 |
+
|
201 |
+
if not url.startswith(('http://', 'https://')):
|
202 |
+
return {
|
203 |
+
"statusCode": 400,
|
204 |
+
"headers": cors_headers,
|
205 |
+
"body": json.dumps({
|
206 |
+
"error": "Invalid URL",
|
207 |
+
"details": "URL must start with http:// or https://"
|
208 |
+
})
|
209 |
+
}
|
210 |
+
|
211 |
+
# Download and process file
|
212 |
+
temp_path, content_type = download_file(url)
|
213 |
+
try:
|
214 |
+
# Process file here
|
215 |
+
return {
|
216 |
+
"statusCode": 200,
|
217 |
+
"headers": cors_headers,
|
218 |
+
"body": json.dumps({
|
219 |
"status": "success",
|
220 |
"message": "URL processed successfully",
|
221 |
"metadata": {
|
|
|
223 |
"content_type": content_type,
|
224 |
"size": os.path.getsize(temp_path)
|
225 |
}
|
226 |
+
})
|
227 |
+
}
|
228 |
+
finally:
|
229 |
+
# Clean up temp file
|
230 |
+
try:
|
231 |
+
os.unlink(temp_path)
|
232 |
+
except:
|
233 |
+
pass
|
234 |
+
|
|
|
|
|
|
|
|
|
235 |
else:
|
236 |
+
return {
|
237 |
+
"statusCode": 404,
|
238 |
+
"headers": cors_headers,
|
239 |
+
"body": json.dumps({
|
240 |
+
"error": "Not Found",
|
241 |
+
"details": f"Path {path} not found"
|
242 |
+
})
|
243 |
+
}
|
244 |
+
|
245 |
+
except Exception as e:
|
246 |
+
return {
|
247 |
+
"statusCode": 500,
|
248 |
+
"headers": cors_headers,
|
249 |
+
"body": json.dumps({
|
250 |
+
"error": "Internal Server Error",
|
251 |
+
"details": str(e)
|
252 |
+
})
|
253 |
+
}
|