diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/src/lib.rs | 70 |
1 files changed, 52 insertions, 18 deletions
diff --git a/client/src/lib.rs b/client/src/lib.rs index f88ecc5..e751958 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -63,6 +63,9 @@ pub struct Session { session_token: String, } +pub trait UnpinWrite: tokio::io::AsyncWrite + std::marker::Unpin {} +impl<T: tokio::io::AsyncWrite + std::marker::Unpin> UnpinWrite for T {} + impl Session { fn session_param(&self) -> String { format!("session={}", self.session_token) @@ -79,24 +82,65 @@ impl Session { .await?) } - // TODO use AssetRole instead of str pub async fn node_asset( &self, + writer: impl UnpinWrite, id: &str, role: AssetRole, width: usize, - mut writer: impl tokio::io::AsyncWrite + std::marker::Unpin, ) -> Result<()> { debug!("downloading asset {role:?} for {id:?}"); - let mut r = self - .client - .get(format!( + self.download_url( + writer, + format!( "{}/n/{id}/asset?role={}&width={width}", self.instance.base(), role.as_str() - )) - .send() - .await?; + ), + ) + .await + } + + pub async fn node_thumbnail( + &self, + writer: impl UnpinWrite, + id: &str, + width: usize, + time: f64, + ) -> Result<()> { + debug!("downloading thumbnail for {id:?} at {time}s"); + self.download_url( + writer, + format!( + "{}/n/{id}/thumbnail?t={time}&width={width}", + self.instance.base(), + ), + ) + .await + } + + pub async fn stream( + &self, + writer: impl UnpinWrite, + id: &str, + stream_spec: &StreamSpec, + ) -> Result<()> { + self.download_url(writer, self.stream_url(id, stream_spec)) + .await + } + + pub fn stream_url(&self, id: &str, stream_spec: &StreamSpec) -> String { + format!( + "{}/n/{}/stream?{}&{}", + self.instance.base(), + id, + stream_spec.to_query(), + self.session_param() + ) + } + + pub async fn download_url(&self, mut writer: impl UnpinWrite, url: String) -> Result<()> { + let mut r = self.client.get(url).send().await?; while let Some(chunk) = r.chunk().await? { writer.write_all(&chunk).await?; } @@ -113,14 +157,4 @@ impl Session { info!("done"); Ok(()) } - - pub fn stream(&self, id: &str, stream_spec: &StreamSpec) -> String { - format!( - "{}/n/{}/stream?{}&{}", - self.instance.base(), - id, - stream_spec.to_query(), - self.session_param() - ) - } } |