rayhanzuck commited on
Commit
c846156
·
verified ·
1 Parent(s): 44cd996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -1,46 +1,50 @@
1
  from fastapi import FastAPI, File, UploadFile
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import JSONResponse
 
4
  import os
5
 
6
  app = FastAPI()
7
 
8
- # Konfigurasi CORS agar bisa diakses dari frontend
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"],
12
- allow_credentials=True,
13
- allow_methods=["*"],
14
- allow_headers=["*"],
15
- )
16
-
17
- # Direktori tempat menyimpan file
18
  UPLOAD_FOLDER = "/tmp/uploads"
19
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
20
 
21
- # Base URL (Ganti dengan domain sebenarnya jika di-deploy)
22
- BASE_URL = "https://rayhanzuck-cloud.hf.space"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- @app.get("/files")
25
- def list_files():
26
- try:
27
- files = os.listdir(UPLOAD_FOLDER)
28
- file_urls = [f"{BASE_URL}/uploads/{file}" for file in files]
29
- return JSONResponse(content={"files": file_urls})
30
- except Exception as e:
31
- return JSONResponse(content={"error": str(e)}, status_code=500)
32
-
33
  @app.post("/upload")
34
- async def upload_file(file: UploadFile = File(...)):
35
  file_path = os.path.join(UPLOAD_FOLDER, file.filename)
36
-
37
- # Simpan file
38
  with open(file_path, "wb") as buffer:
39
- buffer.write(await file.read())
 
40
 
41
- file_url = f"{BASE_URL}/uploads/{file.filename}"
42
- return {"filename": file.filename, "url": file_url, "status": "uploaded"}
43
-
44
- # Endpoint untuk menyajikan file statis
45
- from fastapi.staticfiles import StaticFiles
46
- app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
 
1
  from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import shutil
5
  import os
6
 
7
  app = FastAPI()
8
 
 
 
 
 
 
 
 
 
 
 
9
  UPLOAD_FOLDER = "/tmp/uploads"
10
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
11
 
12
+ # Menyediakan folder statis untuk menyimpan file yang diunggah
13
+ app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
14
+
15
+ @app.get("/", response_class=HTMLResponse)
16
+ async def main():
17
+ return """
18
+ <html>
19
+ <body>
20
+ <h2>Upload File</h2>
21
+ <form action="/upload" method="post" enctype="multipart/form-data">
22
+ <input type="file" name="file">
23
+ <input type="submit" value="Upload">
24
+ </form>
25
+ <h3>Uploaded Files:</h3>
26
+ <ul id="file-list"></ul>
27
+ <script>
28
+ async function fetchFiles() {
29
+ let response = await fetch("/files");
30
+ let files = await response.json();
31
+ let fileList = document.getElementById("file-list");
32
+ fileList.innerHTML = files.map(f => `<li><a href="/uploads/${f}">${f}</a></li>`).join("");
33
+ }
34
+ fetchFiles();
35
+ </script>
36
+ </body>
37
+ </html>
38
+ """
39
 
 
 
 
 
 
 
 
 
 
40
  @app.post("/upload")
41
+ async def upload_file(file: UploadFile):
42
  file_path = os.path.join(UPLOAD_FOLDER, file.filename)
 
 
43
  with open(file_path, "wb") as buffer:
44
+ shutil.copyfileobj(file.file, buffer)
45
+ return {"filename": file.filename, "url": f"/uploads/{file.filename}"}
46
 
47
+ @app.get("/files")
48
+ async def list_files():
49
+ files = os.listdir(UPLOAD_FOLDER)
50
+ return files