Working with the user's media
src.plugin_api.media lets a plugin read the file of a media the user picked, take a frame out of
a video, store a file it made in the user's Library and tell which file types the Library takes.
Each function that names a media takes the acting user and reaches only what the history and
Library APIs show that user: a media that is not theirs, that their content policy hides or whose
file is gone is reported as not found, never as forbidden.
A media is named by the reference resolve_media_ref takes (see
Running generations from a plugin):
{"kind": "history", "generation_id", "file_id"} for a file of a generation, {"kind": "library", "item_id"} for a Library item. Paths and storage keys are never accepted. resolve_media_ref,
MediaRefError and the reference types import from src.plugin_api.media and from
src.plugin_api as well.
| Function | Does |
|---|---|
await extract_frame(user, ref, at="last", purpose="user_upload") | Takes one frame of a video the user may see and stores it as their PNG upload, named after the video: clip-frame-last.png, clip-frame-2.5s.png. at is "last" or a number of seconds from the start, turned into a frame with the frame rate probe_video reads, so at in seconds needs ffprobe and "last" does not |
await save_user_media(user, path, purpose="user_upload") | Stores a file the plugin wrote, at an absolute path on this server, as the user's upload, the way POST /api/media/upload stores one: the media.before_upload hooks, the same file stored once per user and purpose, thumbnails, and the user's plan. Its extension decides what it is: an image, a video, an audio file or a 3D model |
media_local_path(user, ref) | The path on this server of the file of a media the user may see, or None when the storage keeps it elsewhere, such as object storage. The path is the stored file itself: read it, never write, move or delete it |
copy_media_file(user, ref, target) | Streams the file of a media the user may see into target, a file of the plugin, wherever the storage keeps it. A copy that fails leaves no file at target |
upload_formats() | The file extensions the Library takes, keyed by the media_type a stored file of that extension gets: {"image": [".bmp", ".gif", ...], "video": [...], "audio": [...], "model": [".glb"]}, each list sorted and read when called. save_user_media stores exactly these. A plugin can hand them to its page for a file picker, leaving out any its own code cannot read |
extract_frame and save_user_media return a StoredMedia: the keys of a ResolvedMedia, which
a media field and bind_director_media take (value, uploads/<file>; media_type;
original_filename; origin, None), plus ref, the {"kind": "library", "item_id"}
reference of the stored item, and width and height in pixels when they could be read. ref is
None only when the store could not write the Library row; the file is stored all the same, as
with the upload route.
purpose="user_upload" makes the file a Library item the user browses. purpose="derived_artifact"
keeps it servable and referenceable but out of the Library, for a helper file such as a mask; a
helper file is an image, a video or an audio file of at most 512 MB.
Refusals raise MediaRefError with a code:
code | When |
|---|---|
invalid_ref | The reference is not a reference object, is malformed or has other keys; at is not "last" or a finite number of seconds, 0 or more; path is relative |
not_found | The media is not one the user may see, or its file is gone; path is not a file |
not_a_video | extract_frame was given an image or an audio file |
extract_failed | The frame could not be read, for example at a time past the end of the video, or the frame rate could not be read for at in seconds |
unsupported_media | save_user_media was given a file whose extension is not one of an image, a video, an audio file or a 3D model |
invalid_purpose | purpose is neither user_upload nor derived_artifact |
upload_rejected | The store refused the file: a media.before_upload hook blocked it (the message is its reason), or a helper file is too large |
A full storage plan raises LimitExceeded (src.plugin_api.limits), as the upload route does.
save_user_media checks the plan and the helper file cap against the size of the file before it
reads it. Without a user, extract_frame and save_user_media raise PermissionError.
media_local_path and copy_media_file raise invalid_ref and not_found only, and a copy lets
the error of the storage through when it fails.
save_user_media stores whatever file it is given. Give it files your plugin wrote, never a path
taken from a request.
from src.plugin_api.media import extract_frame
from src.plugin_api.presets import bind_director_media
async def continue_from(user, request, generation_id, file_id):
frame = await extract_frame(user, {"kind": "history", "generation_id": generation_id, "file_id": file_id})
return bind_director_media(request, "first", frame, promote=True)