fred-dev commited on
Commit
5cacabe
·
verified ·
1 Parent(s): 359284a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -32
app.py CHANGED
@@ -10,7 +10,6 @@ from app.logger import setup_logger
10
  import itertools
11
  import utils.extra_config
12
  import logging
13
- from flask import Flask, request, send_from_directory, render_template_string
14
  import threading
15
 
16
  if __name__ == "__main__":
@@ -57,35 +56,6 @@ if __name__ == "__main__":
57
 
58
  setup_logger(log_level=args.verbose, use_stdout=args.log_stdout)
59
 
60
- def start_file_browser():
61
- app = Flask(__name__)
62
- UPLOAD_FOLDER = "/data"
63
-
64
- @app.route("/files")
65
- def index():
66
- files = os.listdir(UPLOAD_FOLDER)
67
- file_list = ''.join(f'<li><a href="/files/download/{f}">{f}</a></li>' for f in files)
68
- return render_template_string('''
69
- <h2>File Browser (/data)</h2>
70
- <ul>{{ file_list | safe }}</ul>
71
- <h3>Upload File</h3>
72
- <form method=post enctype=multipart/form-data action="/files/upload">
73
- <input type=file name=file>
74
- <input type=submit value=Upload>
75
- </form>
76
- ''', file_list=file_list)
77
-
78
- @app.route("/files/download/<path:filename>")
79
- def download_file(filename):
80
- return send_from_directory(UPLOAD_FOLDER, filename, as_attachment=True)
81
-
82
- @app.route("/files/upload", methods=["POST"])
83
- def upload_file():
84
- f = request.files["file"]
85
- f.save(os.path.join(UPLOAD_FOLDER, f.filename))
86
- return 'Uploaded! <a href="/files">Go back</a>'
87
-
88
- app.run(host="0.0.0.0", port=7861, debug=False)
89
 
90
 
91
  def apply_custom_paths():
@@ -335,7 +305,86 @@ def start_comfyui(asyncio_loop=None):
335
 
336
  cuda_malloc_warning()
337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  prompt_server.add_routes()
 
339
  hijack_progress(prompt_server)
340
 
341
  threading.Thread(target=prompt_worker, daemon=True, args=(q, prompt_server,)).start()
@@ -364,8 +413,6 @@ def start_comfyui(asyncio_loop=None):
364
 
365
 
366
  if __name__ == "__main__":
367
- #start file browser
368
- threading.Thread(target=start_file_browser, daemon=True).start()
369
 
370
  # Running directly, just start ComfyUI.
371
  logging.info("ComfyUI version: {}".format(comfyui_version.__version__))
 
10
  import itertools
11
  import utils.extra_config
12
  import logging
 
13
  import threading
14
 
15
  if __name__ == "__main__":
 
56
 
57
  setup_logger(log_level=args.verbose, use_stdout=args.log_stdout)
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
 
61
  def apply_custom_paths():
 
305
 
306
  cuda_malloc_warning()
307
 
308
+ # Inject file browser routes into the FastAPI app
309
+ # In your app.py (inside start_comfyui)
310
+ from fastapi import UploadFile, Request, Form
311
+ from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse
312
+ from fastapi.routing import APIRouter
313
+ import mimetypes
314
+
315
+ UPLOAD_FOLDER = "/data"
316
+ router = APIRouter()
317
+
318
+ # Helper to get safe paths
319
+ def get_safe_path(subpath: str):
320
+ full_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, subpath))
321
+ if not full_path.startswith(os.path.abspath(UPLOAD_FOLDER)):
322
+ raise ValueError("Invalid path")
323
+ return full_path
324
+
325
+ @router.get("/files/{path:path}", response_class=HTMLResponse)
326
+ async def browse_files(path: str):
327
+ full_path = get_safe_path(path)
328
+ if not os.path.exists(full_path):
329
+ return HTMLResponse("Not found", status_code=404)
330
+
331
+ if os.path.isfile(full_path):
332
+ mime_type, _ = mimetypes.guess_type(full_path)
333
+ return FileResponse(full_path, media_type=mime_type)
334
+
335
+ # Show folder listing
336
+ files = os.listdir(full_path)
337
+ files.sort()
338
+ parent_path = os.path.dirname(path)
339
+ list_html = f'<li><a href="/files/{parent_path}">.. (Up)</a></li>' if path else ''
340
+ for f in files:
341
+ sub = os.path.join(path, f)
342
+ encoded = sub.replace(" ", "%20")
343
+ entry_path = os.path.join(full_path, f)
344
+ preview = ''
345
+ if os.path.isfile(entry_path):
346
+ mime, _ = mimetypes.guess_type(entry_path)
347
+ if mime and mime.startswith("image"):
348
+ preview = f'<br><img src="/files/{encoded}" style="max-height:100px;">'
349
+ elif mime and mime.startswith("video"):
350
+ preview = f'<br><video src="/files/{encoded}" style="max-height:100px;" controls></video>'
351
+ list_html += f'<li><a href="/files/{encoded}">{f}</a> '
352
+ list_html += f'<a href="/files/delete/{encoded}" style="color:red">[Delete]</a>{preview}</li>'
353
+
354
+ return f'''
355
+ <h2>Browsing: /{path}</h2>
356
+ <ul>{list_html}</ul>
357
+ <h3>Upload File</h3>
358
+ <form method=post enctype=multipart/form-data action="/files/upload/{path}">
359
+ <input type=file name=file>
360
+ <input type=submit value=Upload>
361
+ </form>
362
+ '''
363
+
364
+ @router.post("/files/upload/{path:path}")
365
+ async def upload_file(path: str, file: UploadFile):
366
+ full_path = get_safe_path(path)
367
+ if not os.path.exists(full_path):
368
+ os.makedirs(full_path)
369
+ file_path = os.path.join(full_path, file.filename)
370
+ with open(file_path, "wb") as f:
371
+ f.write(await file.read())
372
+ return RedirectResponse(url=f"/files/{path}", status_code=303)
373
+
374
+ @router.get("/files/delete/{path:path}")
375
+ async def delete_file(path: str):
376
+ try:
377
+ file_path = get_safe_path(path)
378
+ if os.path.isfile(file_path):
379
+ os.remove(file_path)
380
+ return RedirectResponse(url="/files/" + os.path.dirname(path), status_code=303)
381
+ return HTMLResponse("Not a file", status_code=400)
382
+ except Exception as e:
383
+ return HTMLResponse(f"Error: {str(e)}", status_code=400)
384
+
385
+
386
  prompt_server.add_routes()
387
+ prompt_server.app.include_router(router)
388
  hijack_progress(prompt_server)
389
 
390
  threading.Thread(target=prompt_worker, daemon=True, args=(q, prompt_server,)).start()
 
413
 
414
 
415
  if __name__ == "__main__":
 
 
416
 
417
  # Running directly, just start ComfyUI.
418
  logging.info("ComfyUI version: {}".format(comfyui_version.__version__))