kaiju_files.files module

class FileService[source]

Bases: SQLService, ContextableService, AbstractRPCCompatible

File 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

async init()[source]

Define your asynchronous initialization here.

async close()[source]

Define your asynchronous de-initialization here.

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

async upload_content(id, content, _move=True)[source]
Parameters:

id (UUID) –

async delete_unlinked_files(days=1)[source]

Remove all old file record which have no hash (i.e. an actual file) linked to them.

async delete_local_file(name)[source]
Parameters:

name (Path | str | TemporaryFile) –

async get_local_file_path(id)[source]
Parameters:

id (UUID) –

get_temp_file_path()[source]
Return type:

Path

async get_temp_dir(*args, **kws)[source]
Return type:

TemporaryDirectory

async get_temp_file(*args, **kws)[source]
Return type:

NamedTemporaryFile

get_temp_file_sync(*args, **kws)[source]
Return type:

NamedTemporaryFile