kaiju_files.files module¶
- class FileService[source]¶
Bases:
SQLService,ContextableService,AbstractRPCCompatibleFile management service which handlers uploads and downloads.
It’s expected to be initialized within a service context manager, but it’s possible to do initialization manually by directly providing an instance of the database service.
File service consists of two main parts: file records table and actual files stored in a local directory.
To upload a file first you have to create an empty file record with all metadata about the file and then link it to a local file or upload data referencing the record by its id.
async with FileService(app, database_service): data = await file_service.create({'name': 'test', 'extension': 'txt', 'meta': {'tag': 'file'}}) data = await file_service.upload_local_file(data['id'], file_path)
Files are stored locally under their hash UUID and symlinked using their specified names, thus same files with different names can coexist in a filesystem and may be served statically via nginx or other server.
- service_name = 'files'¶
you may define a custom service name here
- URI_PREFIX = '/files/'¶
- DELETE_UNLINKED_INTERVAL_DAYS = 1¶
- insert_columns = ('name', 'extension', 'meta')¶
you may specify insert columns here
- update_columns = ('name', 'extension', 'hash', 'meta')¶
you may specify columns for update here
- __init__(app, database_service, dir='.', uri_prefix='/files/', logger=None)[source]¶
Initialize.
- Parameters:
app –
database_service (DatabaseService) –
dir – local file storage path
uri_prefix (str) – optional prefix for returned URIs
logger –
- property closed: bool¶
Must return True if close() procedure has been successfully executed.
- property temp_dir: Path¶
- async upload_local_file(id, path, move=True)[source]¶
Upload local file.
Use this method to ‘upload’ a local file. This operation will move the file into a file service directory and link it to a file record.
- Parameters:
id (UUID) – file record id
path (str | Path | NamedTemporaryFile) – local file path
move – move file instead of copying it
- Returns:
file and URI