async def api_delete_file(request):
    fname = request.path_params['fname']
    if not fname:
        return JSONResponse({
            "success": False,
            "message": "No filename specified"
        })
    fname = os.path.realpath(os.path.join(get_finished_path(), fname))
    if os.path.commonprefix(
        (fname, get_finished_path())) != get_finished_path():
        return JSONResponse({"success": False, "message": "Invalid filename"})
    fname = Path(fname)
    try:
        if fname.is_dir():
            shutil.rmtree(fname)
        else:
            fname.unlink()
    except OSError as e:
        print(e)
        return JSONResponse({
            "success": False,
            "message": "Could not delete the specified file"
        })

    return JSONResponse({"success": True, "message": "File deleted"})
async def front_finished(request):
    root_dir = Path(get_finished_path())
    matches = root_dir.glob('*')

    files = [{
        'name':
        f1.name,
        'modified':
        datetime.fromtimestamp(f1.stat().st_mtime).strftime('%H:%m %D'),
        'directory':
        f1.is_dir(),
        'children':
        sorted([{
            'name':
            f2.name,
            'modified':
            datetime.fromtimestamp(f2.stat().st_mtime).strftime('%H:%m %D')
        } for f2 in f1.iterdir() if not f2.name.startswith('.')],
               key=itemgetter('modified'),
               reverse=True) if f1.is_dir() else None
    } for f1 in matches if not f1.name.startswith('.')]

    context = {
        'request': request,
        'ydl_version': request.app.state.ydlhandler.get_ydl_version(),
        'ydl_name': request.app.state.ydlhandler.ydl_module_name,
        'ydl_website': request.app.state.ydlhandler.ydl_website,
        'finished_files': sorted(files,
                                 key=itemgetter('modified'),
                                 reverse=True)
    }
    return templates.TemplateResponse('finished.html', context=context)
Esempio n. 3
0
async def front_finished(request):
    root_dir = Path(get_finished_path())
    matches = root_dir.glob("*")

    files = [{
        "name":
        f1.name,
        "modified":
        datetime.fromtimestamp(f1.stat().st_mtime).strftime("%H:%m %D"),
        "directory":
        f1.is_dir(),
        "children":
        sorted(
            [{
                "name":
                f2.name,
                "modified":
                datetime.fromtimestamp(f2.stat().st_mtime).strftime("%H:%m %D")
            } for f2 in f1.iterdir() if not f2.name.startswith(".")],
            key=itemgetter("modified"),
            reverse=True,
        ) if f1.is_dir() else None,
    } for f1 in matches if not f1.name.startswith(".")]

    context = {
        "request": request,
        "ydl_version": request.app.state.ydlhandler.get_ydl_version(),
        "ydl_name": request.app.state.ydlhandler.ydl_module_name,
        "ydl_website": request.app.state.ydlhandler.ydl_website,
        "finished_files": sorted(files,
                                 key=itemgetter("modified"),
                                 reverse=True),
    }
    return templates.TemplateResponse("finished.html", context=context)
Esempio n. 4
0
async def api_list_finished(request):
    root_dir = Path(get_finished_path())
    matches = root_dir.glob('*')

    files = [{
        'name':
        f1.name,
        'modified':
        f1.stat().st_mtime * 1000,
        'children':
        sorted([{
            'name': f2.name,
            'modified': f2.stat().st_mtime * 1000
        } for f2 in f1.iterdir() if not f2.name.startswith('.')],
               key=itemgetter('modified'),
               reverse=True) if f1.is_dir() else None
    } for f1 in matches if not f1.name.startswith('.')]

    files = sorted(files, key=itemgetter('modified'), reverse=True)
    return JSONResponse({"success": True, "files": files})
Esempio n. 5
0
from pathlib import Path

from ydl_server import views
from ydl_server.config import app_config, get_finished_path

from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles

static = StaticFiles(directory=str(Path(__file__).parent / "static"))

finished_files = StaticFiles(directory=get_finished_path())

routes = [
    Route("/", views.front_index, name="index"),
    Route("/index", views.front_index, name="index"),
    Route("/logs", views.front_logs, name="logs"),
    Route("/finished", views.front_finished, name="finished"),
    Route("/api/extractors",
          views.api_list_extractors,
          name="api_list_extractors"),
    Route("/api/youtube-dl/update", views.ydl_update, name="api_update"),
    Route("/api/downloads/stats", views.api_queue_size, name="api_queue_size"),
    Route("/api/downloads", views.api_logs, name="api_logs"),
    Route("/api/downloads/clean", views.api_logs_clean, name="api_logs_clean"),
    Route("/api/downloads",
          views.api_logs_purge,
          name="api_logs_purge",
          methods=['DELETE']),
    Route("/api/downloads",
          views.api_queue_download,
          name="api_queue_download",