veltrixcode commited on
Commit
e2ffe48
·
verified ·
1 Parent(s): ff44414

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -1,34 +1,58 @@
1
  import os
2
  import uuid
3
- from fastapi import FastAPI, UploadFile, File, HTTPException
 
4
  from internetarchive import upload
5
 
6
- # Load keys from environment variables
7
  IA_ACCESS_KEY = os.getenv("YXzY5OrREXLL6XBh")
8
  IA_SECRET_KEY = os.getenv("m2XnL3X7xCB1pNGE")
9
 
10
  if not IA_ACCESS_KEY or not IA_SECRET_KEY:
11
- raise RuntimeError("Internet Archive keys not set in environment variables")
12
 
13
  app = FastAPI(title="Internet Archive Image Upload API")
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.post("/upload")
16
  async def upload_image(file: UploadFile = File(...)):
17
  if not file.content_type.startswith("image/"):
18
- raise HTTPException(status_code=400, detail="Only image files allowed")
19
 
20
- # Save file temporarily
21
- temp_filename = f"/tmp/{uuid.uuid4()}_{file.filename}"
22
- with open(temp_filename, "wb") as f:
23
  f.write(await file.read())
24
 
25
- # Unique identifier for IA
26
- identifier = f"img-upload-{uuid.uuid4()}"
27
 
28
  try:
29
  upload(
30
  identifier=identifier,
31
- files=temp_filename,
32
  access_key=IA_ACCESS_KEY,
33
  secret_key=IA_SECRET_KEY,
34
  metadata={
@@ -37,16 +61,14 @@ async def upload_image(file: UploadFile = File(...)):
37
  }
38
  )
39
  except Exception as e:
40
- raise HTTPException(status_code=500, detail=str(e))
41
  finally:
42
- os.remove(temp_filename)
43
 
44
- # Direct download URL
45
  url = f"https://archive.org/download/{identifier}/{file.filename}"
46
 
47
  return {
48
  "success": True,
49
  "identifier": identifier,
50
- "filename": file.filename,
51
  "url": url
52
  }
 
1
  import os
2
  import uuid
3
+ from fastapi import FastAPI, UploadFile, File, Request, HTTPException
4
+ from fastapi.responses import HTMLResponse
5
  from internetarchive import upload
6
 
 
7
  IA_ACCESS_KEY = os.getenv("YXzY5OrREXLL6XBh")
8
  IA_SECRET_KEY = os.getenv("m2XnL3X7xCB1pNGE")
9
 
10
  if not IA_ACCESS_KEY or not IA_SECRET_KEY:
11
+ raise RuntimeError("Set IA_ACCESS_KEY and IA_SECRET_KEY env variables")
12
 
13
  app = FastAPI(title="Internet Archive Image Upload API")
14
 
15
+ # ---------- FRONTEND ----------
16
+ @app.get("/", response_class=HTMLResponse)
17
+ async def home():
18
+ return """
19
+ <!DOCTYPE html>
20
+ <html>
21
+ <head>
22
+ <title>IA Image Upload</title>
23
+ <style>
24
+ body { font-family: Arial; background:#111; color:#fff; text-align:center; padding-top:40px }
25
+ input, button { padding:10px; margin:10px }
26
+ button { cursor:pointer }
27
+ </style>
28
+ </head>
29
+ <body>
30
+ <h2>Upload Image to Internet Archive</h2>
31
+ <form action="/upload" method="post" enctype="multipart/form-data">
32
+ <input type="file" name="file" accept="image/*" required />
33
+ <br/>
34
+ <button type="submit">Upload</button>
35
+ </form>
36
+ </body>
37
+ </html>
38
+ """
39
+
40
+ # ---------- API ----------
41
  @app.post("/upload")
42
  async def upload_image(file: UploadFile = File(...)):
43
  if not file.content_type.startswith("image/"):
44
+ raise HTTPException(400, "Only images allowed")
45
 
46
+ temp_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
47
+ with open(temp_path, "wb") as f:
 
48
  f.write(await file.read())
49
 
50
+ identifier = f"hf-img-{uuid.uuid4()}"
 
51
 
52
  try:
53
  upload(
54
  identifier=identifier,
55
+ files=temp_path,
56
  access_key=IA_ACCESS_KEY,
57
  secret_key=IA_SECRET_KEY,
58
  metadata={
 
61
  }
62
  )
63
  except Exception as e:
64
+ raise HTTPException(500, str(e))
65
  finally:
66
+ os.remove(temp_path)
67
 
 
68
  url = f"https://archive.org/download/{identifier}/{file.filename}"
69
 
70
  return {
71
  "success": True,
72
  "identifier": identifier,
 
73
  "url": url
74
  }