aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-01-16 21:53:40 +0100
committermetamuffin <metamuffin@disroot.org>2024-01-16 21:53:40 +0100
commit98b379afb31e455b529d443dcfc5797b8dd6723e (patch)
tree98ee2cbbca36fd7e5d4a4c4495a8d62144d4e3d1 /client
parent4558265c09dbb8ff53462ca842d82f2abfe39cb4 (diff)
downloadjellything-98b379afb31e455b529d443dcfc5797b8dd6723e.tar
jellything-98b379afb31e455b529d443dcfc5797b8dd6723e.tar.bz2
jellything-98b379afb31e455b529d443dcfc5797b8dd6723e.tar.zst
thumbnail generation
Diffstat (limited to 'client')
-rw-r--r--client/src/lib.rs70
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()
- )
- }
}