feat: preview video thumbnails in a floating window

This commit is contained in:
2026-10-09 22:08:34 +02:00
parent 656474df5b
commit 689f14a634
5 changed files with 1152 additions and 36 deletions
+136 -21
View File
@@ -7,6 +7,9 @@
//! Normal-mode d opens the full description; Up/Down scrolls lines and PgUp/PgDn scrolls pages.
//! The centered popup uses up to 70% width and 60% height, capped at 96 by 20 cells.
//! Esc closes the popup; j/k loads the selected video's description and i returns to editing.
//! Normal-mode t toggles a thumbnail in the same-sized popup; j/k follows selection, Esc closes.
//! Images use native terminal graphics when supported, otherwise colored half-block characters.
//! curl fetches JPEGs lazily into memory; decoding and image resizing/encoding run off-thread.
//! Channel categories live in `$XDG_CONFIG_HOME/tubular/config.toml`, or `~/.config/tubular/config.toml`.
//! Under `[categories]`, write `Rust = ["@RustVideos"]`; channel URLs and UC IDs also work.
//! Normal-mode c opens the category picker; n there prompts for a new name, Enter saves, Esc cancels.
@@ -17,6 +20,7 @@
//! See `--help` for controls; replacing playback or quitting cancels its processes.
mod config;
mod thumbnail;
mod youtube;
use std::{ffi::OsString, io, path::PathBuf, time::Duration};
@@ -36,7 +40,7 @@ use youtube::{Job, Playback, Video};
#[command(
version,
about = "Search YouTube and stream videos to your chosen player",
after_help = "Requires yt-dlp, FFmpeg, and a player on PATH.\nDefaults to IINA on macOS and mpv elsewhere; --player overrides the default.\nIINA receives '--stdin'; other players receive '-' to read audio/video from standard input.\nWithout a CLI query, start in INSERT; a CLI query is searched automatically in NORMAL.\nINSERT: type to edit; Enter submits if changed and returns to NORMAL; Esc returns without submitting.\nNORMAL: j/k or arrows select; Enter streams; i edits the query; d toggles description; c saves a channel; q quits.\nTab/Shift-Tab switch Search/category views; r reloads config and refreshes the current view.\nSubscriptions: $XDG_CONFIG_HOME/tubular/config.toml, or ~/.config/tubular/config.toml when XDG_CONFIG_HOME is unset/empty/relative.\nExample TOML:\n[categories]\nRust = [\"@RustVideos\"]\nChannel entries accept @handles, HTTPS YouTube channel URLs, or canonical UC channel IDs.\nFeeds combine up to 20 videos per channel's /videos tab, newest-first using approximate upload dates.\nCATEGORY PICKER: j/k or arrows choose; Enter saves; n prompts for a new category; Esc cancels.\nNEW CATEGORY: type a name; Enter creates it and saves the channel; Esc returns to the picker; Ctrl-U clears.\nOpening the description fetches full video metadata, preserving paragraphs and caching it for these results.\nThe centered description window uses up to 70% width and 60% height, capped at 96 by 20 cells.\nWhile it is open, Up/Down scroll lines and PgUp/PgDn scroll pages; j/k still selects videos.\nEsc closes the description; i closes it to edit the query. Scrolling stays within the text.\nCtrl-U clears in INSERT; Ctrl-C quits in any mode or popup.\nSelecting another video replaces playback. Quitting stops playback.\nExample: tubular --player mpv --player-arg=--volume=50 \"rust programming\""
after_help = "Requires yt-dlp, FFmpeg, and a player on PATH; thumbnails also use curl.\nDefaults to IINA on macOS and mpv elsewhere; --player overrides the default.\nIINA receives '--stdin'; other players receive '-' to read audio/video from standard input.\nWithout a CLI query, start in INSERT; a CLI query is searched automatically in NORMAL.\nINSERT: type to edit; Enter submits if changed and returns to NORMAL; Esc returns without submitting.\nNORMAL: j/k or arrows select; Enter streams; i edits the query; d toggles description; t toggles thumbnail; c saves a channel; q quits.\nTab/Shift-Tab switch Search/category views; r reloads config and refreshes the current view.\nSubscriptions: $XDG_CONFIG_HOME/tubular/config.toml, or ~/.config/tubular/config.toml when XDG_CONFIG_HOME is unset/empty/relative.\nExample TOML:\n[categories]\nRust = [\"@RustVideos\"]\nChannel entries accept @handles, HTTPS YouTube channel URLs, or canonical UC channel IDs.\nFeeds combine up to 20 videos per channel's /videos tab, newest-first using approximate upload dates.\nCATEGORY PICKER: j/k or arrows choose; Enter saves; n prompts for a new category; Esc cancels.\nNEW CATEGORY: type a name; Enter creates it and saves the channel; Esc returns to the picker; Ctrl-U clears.\nOpening the description fetches full video metadata, preserving paragraphs and caching it for these results.\nPreview windows use up to 70% width and 60% height, capped at 96 by 20 cells.\nIn descriptions, Up/Down scroll lines and PgUp/PgDn scroll pages; j/k still selects videos.\nEsc closes the description; i closes it to edit the query. Scrolling stays within the text.\nTHUMBNAIL: t or Esc closes; j/k or arrows load the selected video's image; d switches to description.\nThumbnails are fetched lazily into memory and fit proportionally inside the popup on resize.\nTerminal graphics are detected at startup, with colored half-block rendering when image protocols are unavailable.\nDownloading, JPEG decoding, and image resizing/encoding do not block navigation.\ni, c, view changes, and refresh dismiss the thumbnail; typing t in INSERT or a category name edits text.\nCtrl-U clears in INSERT; Ctrl-C quits in any mode or popup.\nSelecting another video replaces playback. Quitting stops playback.\nExample: tubular --player mpv --player-arg=--volume=50 \"rust programming\""
)]
struct Options {
/// Player executable (IINA or a player supporting '-' for standard input).
@@ -95,6 +99,8 @@ struct App {
views: Vec<View>,
view_index: usize,
category_dialog: Option<CategoryDialog>,
thumbnail_open: bool,
thumbnail: thumbnail::Thumbnail,
description_open: bool,
description_job: Option<(usize, Job)>,
description_error: Option<String>,
@@ -126,6 +132,8 @@ impl App {
views,
view_index: 0,
category_dialog: None,
thumbnail_open: false,
thumbnail: thumbnail::Thumbnail::new(),
description_open: false,
description_job: None,
description_error: None,
@@ -157,6 +165,7 @@ impl App {
self.job = None;
self.view_index = index;
}
self.close_thumbnail();
self.description_open = false;
self.description_job = None;
self.description_viewport = None;
@@ -177,6 +186,7 @@ impl App {
self.job = None;
self.description_open = false;
self.description_job = None;
self.close_thumbnail();
let view = &mut self.views[self.view_index];
view.videos.clear();
view.selection = TableState::default();
@@ -237,9 +247,11 @@ impl App {
self.job = None;
self.description_open = false;
self.description_job = None;
self.close_thumbnail();
}
fn refresh(&mut self) {
self.close_thumbnail();
match config::load(&self.config_path) {
Ok(config) => {
self.apply_config(config);
@@ -250,6 +262,7 @@ impl App {
}
fn open_category_picker(&mut self) {
self.close_thumbnail();
let view = &self.views[self.view_index];
let Some(video) = view
.selection
@@ -368,6 +381,13 @@ impl App {
self.category_dialog = Some(dialog);
}
fn close_thumbnail(&mut self) {
if self.thumbnail_open {
self.thumbnail_open = false;
self.thumbnail.cancel();
}
}
fn load_description(&mut self) {
self.description_job = None;
self.description_error = None;
@@ -483,6 +503,7 @@ impl App {
}
let selected_before = self.views[self.view_index].selection.selected();
let description_was_open = self.description_open;
let thumbnail_was_open = self.thumbnail_open;
if self.mode == Mode::Insert {
match key.code {
KeyCode::Esc => self.mode = Mode::Normal,
@@ -510,16 +531,32 @@ impl App {
KeyCode::Char('q') => return true,
KeyCode::Char('i') => {
self.description_open = false;
self.close_thumbnail();
self.mode = Mode::Insert;
}
KeyCode::Char('d') => {
self.close_thumbnail();
self.description_open = !self.description_open
&& self.views[self.view_index]
.selection
.selected()
.is_some_and(|index| index < self.views[self.view_index].videos.len());
}
KeyCode::Esc => self.description_open = false,
KeyCode::Char('t') => {
if self.thumbnail_open {
self.close_thumbnail();
} else {
self.thumbnail_open = self.views[self.view_index]
.selection
.selected()
.is_some_and(|index| index < self.views[self.view_index].videos.len());
}
self.description_open = false;
}
KeyCode::Esc => {
self.description_open = false;
self.close_thumbnail();
}
KeyCode::Down if self.description_open => {
self.description_scroll = self
.description_scroll
@@ -586,6 +623,16 @@ impl App {
{
self.load_description();
}
if self.thumbnail_open
&& (!thumbnail_was_open
|| self.views[self.view_index].selection.selected() != selected_before)
&& let Some(video) = self.views[self.view_index]
.selection
.selected()
.and_then(|index| self.views[self.view_index].videos.get(index))
{
self.thumbnail.select(&video.id);
}
false
}
@@ -656,8 +703,11 @@ impl App {
Mode::Normal if self.description_open => {
"NORMAL · ↑/↓ PgUp/PgDn scroll · j/k video · d/Esc close · i insert · q quit"
}
Mode::Normal if self.thumbnail_open => {
"NORMAL · j/k video · t/Esc close · d description · i insert · q quit"
}
Mode::Normal => {
"NORMAL · j/k select · Enter play · i search · d info · c save · Tab views · r refresh · q quit"
"NORMAL · j/k · Enter play · i search · d info · t image · c save · Tab views · r refresh · q quit"
}
};
frame.render_widget(Paragraph::new(help_text).dim(), help);
@@ -667,23 +717,7 @@ impl App {
.selected()
.and_then(|index| self.views[self.view_index].videos.get(index))
{
let area = frame.area();
let width = area
.width
.saturating_sub(4)
.min((u32::from(area.width) * 70 / 100) as u16)
.min(96);
let height = area
.height
.saturating_sub(4)
.min((u32::from(area.height) * 60 / 100) as u16)
.min(20);
let popup = Rect::new(
area.x + (area.width - width) / 2,
area.y + (area.height - height) / 2,
width,
height,
);
let popup = preview_area(frame.area());
frame.render_widget(Clear, popup);
let text = match video.description.as_deref() {
Some("") => "This video has no description.",
@@ -694,7 +728,10 @@ impl App {
.unwrap_or("Loading full description..."),
};
let paragraph = Paragraph::new(text).wrap(Wrap { trim: false });
let viewport = (width.saturating_sub(2), height.saturating_sub(2));
let viewport = (
popup.width.saturating_sub(2),
popup.height.saturating_sub(2),
);
if self.description_viewport != Some(viewport) {
self.description_max_scroll = if viewport.0 == 0 || viewport.1 == 0 {
0
@@ -721,6 +758,26 @@ impl App {
popup,
);
}
if self.thumbnail_open
&& let Some(video) = self.views[self.view_index]
.selection
.selected()
.and_then(|index| self.views[self.view_index].videos.get(index))
{
let popup = preview_area(frame.area());
frame.render_widget(Clear, popup);
let block = Block::bordered()
.title(" Thumbnail · t/Esc close ")
.title_bottom(Line::from(vec![
" ".into(),
video.title.as_str().into(),
" ".into(),
]))
.border_style(Style::new().fg(Color::Cyan));
let inner = block.inner(popup);
frame.render_widget(block, popup);
self.thumbnail.draw(frame, inner);
}
self.draw_category_dialog(frame);
}
@@ -804,6 +861,7 @@ impl App {
}
fn run(mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
self.thumbnail.query_terminal();
if !self.query.trim().is_empty() {
self.search();
}
@@ -821,6 +879,25 @@ impl App {
}
}
fn preview_area(area: Rect) -> Rect {
let width = area
.width
.saturating_sub(4)
.min((u32::from(area.width) * 70 / 100) as u16)
.min(96);
let height = area
.height
.saturating_sub(4)
.min((u32::from(area.height) * 60 / 100) as u16)
.min(20);
Rect::new(
area.x + (area.width - width) / 2,
area.y + (area.height - height) / 2,
width,
height,
)
}
fn main() -> io::Result<()> {
let options = Options::parse();
let config_path = config::config_path()?;
@@ -960,4 +1037,42 @@ mod tests {
assert!(!app.views[app.view_index].loaded);
assert!(app.job.is_none());
}
#[test]
fn thumbnails_are_normal_mode_only_and_other_popups_dismiss_them() {
let mut app = app(&["tubular"]);
app.key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::NONE));
assert_eq!(app.query, "t");
assert!(!app.thumbnail_open);
app.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
app.key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::NONE));
assert!(!app.thumbnail_open);
app.views[0].videos = youtube::search_results(
r#"{"entries":[{"id":"jNQXAC9IVRw","title":"Me at the zoo","channel_id":"UC4QobU6STFB0P71PMvOGN5A"}]}"#,
).unwrap();
app.views[0].videos[0].description = Some("A cached full description.".into());
app.views[0].selection.select(Some(0));
app.views[0].loaded = true;
app.thumbnail_open = true;
app.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE));
assert!(app.description_open);
assert!(!app.thumbnail_open);
assert!(app.description_job.is_none());
app.thumbnail_open = true;
app.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
assert!(!app.description_open);
assert!(!app.thumbnail_open);
assert_eq!(app.mode, Mode::Normal);
app.thumbnail_open = true;
app.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::NONE));
assert!(app.category_dialog.is_some());
assert!(!app.thumbnail_open);
app.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
app.thumbnail_open = true;
app.key(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
assert_eq!(app.mode, Mode::Insert);
assert!(!app.thumbnail_open);
}
}
+301
View File
@@ -0,0 +1,301 @@
use std::{
io::Cursor,
sync::mpsc::{self, Receiver, TryRecvError},
thread,
};
use image::{ImageFormat, ImageReader, Limits};
use ratatui::{
Frame,
layout::{Alignment, Rect, Size},
widgets::{Paragraph, Wrap},
};
use ratatui_image::{
FilterType, Resize, StatefulImage,
picker::Picker,
thread::{ResizeRequest, ResizeResponse, ThreadProtocol},
};
use crate::youtube::ThumbnailJob;
struct ThumbnailImage {
id: String,
protocol: ThreadProtocol,
replies: Receiver<Result<ResizeResponse, String>>,
complete: bool,
}
pub struct Thumbnail {
picker: Picker,
selected: Option<String>,
fetch: Option<ThumbnailJob>,
decoder: Option<Receiver<Result<ThumbnailImage, String>>>,
image: Option<ThumbnailImage>,
// ponytail: one completed thumbnail plus an in-flight replacement, not a video cache.
cached: Option<ThumbnailImage>,
error: Option<String>,
}
impl Thumbnail {
pub fn new() -> Self {
Self {
picker: Picker::halfblocks(),
selected: None,
fetch: None,
decoder: None,
image: None,
cached: None,
error: None,
}
}
/// Call once in raw mode/alternate screen, before the event loop reads stdin.
pub fn query_terminal(&mut self) {
self.picker = Picker::from_query_stdio().unwrap_or_else(|_| Picker::halfblocks());
let font = self.picker.font_size();
if font.width == 0 || font.height == 0 {
self.picker = Picker::halfblocks();
}
}
pub fn select(&mut self, id: &str) {
if self.selected.as_deref() == Some(id) {
return;
}
self.cancel();
self.selected = Some(id.to_owned());
if self.cached.as_ref().is_some_and(|image| image.id == id) {
self.image = self.cached.take();
return;
}
match ThumbnailJob::start(id) {
Ok(job) => self.fetch = Some(job),
Err(error) => self.error = Some(format!("Could not load thumbnail: {error}")),
}
}
pub fn cancel(&mut self) {
self.fetch = None;
// Each decode has its own receiver: dropping it invalidates even a queued completion.
self.decoder = None;
self.selected = None;
self.error = None;
if let Some(image) = self.image.take()
&& image.complete
{
self.cached = Some(image);
}
}
pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
self.poll();
if area.width == 0 || area.height == 0 || self.selected.is_none() {
return;
}
let mut message = self.error.as_deref().unwrap_or("Loading thumbnail...");
if let Some(image) = self.image.as_mut() {
let resize = Resize::Fit(Some(FilterType::Triangle));
if let Some(size) = image.protocol.size_for(resize.clone(), area.into()) {
frame.render_stateful_widget(
StatefulImage::new().resize(resize),
centered(area, size),
&mut image.protocol,
);
if image.protocol.protocol_type().is_some() {
return;
}
}
message = "Preparing thumbnail...";
} else if self.decoder.is_some() {
message = "Decoding thumbnail...";
}
frame.render_widget(
Paragraph::new(message)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true }),
Rect::new(
area.x,
area.y + area.height / 2,
area.width,
area.height - area.height / 2,
),
);
}
fn poll(&mut self) {
if let Some(result) = self.fetch.as_mut().and_then(ThumbnailJob::poll) {
self.fetch = None;
match result {
Ok(bytes) => {
let picker = self.picker.clone();
let id = self.selected.clone().expect("A fetch has a selected video");
let (tx, rx) = mpsc::channel();
match thread::Builder::new()
.name("thumbnail-decode".into())
.spawn(move || {
let _ = tx.send(prepare(id, bytes, picker));
}) {
Ok(_) => self.decoder = Some(rx),
Err(error) => {
self.error = Some(format!("Could not decode thumbnail: {error}"));
}
}
}
Err(error) => self.error = Some(format!("Could not load thumbnail: {error}")),
}
}
if let Some(result) = self.decoder.as_ref().map(Receiver::try_recv) {
match result {
Ok(result) => {
self.decoder = None;
match result {
Ok(image) => self.image = Some(image),
Err(error) => self.error = Some(error),
}
}
Err(TryRecvError::Disconnected) => {
self.decoder = None;
self.error = Some("Thumbnail decoder stopped before completing.".into());
}
Err(TryRecvError::Empty) => {}
}
}
if let Some(image) = self.image.as_mut() {
match image.replies.try_recv() {
Ok(Ok(response)) => {
// ThreadProtocol rejects obsolete requests; the widget checks the current
// area again before rendering, so rapid resizes cannot draw an old size.
if image.protocol.update_resized_protocol(response) {
image.complete = true;
self.cached = None;
}
}
Ok(Err(error)) => {
self.image = None;
self.error = Some(format!("Could not render thumbnail: {error}"));
}
Err(TryRecvError::Disconnected) => {
self.image = None;
self.error = Some("Thumbnail renderer stopped before completing.".into());
}
Err(TryRecvError::Empty) => {}
}
}
}
}
fn prepare(id: String, bytes: Vec<u8>, picker: Picker) -> Result<ThumbnailImage, String> {
let image = decode(bytes)?;
let protocol = picker.new_resize_protocol(image);
let (requests_tx, requests_rx) = mpsc::channel::<ResizeRequest>();
let (replies_tx, replies_rx) = mpsc::channel();
thread::Builder::new()
.name("thumbnail-resize".into())
.spawn(move || {
for request in requests_rx {
if replies_tx
.send(request.resize_encode().map_err(|error| error.to_string()))
.is_err()
{
break;
}
}
})
.map_err(|error| format!("Could not prepare thumbnail renderer: {error}"))?;
Ok(ThumbnailImage {
id,
protocol: ThreadProtocol::new(requests_tx, Some(protocol)),
replies: replies_rx,
complete: false,
})
}
fn decode(bytes: Vec<u8>) -> Result<image::DynamicImage, String> {
if bytes.len() > 2 * 1024 * 1024 {
return Err("Thumbnail exceeds the 2 MiB download limit.".into());
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), ImageFormat::Jpeg);
let mut limits = Limits::default();
limits.max_image_width = Some(4096);
limits.max_image_height = Some(4096);
limits.max_alloc = Some(16 * 1024 * 1024);
reader.limits(limits);
reader
.decode()
.map_err(|error| format!("Could not decode thumbnail JPEG: {error}"))
}
fn centered(area: Rect, size: Size) -> Rect {
let width = size.width.min(area.width);
let height = size.height.min(area.height);
Rect::new(
area.x + (area.width - width) / 2,
area.y + (area.height - height) / 2,
width,
height,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_corrupt_and_oversized_jpeg_input() {
assert!(decode(b"not a JPEG".to_vec()).is_err());
assert!(decode(vec![0; 2 * 1024 * 1024 + 1]).is_err());
}
#[test]
fn cancellation_discards_queued_decode_completion() {
let mut thumbnail = Thumbnail::new();
let (tx, rx) = mpsc::channel();
let (requests_tx, _requests_rx) = mpsc::channel();
let (_replies_tx, replies_rx) = mpsc::channel();
let protocol = thumbnail
.picker
.new_resize_protocol(image::DynamicImage::new_rgb8(1, 1));
thumbnail.selected = Some("old-video".into());
thumbnail.decoder = Some(rx);
assert!(
tx.send(Ok(ThumbnailImage {
id: "old-video".into(),
protocol: ThreadProtocol::new(requests_tx, Some(protocol)),
replies: replies_rx,
complete: false,
}))
.is_ok()
);
thumbnail.cancel();
thumbnail.selected = Some("new-video".into());
thumbnail.poll();
assert!(thumbnail.image.is_none());
assert!(thumbnail.error.is_none());
}
#[test]
fn resized_image_preserves_aspect_and_stays_inside_popup() {
let picker = Picker::halfblocks();
let font = picker.font_size();
let protocol = picker.new_resize_protocol(image::DynamicImage::new_rgb8(480, 360));
let area = Rect::new(7, 3, 80, 10);
let size = protocol.size_for(Resize::Fit(None), area.into());
let image_area = centered(area, size);
assert!(image_area.x >= area.x && image_area.right() <= area.right());
assert!(image_area.y >= area.y && image_area.bottom() <= area.bottom());
let left = image_area.x - area.x;
let right = area.right() - image_area.right();
let top = image_area.y - area.y;
let bottom = area.bottom() - image_area.bottom();
assert!(left.abs_diff(right) <= 1 && top.abs_diff(bottom) <= 1);
let pixel_width = u32::from(image_area.width) * u32::from(font.width);
let pixel_height = u32::from(image_area.height) * u32::from(font.height);
// Cell rounding may add at most one cell to either fitted pixel dimension.
assert!(
pixel_width.abs_diff(pixel_height * 4 / 3)
<= u32::from(font.width) + u32::from(font.height) * 4 / 3
);
assert_eq!(centered(Rect::new(7, 3, 0, 0), size), Rect::new(7, 3, 0, 0));
}
}
+116 -5
View File
@@ -45,13 +45,15 @@ fn valid_channel_id(id: &str) -> bool {
.all(|c| c.is_ascii_alphanumeric() || c == b'-' || c == b'_')
}
fn validate_video(video: &mut Video) -> Result<(), String> {
if video.id.len() != 11
|| !video
.id
fn valid_video_id(id: &str) -> bool {
id.len() == 11
&& id
.bytes()
.all(|c| c.is_ascii_alphanumeric() || c == b'-' || c == b'_')
{
}
fn validate_video(video: &mut Video) -> Result<(), String> {
if !valid_video_id(&video.id) {
return Err("an invalid YouTube video ID".into());
}
if video
@@ -222,6 +224,96 @@ impl Drop for Job {
}
}
const MAX_THUMBNAIL_BYTES: usize = 2 * 1024 * 1024;
pub struct ThumbnailJob {
child: Child,
output: Receiver<io::Result<(Vec<u8>, String)>>,
}
impl ThumbnailJob {
pub fn start(id: &str) -> io::Result<Self> {
if !valid_video_id(id) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid YouTube video ID",
));
}
let mut child = isolated_command(OsStr::new("curl"))
.args([
"--disable",
"--silent",
"--show-error",
"--fail",
"--location",
"--proto",
"=https",
"--proto-redir",
"=https",
"--connect-timeout",
"5",
"--max-time",
"15",
"--max-filesize",
"2097152", // 2 MiB; read_thumbnail also enforces the limit.
])
.arg(format!("https://i.ytimg.com/vi/{id}/hqdefault.jpg"))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|error| io::Error::new(error.kind(), format!("Cannot start curl: {error}")))?;
let stdout = child.stdout.take().expect("stdout is piped");
let errors = read_errors(child.stderr.take().expect("stderr is piped"));
let (sender, output) = mpsc::channel();
thread::spawn(move || {
let bytes = read_thumbnail(stdout);
let result = errors
.recv()
.unwrap_or_else(|_| Err(io::Error::other("curl error reader disconnected")))
.and_then(|errors| bytes.map(|bytes| (bytes, errors)));
let _ = sender.send(result);
});
Ok(Self { child, output })
}
pub fn poll(&mut self) -> Option<Result<Vec<u8>, String>> {
// EOF alone is not success: curl can write a partial body and then fail.
let status = match self.child.try_wait() {
Ok(Some(status)) => status,
Ok(None) => return None,
Err(error) => return Some(Err(format!("Cannot wait for curl: {error}"))),
};
match self.output.try_recv() {
Ok(Ok((bytes, _))) if status.success() => Some(Ok(bytes)),
Ok(Ok((_, errors))) => Some(Err(if errors.trim().is_empty() {
format!("curl exited with {status}")
} else {
errors.trim().to_owned()
})),
Ok(Err(error)) => Some(Err(format!("Cannot read thumbnail: {error}"))),
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => Some(Err("curl output reader disconnected".into())),
}
}
}
impl Drop for ThumbnailJob {
fn drop(&mut self) {
stop(&mut self.child);
}
}
fn read_thumbnail(pipe: impl Read) -> io::Result<Vec<u8>> {
let mut bytes = Vec::new();
pipe.take((MAX_THUMBNAIL_BYTES + 1) as u64)
.read_to_end(&mut bytes)?;
if bytes.len() > MAX_THUMBNAIL_BYTES {
return Err(io::Error::other("Thumbnail exceeds 2 MiB"));
}
Ok(bytes)
}
fn isolated_command(program: &OsStr) -> Command {
let mut command = Command::new(program);
#[cfg(unix)]
@@ -371,6 +463,25 @@ impl Drop for Playback {
mod tests {
use super::*;
#[test]
fn thumbnails_bound_binary_input_and_reject_unsafe_ids() {
let bytes = [0xff, 0xd8, 0x00, 0xfe];
assert_eq!(read_thumbnail(bytes.as_slice()).unwrap(), bytes);
assert_eq!(
read_thumbnail(io::repeat(0).take(MAX_THUMBNAIL_BYTES as u64))
.unwrap()
.len(),
MAX_THUMBNAIL_BYTES
);
assert!(read_thumbnail(io::repeat(0)).is_err());
for id in ["../secrets", "abcdefghij/", "abcdefghijé", "--bad"] {
assert!(matches!(
ThumbnailJob::start(id),
Err(error) if error.kind() == io::ErrorKind::InvalidInput
));
}
}
#[test]
fn search_handles_unavailable_entries_and_rejects_invalid_ids() {
let videos = search_results(