Skip to content

Commit 9e17513

Browse files
Codys-Wrightclaude
andcommitted
A library song's small files in one request
The library source reads a song's documents — its session's objects and its waveforms — through the server's files/{root}/docs batch (task #138), the records the share source already reads, with the signed-in person's token. Read one by one they took 19 s a song. Where the server has no such route, or refuses, they are read one by one as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cda8add commit 9e17513

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎apps/session-daw/src/song_stream.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,39 @@ impl SongSource for TaskSource {
227227
Box::pin(async move { library.song_chart(&slug).await })
228228
}
229229

230+
/// The song's small files in one request (the server's
231+
/// `files/{root}/docs`) — its session's hundreds of objects and its
232+
/// waveforms, which read one by one took 19 s a song. `None` when the
233+
/// server has no such request, or it failed: they are read one by one.
234+
fn read_many(&self, paths: Vec<String>) -> Pending<Option<Vec<(String, Option<Vec<u8>>)>>> {
235+
let url = self.song.docs_url();
236+
let token = self.song.token().map(str::to_owned);
237+
Box::pin(async move {
238+
let mut request = reqwest::Client::new()
239+
.post(url)
240+
.header(reqwest::header::CONTENT_TYPE, "text/plain")
241+
.body(paths.join("\n"));
242+
if let Some(token) = token {
243+
request = request.bearer_auth(token);
244+
}
245+
let response = request.send().await.ok()?;
246+
if !response.status().is_success() {
247+
tracing::warn!(
248+
http.status = response.status().as_u16(),
249+
"library: the documents batch was refused; reading them one by one"
250+
);
251+
return None;
252+
}
253+
let body = response.bytes().await.ok()?;
254+
Some(
255+
batch_records(&body)?
256+
.into_iter()
257+
.map(|(path, status, bytes)| (path, (status == 0).then_some(bytes)))
258+
.collect(),
259+
)
260+
})
261+
}
262+
230263
fn fetch_whole(
231264
&self,
232265
path: String,

‎crates/session/library/src/lib.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ impl Library {
144144
format!("{}/org/{}/vox", self.server.trim_end_matches('/'), self.org)
145145
}
146146

147+
/// The server over HTTP — `https://host` for `wss://host`.
148+
#[must_use]
149+
pub fn http_base(&self) -> String {
150+
self.server
151+
.trim_end_matches('/')
152+
.replacen("wss://", "https://", 1)
153+
.replacen("ws://", "http://", 1)
154+
}
155+
147156
async fn client<C: vox_core::FromVoxLane + 'static>(&self) -> eyre::Result<C> {
148157
task_dial::establish_at::<C>(&self.org_url(), self.token.as_deref())
149158
.await
@@ -786,6 +795,25 @@ impl TaskSong {
786795
eyre::bail!("{}", errors.join(" (then) "))
787796
}
788797

798+
/// Where many of the song's documents are read in one request (the
799+
/// server's `files/{root}/docs`: the paths in the body, one per line;
800+
/// a record per path back).
801+
#[must_use]
802+
pub fn docs_url(&self) -> String {
803+
format!(
804+
"{}/org/{}/files/{}/docs",
805+
self.library.http_base(),
806+
self.library.org,
807+
self.root.get()
808+
)
809+
}
810+
811+
/// The token the server knows the signed-in person by, if any.
812+
#[must_use]
813+
pub fn token(&self) -> Option<&str> {
814+
self.library.token.as_deref()
815+
}
816+
789817
/// The whole of `path`, streamed into `sink` a piece at a time — one
790818
/// request for the file, not one per range: how a download fetches a
791819
/// proxy. No timeout of its own (a proxy is tens of megabytes, and a

0 commit comments

Comments
 (0)