{"result":{"depiction_url":null,"edges":{"in_module":{"objects":[{"created":"2026-09-09T13:48:11Z","object_id":{"id":335,"is_a":["text","documentation","reference","module"],"name":"doc_core","title":"Zotonic Core","uri":"https:\/\/zotonic.com\/id\/335"},"seq":1}],"predicate":{"id":333,"is_a":["meta","predicate"],"name":"in_module","title":{"_type":"trans","tr":{"en":"In module"}},"uri":"https:\/\/zotonic.com\/id\/in_module"}},"subject":{"objects":[{"created":"2026-09-09T13:48:11Z","object_id":{"id":2555,"is_a":["categorization","keyword","keyword_information_type"],"name":"zotonic_topic_reference","title":"Reference","uri":"https:\/\/zotonic.com\/id\/2555"},"seq":1},{"created":"2026-09-09T13:48:11Z","object_id":{"id":2564,"is_a":["categorization","keyword","keyword_audience"],"name":"zotonic_topic_backend_developer","title":"Backend developer","uri":"https:\/\/zotonic.com\/id\/2564"},"seq":2},{"created":"2026-09-09T13:48:11Z","object_id":{"id":2576,"is_a":["categorization","keyword","keyword_domain"],"name":"zotonic_topic_media_management","title":"Media management","uri":"https:\/\/zotonic.com\/id\/2576"},"seq":3},{"created":"2026-11-05T18:43:02Z","object_id":{"id":2581,"is_a":["categorization","keyword","keyword_domain"],"name":"zotonic_topic_file_storage","title":"File storage","uri":"https:\/\/zotonic.com\/id\/2581"},"seq":4},{"created":"2026-09-09T13:48:11Z","object_id":{"id":2619,"is_a":["categorization","keyword","keyword_architecture"],"name":"zotonic_topic_media_resource","title":"Media resource","uri":"https:\/\/zotonic.com\/id\/2619"},"seq":5},{"created":"2026-09-09T13:48:11Z","object_id":{"id":2625,"is_a":["categorization","keyword","keyword_architecture"],"name":"zotonic_topic_model","title":"Model","uri":"https:\/\/zotonic.com\/id\/2625"},"seq":6},{"created":"2026-09-09T13:48:11Z","object_id":{"id":2643,"is_a":["categorization","keyword","keyword_task"],"name":"zotonic_topic_query","title":"Query","uri":"https:\/\/zotonic.com\/id\/2643"},"seq":7}],"predicate":{"id":308,"is_a":["meta","predicate"],"name":"subject","title":{"_type":"trans","tr":{"en":"Keyword"}},"uri":"http:\/\/purl.org\/dc\/elements\/1.1\/subject"}}},"export_revision":4161,"id":1284,"is_a":["text","documentation","reference","model"],"links":[{"rel":"self","target":"https:\/\/zotonic.com\/.zotonic\/websub\/topic\/1284"},{"rel":"hub","target":"https:\/\/zotonic.com\/.zotonic\/websub"}],"medium":null,"medium_url":null,"name":"doc_model_model_media","page_url":{"en":"https:\/\/zotonic.com\/docs\/1284\/media","x-default":"https:\/\/zotonic.com\/docs\/1284\/media"},"preview_url":null,"resource":{"version":4161,"pivot_location_lat":null,"title":"media","is_authoritative":true,"body":"<p>Access to data about uploaded files and other media.<\/p>\n<p>The <code>medium<\/code> (singular form of media) table stores all information of uploaded files or other media. Every resource can\ncontain a single medium. A resource with a medium is most often of the category image, audio, video or document.<\/p>\n<p>In template the <code>m_media<\/code> model is used to fetch the medium record by the resource id: <code>m.media[id]<\/code>. This is the same\nfunction as with <code>m.rsc[id].medium<\/code> except, that the m_rsc model does access control checks and the m_media does not.<\/p>\n<p>The <code>m_media<\/code> model implements all functions to handle media files and is used by other Erlang modules.<\/p>\n<p>Owned uploaded files and explicit previews use <code>m_cafs<\/code> for content-addressable\nstorage. <code>filename<\/code> and <code>preview_filename<\/code> remain logical filenames used by image\nand media URLs; they need not exist as individual files in the archive directory.\nResolve files through Zotonic&#39;s file APIs instead of constructing local paths.\nIdentical bytes can share storage without sharing resource permissions.<\/p>\n<p>The optional <code>digest<\/code> property is the lowercase hexadecimal SHA-256 digest of the\noriginal bytes. Legacy media can lack a digest or CAFS mapping; existing files are\npromoted on demand when their bytes are available. Reading a medium does not fetch\nremote bytes to calculate a digest. See <a href=\"\/id\/doc_model_model_m_cafs\" class=\"doc-reference doc-reference-model\"><code>model#m_cafs<\/code><\/a> for the storage lifecycle,\npreview retention, and internal Erlang APIs.<\/p>\n<h2>Copy a medium to another resource<\/h2>\n<p><code>m_media:migrate_to_cafs(Context)<\/code> queues a resumable scan of all current medium\nrecords. It requires <code>use mod_admin_config<\/code>. Owned originals and extracted preview\nimages are deduplicated into CAFS; logical filenames remain unchanged. Local bytes\nare hashed, while remote-only originals with a valid SHA-256 digest reuse their\nexisting remote location. Remote-only files without a digest are logged and skipped,\nnever downloaded for hashing. Retry tasks handle unavailable backends separately\nfrom the scan. Legacy remote copies are retired only after a healthy canonical\nremote copy exists; deletion follows filestore retention. Rerunning is safe.<\/p>\n<p><code>m_media:delete_generated_previews(Context)<\/code> queues background deletion of resized\nvariants and requires <code>use<\/code> permission on <code>mod_admin_config<\/code>. Original files and\n<code>preview_filename<\/code> images are preserved, including CAFS bytes shared with them.\nThe maintenance page exposes this operation. Remote deletion follows filestore\nretention; requests may regenerate previews during or after cleanup.<\/p>\n<p>Use <code>m_media:copy(Source, Destination, Context)<\/code> to replace an existing resource&#39;s\nmedium with a copy of another resource&#39;s medium. Both arguments accept resource\nids or names. The result is <code>ok<\/code> or <code>{error, Reason}<\/code>.<\/p>\n<pre class=\"notranslate\"><code class=\"notranslate language-erlang\">m_media:copy(SourceId, DestinationId, Context).\n<\/code><\/pre>\n<p>The caller must be allowed to view the source, edit the destination, and insert\nmedia of the source&#39;s MIME type and size. Permission failures return\n<code>{error, eacces}<\/code>; missing resources or a source without a medium return\n<code>{error, enoent}<\/code>. Copying a resource onto itself is a no-op after these checks.\nA missing source medium never clears the destination.<\/p>\n<p>The source is preserved. Destination resource properties, including category,\ntitle, and image-edit settings, are unchanged. Medium properties are copied with\nthe destination id and a new creation timestamp. Owned originals and explicit\npreviews receive independent logical filenames sharing CAFS bytes; mapped remote\nobjects need no download. Legacy owned files are promoted on demand. Non-owned\nfiles, including library assets, are snapshotted into CAFS under new owned filenames\nwithout changing the source reference. Failure to prepare either owned file leaves the\ndestination medium untouched and releases newly created aliases.<\/p>\n<p>Replacement uses the normal medium notifications, cache invalidation, MQTT update,\nand recovery retention for the destination&#39;s previous files. This is an Erlang API;\nno HTTP or MQTT copy endpoint is provided. <code>duplicate\/3<\/code> remains the internal\ninsertion-only helper used when duplicating a whole resource.<\/p>\n<h2>Properties of a medium record<\/h2>\n<p>A medium record has minimally the following properties, other properties can be added by modules.<\/p>\n<table class=\"table\"><thead><tr><th>Property<\/th><th>Description<\/th><th>Example value<\/th><\/tr><\/thead><tbody><tr><td>id<\/td><td>Id of the medium record, equal to the page id.<\/td><td><code>512<\/code><\/td><\/tr><tr><td>filename<\/td><td>Logical filename of the uploaded file. For legacy storage, this is relative to the archive directory.<\/td><td><code>&lt;&lt;&quot;2009\/10\/20\/zotonic-datamodel.jpg&quot;&gt;&gt;<\/code><\/td><\/tr><tr><td>rootname<\/td><td>Root name of the filename.<\/td><td><code>&lt;&lt;&quot;zotonic-datamodel&quot;&gt;&gt;<\/code><\/td><\/tr><tr><td>original_filename<\/td><td>Filename as suggested by the user agent when uploading the file. Directory paths are removed and unsafe characters replaced with underscores. The root name is limited to 80 Unicode characters; the extension is preserved.<\/td><td><code>&lt;&lt;&quot;Zotonic-datamodel.jpg&quot;&gt;&gt;<\/code><\/td><\/tr><tr><td>mime<\/td><td>Mime type of the medium.<\/td><td><code>&lt;&lt;&quot;image\/jpeg&quot;&gt;&gt;<\/code><\/td><\/tr><tr><td>width<\/td><td>Width in pixels.<\/td><td><code>536<\/code><\/td><\/tr><tr><td>height<\/td><td>Height in pixels.<\/td><td><code>737<\/code><\/td><\/tr><tr><td>orientation<\/td><td>Exif oritentation of the image.<\/td><td><code>1<\/code><\/td><\/tr><tr><td>sha1<\/td><td>Optional sha1 checksum of uploaded file. Undefined when not present.<\/td><td><\/td><\/tr><tr><td>digest<\/td><td>Optional SHA-256 digest of the original bytes as 64 lowercase hexadecimal characters. Legacy media can lack this value.<\/td><td><\/td><\/tr><tr><td>size<\/td><td>Size in bytes of the uploaded file.<\/td><td><code>71585<\/code><\/td><\/tr><tr><td>preview_filename<\/td><td>Optional filename for a generated file preview.<\/td><td><\/td><\/tr><tr><td>preview_width<\/td><td>Optional. Width of the generated preview.<\/td><td><\/td><\/tr><tr><td>preview_height<\/td><td>Optional. Height of the generated preview.<\/td><td><\/td><\/tr><tr><td>is_deletable_file<\/td><td>If the file should be deleted when the medium record is deleted. A boolean.<\/td><td><code>true<\/code><\/td><\/tr><tr><td>is_deletable_preview<\/td><td>If the optionally generated preview file should be deleted when the medium record is deleted. A boolean.<\/td><td><code>false<\/code><\/td><\/tr><tr><td>created<\/td><td>Timestamp when the medium record is created.<\/td><td><code>{{2009,10,20},{13,47,27}}<\/code><\/td><\/tr><\/tbody><\/table>\n<h2>Available Model API Paths<\/h2>\n<table class=\"table\"><thead><tr><th>Method<\/th><th>Path pattern<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>get<\/code><\/td><td><code>\/+id\/...<\/code><\/td><td>Return medium record for resource <code>+id<\/code> when the resource is visible; returns <code>enoent<\/code> for unknown resource and <code>eacces<\/code> when not visible.<\/td><\/tr><tr><td><code>post<\/code><\/td><td><code>\/upload_check<\/code><\/td><td>Check payload <code>digest<\/code> (SHA-256), <code>size<\/code>, and <code>filename<\/code> for a visible current CAFS original. Returns <code>needed: true<\/code> on a miss, or <code>needed: false<\/code> plus <code>media_upload_token<\/code>.<\/td><\/tr><tr><td><code>post<\/code><\/td><td><code>\/+id<\/code><\/td><td>Replace medium of existing resource <code>+id<\/code> from payload <code>url<\/code> or uploaded <code>file<\/code> (<code>#upload<\/code>); optional payload <code>rsc<\/code> map updates resource props. No further lookups.<\/td><\/tr><tr><td><code>post<\/code><\/td><td><code>\/<\/code><\/td><td>Create a new resource+medium from payload <code>url<\/code> or uploaded <code>file<\/code> (<code>#upload<\/code>), with optional payload <code>rsc<\/code> properties for the new resource. No further lookups.<\/td><\/tr><tr><td><code>delete<\/code><\/td><td><code>\/+id<\/code><\/td><td>Delete medium linked to resource <code>+id<\/code> (queues file cleanup, emits media events); requires editable resource access. No further lookups.<\/td><\/tr><\/tbody><\/table>\n<p>Upload clients can first call <code>\/upload_check<\/code>. Unknown, inaccessible and unavailable\nfiles return the same miss response. A successful token is signed, user-bound,\nand valid for one hour. Pass <code>file: {media_upload_token: Token}<\/code> to the insert or\nreplace API, or use this map as a wired form field value. Source visibility and\nits current CAFS mapping are rechecked at submission. Destination ACLs, scanning,\nsanitization and media observers still run normally. Remote-only CAFS bytes can\nbe fetched server-side for this processing. Visible legacy originals with a\nmatching checksum are lazily promoted before reuse. Originals without a known checksum and files changed by previous\nconversion require a normal upload.<\/p>\n<p><code>\/+name<\/code> marks a variable path segment. A trailing <code>\/...<\/code> means extra path segments are accepted for further lookups.<\/p>","slug":"media","is_protected":false,"visible_for":0,"tz":"UTC","language":["en"],"doc_source_hash":"940431bb968cfe2b8e365ad66010492f82cc2b33ef31d32bb27c7138e5cdcbf6","is_featured":false,"content_group_id":{"id":2551,"is_a":["meta","content_group"],"name":"content_group_imported_docs","title":"Imported documentation","uri":"https:\/\/zotonic.com\/id\/content_group_imported_docs"},"category_id":{"id":322,"is_a":["meta","category"],"name":"model","title":"Models","uri":"https:\/\/test.zotonic.com\/id\/322"},"doc_source_path":"apps\/zotonic_core\/src\/models\/m_media.erl","publication_start":"2023-06-24T08:17:58Z","github_url":"https:\/\/github.com\/zotonic\/zotonic\/blob\/master\/apps\/zotonic_core\/src\/models\/m_media.erl","pivot_location_lng":null,"doc_source_kind":"model","name":"doc_model_model_media","is_unfindable":false,"is_published":true,"pivot_geocode":null,"created":"2020-05-30T05:47:16Z","uri":null,"doc_status":"current","is_dependent":false,"erlang_app":"zotonic_core","publication_end":"9999-06-01T00:00:00Z","modifier_id":{"id":1,"is_a":["person"],"name":"administrator","title":"Site Administrator","uri":"https:\/\/zotonic.com\/id\/1"},"privacy":0,"doc_source_commit":"f3c6a9a2dde17cb5b121dd3e3018d9ef4b1b3943\n","erlang_module":"m_media","creator_id":{"id":336,"is_a":["person","robot"],"name":"gitbot","title":"Git","uri":"https:\/\/zotonic.com\/id\/336"},"modified":"2026-11-09T21:39:45Z","title_slug":"media"},"uri":"https:\/\/zotonic.com\/id\/1284","uri_template":"https:\/\/zotonic.com\/id\/:id","version":4161,"websub":{"hub":"https:\/\/zotonic.com\/.zotonic\/websub","topic":"https:\/\/zotonic.com\/.zotonic\/websub\/topic\/1284"}},"status":"ok"}