wrolpi/modules/files/api.py

92 lines
2.5 KiB
Python
Raw Normal View History

Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
import pathlib
from typing import List
from urllib.request import Request
from sanic import response
from sanic_ext import validate
from sanic_ext.extensions.openapi import openapi
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
2022-01-31 17:00:40 -07:00
from wrolpi.common import get_media_directory, api_param_limiter
from wrolpi.errors import InvalidFile
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
from wrolpi.root_api import get_blueprint, json_response
from . import lib, schema
bp = get_blueprint('Files', '/api/files')
def paths_to_files(paths: List[pathlib.Path]):
2022-01-29 21:24:39 -07:00
"""Convert Paths to what the React UI expects."""
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
media_directory = get_media_directory()
new_files = []
for path in paths:
stat = path.stat()
key = path.relative_to(media_directory)
modified = stat.st_mtime
if path.is_dir():
key = f'{key}/'
new_files.append(dict(
key=key,
modified=modified,
url=key,
name=path.name,
))
else:
# A File should know it's size.
new_files.append(dict(
key=key,
modified=modified,
size=stat.st_size,
url=key,
name=path.name,
))
return new_files
@bp.post('/')
@openapi.definition(
summary='List files in a directory',
body=schema.FilesRequest,
)
@validate(schema.FilesRequest)
async def get_files(_: Request, body: schema.FilesRequest):
directories = body.directories or []
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
files = lib.list_files(directories)
files = paths_to_files(files)
return json_response({'files': files})
@bp.post('/delete')
@openapi.definition(
summary='Delete a single file. Returns an error if WROL Mode is enabled.',
body=schema.DeleteRequest,
)
@validate(schema.DeleteRequest)
async def delete_file(_: Request, body: schema.DeleteRequest):
if not body.file:
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
raise InvalidFile('file cannot be empty')
lib.delete_file(body.file)
Squashed commit of the following: commit 54d06bf8045fceaa2684348d110792e6833b170c Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:46:14 2022 -0700 Can't delete files when WROL Mode is enabled. commit 2d91f1983aae47bdb3f6dc7fc2bd2ae642c1126a Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:37:42 2022 -0700 Fixing bug where files could not be deleted in series. commit eed818c50682b19de9f18da2d4daedc54408cc49 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:26:31 2022 -0700 Fixing fake_now to reset now() when complete. commit 183e98cbec25784720ebaeabfac8af4a3fc87da8 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 16:08:14 2022 -0700 Files can be deleted and downloaded. commit c5bcd25a3da041f42b9e11eae804aa359ab9e985 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:41:07 2022 -0700 Files can be deleted or downloaded. commit 690b332bb4f89aa4e97ce2590b35bd4a71fefd87 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 14:11:05 2022 -0700 File is opened in new tab when file is selected. commit ba8ead6f3185d0a398c4f89cecb5958f5e7731a3 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:32:32 2022 -0700 Removing files sort because React FE sorts them itself. commit 4e04753234b11d615c5992bc08e9dd9fb7226c05 Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 13:29:52 2022 -0700 Duplicate directories are ignored. commit c2f7d7929f37655e5c2d11767a512571263b35ef Author: Roland <roland@learningselfreliance.com> Date: Sat Jan 29 12:22:38 2022 -0700 Multiple directories can be requested. commit 31678139c0799d0034d564cc1f8ae448234df90c Author: Roland <roland@learningselfreliance.com> Date: Fri Jan 28 17:21:33 2022 -0700 Files are displayed with the correct modified time. Files can be recursively viewed. commit a2d10a1dd792e518c8c6ad8f699b5d416c3d07e4 Author: Roland <roland@learningselfreliance.com> Date: Thu Jan 27 22:37:19 2022 -0700 Files can be viewed. Folders display their children.
2022-01-29 16:50:22 -07:00
return response.empty()
2022-01-31 17:00:40 -07:00
files_limit_limiter = api_param_limiter(100)
files_offset_limiter = api_param_limiter(100, 0)
@bp.post('/refresh')
@openapi.description('Find and index all files')
async def refresh(_: Request):
lib.refresh_files()
return response.empty()
2022-01-31 17:00:40 -07:00
@bp.post('/search')
@openapi.definition(
summary='Search Files',
body=schema.FilesSearchRequest,
)
@validate(schema.FilesSearchRequest)
async def search_files(_: Request, body: schema.FilesSearchRequest):
files, total = lib.search(body.search_str, body.limit, body.offset)
return json_response(dict(files=files, totals=dict(files=total)))