1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use serde_json::Value;
use crate::client::{encode_path_segment, Client};
use crate::error::Error;
use crate::types::{
CheckMediaParams, ListMediaParams, UpdateMediaParams, UploadMediaFromBase64Params,
UploadMediaFromUrlParams, UploadMediaParams,
};
/// `client.media()`: the workspace Media Library.
#[derive(Debug, Clone, Copy)]
pub struct Media<'a> {
pub(crate) client: &'a Client,
}
impl Media<'_> {
/// `GET /media` - list the media library (newest first).
pub async fn list(&self, params: ListMediaParams) -> Result<Value, Error> {
let mut query = Vec::new();
if let Some(limit) = params.limit {
query.push(("limit", limit.to_string()));
}
if let Some(offset) = params.offset {
query.push(("offset", offset.to_string()));
}
if let Some(search) = params.search {
query.push(("search", search));
}
if let Some(folder_id) = params.folder_id {
query.push(("folder_id", folder_id));
}
self.client.get("/media", query).await
}
/// `GET /media/:id` - fetch a single media item.
pub async fn get(&self, id: &str) -> Result<Value, Error> {
self.client
.get(&format!("/media/{}", encode_path_segment(id)), Vec::new())
.await
}
/// `POST /media/upload` - upload raw bytes as multipart form data (max
/// 50MB for images, 100MB request cap overall; use [`Self::upload_from_url`]
/// or [`Self::create_upload_url`] for larger videos, up to 1GB).
///
/// A PDF is rasterized into image slides and returned as a carousel
/// (`slides` + `media_ids` alongside `data`).
pub async fn upload(&self, params: UploadMediaParams) -> Result<Value, Error> {
let mut fields = Vec::new();
if let Some(name) = params.name {
fields.push(("name", name));
}
if let Some(folder) = params.folder {
fields.push(("folder", folder));
}
if let Some(folder_id) = params.folder_id {
fields.push(("folder_id", folder_id));
}
let filename = if params.filename.is_empty() {
"upload.bin".to_owned()
} else {
params.filename
};
self.client
.post_multipart("/media/upload", params.file, filename, fields)
.await
}
/// `POST /media/upload-from-url` - the server fetches a public URL
/// (files up to 1GB; large videos finish processing in the background
/// and come back with status `"processing"`).
pub async fn upload_from_url(&self, params: UploadMediaFromUrlParams) -> Result<Value, Error> {
self.client.post_json("/media/upload-from-url", ¶ms).await
}
/// `POST /media/upload-from-base64` - upload base64-encoded file data.
pub async fn upload_from_base64(
&self,
params: UploadMediaFromBase64Params,
) -> Result<Value, Error> {
self.client.post_json("/media/upload-from-base64", ¶ms).await
}
/// `POST /media/create-upload-url` - mint a one-time presigned upload URL
/// for large files (up to 1GB). POST the file as multipart form data
/// (field name `"file"`) to the returned `upload_url` within
/// `expires_in_seconds`; no auth headers are needed on that second request.
pub async fn create_upload_url(&self) -> Result<Value, Error> {
self.client.post_empty("/media/create-upload-url").await
}
/// `POST /media/check` - preflight a file's compatibility with the
/// workspace's connected platforms BEFORE uploading. Provide one of: a
/// public `url`, an existing `media_id`, or `size_bytes` + `mime`.
pub async fn check(&self, params: CheckMediaParams) -> Result<Value, Error> {
self.client.post_json("/media/check", ¶ms).await
}
/// `PATCH /media/:id` - rename a file and/or move it into a folder.
pub async fn update(&self, id: &str, params: UpdateMediaParams) -> Result<Value, Error> {
self.client
.patch_json(&format!("/media/{}", encode_path_segment(id)), ¶ms)
.await
}
/// `DELETE /media/:id` - delete a media file. Resolves to
/// [`Value::Null`] (204). Fails with 409 `media_in_use` when the file is
/// attached to a scheduled or publishing post.
pub async fn delete(&self, id: &str) -> Result<Value, Error> {
self.client
.delete(&format!("/media/{}", encode_path_segment(id)))
.await
}
}