m_media (zotonic_core v1.0.0-rc.17)
Access to data about uploaded files and other media.
The medium (singular form of media) table stores all information of uploaded files or other media. Every resource can
contain a single medium. A resource with a medium is most often of the category image, audio, video or document.
In template the m_media model is used to fetch the medium record by the resource id: m.media[id]. This is the same
function as with m.rsc[id].medium except, that the m_rsc model does access control checks and the m_media does not.
The m_media model implements all functions to handle media files and is used by other Erlang modules.
Properties of a medium record
A medium record has minimally the following properties, other properties can be added by modules.
| Property | Description | Example value |
|---|---|---|
| id | Id of the medium record, equal to the page id. | 512 |
| filename | Filename and path of the uploaded file, relative to the archive directory. | <<"2009/10/20/zotonic-datamodel.jpg">> |
| rootname | Root name of the filename. | <<"zotonic-datamodel">> |
| original_filename | Filename as suggested by the user agent when uploading the file. Can contain illegal characters. | <<"Zotonic-datamodel.jpg">> |
| mime | Mime type of the medium. | <<"image/jpeg">> |
| width | Width in pixels. | 536 |
| height | Height in pixels. | 737 |
| orientation | Exif oritentation of the image. | 1 |
| sha1 | Optional sha1 checksum of uploaded file. Undefined when not present. | |
| size | Size in bytes of the uploaded file. | 71585 |
| preview_filename | Optional filename for a generated file preview. | |
| preview_width | Optional. Width of the generated preview. | |
| preview_height | Optional. Height of the generated preview. | |
| is_deletable_file | If the file should be deleted when the medium record is deleted. A boolean. | true |
| is_deletable_preview | If the optionally generated preview file should be deleted when the medium record is deleted. A boolean. | false |
| created | Timestamp when the medium record is created. | {{2009,10,20},{13,47,27}} |
Available Model API Paths
| Method | Path pattern | Description |
|---|---|---|
get | /+id/... | Return medium record for resource +id when the resource is visible; returns enoent for unknown resource and eacces when not visible. |
post | /+id | Replace medium of existing resource +id from payload url or uploaded file (#upload); optional payload rsc map updates resource props. No further lookups. |
post | / | Create a new resource+medium from payload url or uploaded file (#upload), with optional payload rsc properties for the new resource. No further lookups. |
delete | /+id | Delete medium linked to resource +id (queues file cleanup, emits media events); requires editable resource access. No further lookups. |
/+name marks a variable path segment. A trailing /... means extra path segments are accepted for further lookups.
Summary
Types
-type option() :: {preferred_category, m_rsc:resource()} | {preview_url, media_url()} | {max_length, non_neg_integer()} | {timeout, non_neg_integer()} | m_rsc:update_option().
-type options() :: [option()].
Functions
-spec delete(m_rsc:resource_id(), z:context()) -> ok | {error, term()}.
-spec depiction(m_rsc:resource_id(), z:context()) -> map() | undefined.
-spec depicts(m_rsc:resource_id(), z:context()) -> [m_rsc:resource_id()].
-spec duplicate(m_rsc:resource(), m_rsc:resource(), z:context()) -> ok | {error, term()}.
-spec get(m_rsc:resource() | undefined, z:context()) -> z_media_identify:media_info() | undefined.
-spec get_by_filename(binary(), z:context()) -> z_media_identify:media_info() | undefined.
-spec identify(m_rsc:resource_id(), z:context()) -> {ok, z_media_identify:media_info()} | {error, term()}.
-spec insert_file(File, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, Context :: z:context(), RscId :: m_rsc:resource_id().
-spec insert_file(File, RscProps, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscProps :: m_rsc:props_all(), Context :: z:context(), RscId :: m_rsc:resource_id().
-spec insert_file(File, RscProps, Options, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscProps :: m_rsc:props_all(), Options :: options(), Context :: z:context(), RscId :: m_rsc:resource_id().
-spec insert_url(Url, Context) -> {ok, RscId} | {error, term()} when Url :: media_url(), Context :: z:context(), RscId :: m_rsc:resource_id().
-spec insert_url(Url, RscProps, Context) -> {ok, RscId} | {error, term()} when Url :: media_url(), RscProps :: m_rsc:props_all(), Context :: z:context(), RscId :: m_rsc:resource_id().
-spec insert_url(Url, RscProps, Options, Context) -> {ok, RscId} | {error, term()} when Url :: media_url(), RscProps :: m_rsc:props_all(), Options :: options(), Context :: z:context(), RscId :: m_rsc:resource_id().
-spec is_unique_file(Filename, Context) -> boolean() when Filename :: file:filename_all(), Context :: z:context().
-spec m_delete([binary()], zotonic_model:opt_msg(), z:context()) -> {ok, term()} | ok | {error, term()}.
-spec m_get(list(), zotonic_model:opt_msg(), z:context()) -> zotonic_model:return().
-spec m_post([binary()], zotonic_model:opt_msg(), z:context()) -> {ok, term()} | ok | {error, term()}.
-spec make_preview_unique(RscId, Extension, Context) -> PreviewFilename when RscId :: m_rsc:resource_id() | insert_rsc, Extension :: binary(), Context :: z:context(), PreviewFilename :: file:filename_all().
-spec replace(Id, MediaProps, Context) -> ok | {error, term()} when Id :: m_rsc:resource_id(), MediaProps :: map() | list(), Context :: z:context().
-spec replace_file(File, RscId, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscId :: m_rsc:resource_id(), Context :: z:context().
-spec replace_file(File, RscId, RscProps, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscId :: m_rsc:resource_id(), RscProps :: m_rsc:props_all(), Context :: z:context().
-spec replace_file(File, RscId, RscProps, Options, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscId :: m_rsc:resource_id(), RscProps :: m_rsc:props_all(), Options :: options(), Context :: z:context().
-spec replace_file(File, RscId, RscProps, MediaInfo, Options, Context) -> {ok, RscId} | {error, term()} when File :: file:filename_all() | #upload{filename :: binary() | undefined, tmpfile :: file:filename_all() | undefined, tmpmonitor :: undefined | pid(), data :: binary() | undefined, mime :: binary() | undefined}, RscId :: m_rsc:resource_id(), RscProps :: m_rsc:props_all(), MediaInfo :: z_media_identify:media_info() | list(), Options :: options(), Context :: z:context().
-spec replace_url(Url, RscId, RscProps, Context) -> {ok, RscId} | {error, term()} when Url :: media_url(), RscId :: m_rsc:resource_id(), RscProps :: m_rsc:props_all(), Context :: z:context().
-spec replace_url(Url, RscId, RscProps, Options, Context) -> {ok, RscId} | {error, term()} when Url :: media_url(), RscId :: m_rsc:resource_id(), RscProps :: m_rsc:props_all(), Options :: options(), Context :: z:context().
-spec reupload(m_rsc:resource_id(), z:context()) -> {ok, m_rsc:resource_id()} | {error, term()}.
-spec save_preview(m_rsc:resource_id(), iodata(), binary() | string(), z:context()) -> {ok, file:filename_all()} | {error, eacces | term()}.