mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-19 01:55:04 +01:00
feat(gui,fuse,core): video thumbnails in file views
My files, Shared, Trash and the launcher now ask the daemon for video thumbnails, not only image ones. Grid tiles with a video thumbnail show a small play badge. A `.ts` file is a video from 1 MiB up and a TypeScript source below that. One rule in pdfs-core (`is_video_file_name`) decides it for the file icon, the thumbnail gate, the launcher icon and preview, and the opener's `@media` / `@text` classes. The daemon also checks the MPEG-TS sync byte before it runs ffmpeg, so a large TypeScript bundle gets no retries. A video thumbnail no longer downloads the whole file into memory (B283): - the server thumbnail is tried first, at no content cost; - files up to 24 MiB come whole; - larger ones bring a 16 MiB head, plus the MP4/MOV `moov` box when it lies past the head, into a sparse staging file that ffmpeg reads. The 256 MiB cap is gone. Without ffmpeg, a video answers "no thumbnail" at once. The folder thumbnail build includes videos.
This commit is contained in:
1 parent
b49631e18f
commit
0183f17435
23 files changed
+4883
-3995
No files matched your search
@@ -1851,6 +1851,34 @@ pub fn is_thumbnail_image_name(name: &str) -> bool {
|
||||
|| RAW_THUMBNAIL_EXTENSIONS.contains(&extension.as_str())
|
||||
}
|
||||
|
||||
/// The smallest `.ts` file taken for an MPEG transport stream. `.ts` is
|
||||
/// TypeScript source as often as it is a recording, and the two sit far apart
|
||||
/// in size: a source file is kilobytes, while a second of broadcast video is
|
||||
/// already megabytes. Drive often stores either kind as
|
||||
/// `application/octet-stream`, so the size is the only signal a listing has.
|
||||
pub const TS_VIDEO_MIN_BYTES: u64 = 1024 * 1024;
|
||||
|
||||
/// Whether a file named `name`, of `size` bytes when the caller knows it, is a
|
||||
/// video. The extension decides, except for `.ts`, which counts as video only
|
||||
/// from [`TS_VIDEO_MIN_BYTES`] up. A `.ts` of unknown size counts as video, as
|
||||
/// [`PhotoKind::classify`] has it.
|
||||
pub fn is_video_file_name(name: &str, size: Option<u64>) -> bool {
|
||||
if PhotoKind::classify(Some(name), None) != PhotoKind::Video {
|
||||
return false;
|
||||
}
|
||||
let transport_stream = name
|
||||
.rsplit_once('.')
|
||||
.is_some_and(|(_, extension)| extension.eq_ignore_ascii_case("ts"));
|
||||
!transport_stream || size.is_none_or(|size| size >= TS_VIDEO_MIN_BYTES)
|
||||
}
|
||||
|
||||
/// Whether file listings show a thumbnail for a file named `name`, of `size`
|
||||
/// bytes when known: an image [`is_thumbnail_image_name`] accepts, or a video,
|
||||
/// whose thumbnail is a frame the daemon takes with ffmpeg.
|
||||
pub fn is_thumbnail_media_name(name: &str, size: Option<u64>) -> bool {
|
||||
is_thumbnail_image_name(name) || is_video_file_name(name, size)
|
||||
}
|
||||
|
||||
/// Whether `name` is one of the concrete RAW formats handled by the exiftool
|
||||
/// embedded-preview path. Deliberately excludes generic/legacy grab-bags whose
|
||||
/// preview availability is not dependable.
|
||||
@@ -2675,6 +2703,41 @@ mod tests {
|
||||
assert_eq!(PhotoKind::classify(Some("x.jpg"), Some("video/mp4")), Photo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ts_file_is_a_video_only_at_the_size_of_one() {
|
||||
const GB: u64 = 1024 * 1024 * 1024;
|
||||
assert!(!is_video_file_name("app.ts", Some(3 * 1024)));
|
||||
assert!(!is_video_file_name(
|
||||
"bundle.TS",
|
||||
Some(TS_VIDEO_MIN_BYTES - 1)
|
||||
));
|
||||
assert!(is_video_file_name("recording.ts", Some(9 * GB)));
|
||||
assert!(is_video_file_name("recording.TS", Some(TS_VIDEO_MIN_BYTES)));
|
||||
assert!(is_video_file_name("recording.ts", None));
|
||||
// Only `.ts` is in doubt; every other video name is one at any size.
|
||||
assert!(is_video_file_name("clip.mkv", Some(10)));
|
||||
assert!(is_video_file_name("clip.m2ts", Some(10)));
|
||||
assert!(!is_video_file_name("notes.txt", Some(9 * GB)));
|
||||
assert!(!is_video_file_name("ts", Some(9 * GB)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listings_thumbnail_images_and_videos() {
|
||||
assert!(is_thumbnail_media_name("photo.JPG", Some(10)));
|
||||
assert!(is_thumbnail_media_name("camera.nef", None));
|
||||
assert!(is_thumbnail_media_name("clip.mp4", Some(10)));
|
||||
assert!(is_thumbnail_media_name(
|
||||
"recording.ts",
|
||||
Some(64 * 1024 * 1024)
|
||||
));
|
||||
assert!(!is_thumbnail_media_name("app.ts", Some(2048)));
|
||||
assert!(!is_thumbnail_media_name(
|
||||
"song.flac",
|
||||
Some(64 * 1024 * 1024)
|
||||
));
|
||||
assert!(!is_thumbnail_media_name("notes.txt", None));
|
||||
}
|
||||
|
||||
/// Keep every extension accepted by the shared classifier covered. Drive
|
||||
/// activation uses this classification before it knows whether opening a
|
||||
/// file should stream through FUSE or materialise a complete cache entry;
|
||||
|
||||
@@ -27,6 +27,7 @@ use std::os::unix::process::CommandExt;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use crate::control::is_video_file_name;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Environment override for the terminal used by `"terminal": true` rules.
|
||||
@@ -108,11 +109,12 @@ impl OpenWith {
|
||||
/// a Markdown file). The rules are about *what the user opened*, so the
|
||||
/// caller passes that name and the argv still gets the real path.
|
||||
pub fn command_for_named(&self, path: &Path, name: &str, is_dir: bool) -> Vec<String> {
|
||||
let kind = Kind::of(path, name, is_dir);
|
||||
for rule in &self.rules {
|
||||
if rule.command.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !rule.patterns.iter().any(|p| matches(p, name, is_dir)) {
|
||||
if !rule.patterns.iter().any(|p| matches(p, name, kind)) {
|
||||
continue;
|
||||
}
|
||||
let argv = expand(&rule.command, path);
|
||||
@@ -325,16 +327,46 @@ fn expand_env(token: &str) -> Option<String> {
|
||||
value.filter(|v| !v.is_empty())
|
||||
}
|
||||
|
||||
/// What is being opened, as far as the `@class` patterns care.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
Dir,
|
||||
Video,
|
||||
File,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
/// `.ts` names TypeScript and MPEG transport streams alike, so for that
|
||||
/// one extension the size on disk decides (see
|
||||
/// [`is_video_file_name`](crate::control::is_video_file_name)). No other
|
||||
/// name costs a `stat`.
|
||||
fn of(path: &Path, name: &str, is_dir: bool) -> Kind {
|
||||
if is_dir {
|
||||
return Kind::Dir;
|
||||
}
|
||||
let size = extension(name)
|
||||
.eq_ignore_ascii_case("ts")
|
||||
.then(|| std::fs::metadata(path).ok().map(|meta| meta.len()))
|
||||
.flatten();
|
||||
if is_video_file_name(name, size) {
|
||||
Kind::Video
|
||||
} else {
|
||||
Kind::File
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `pattern` — a glob or an `@class` — matches this name.
|
||||
fn matches(pattern: &str, name: &str, is_dir: bool) -> bool {
|
||||
fn matches(pattern: &str, name: &str, kind: Kind) -> bool {
|
||||
let name_lower = name.to_lowercase();
|
||||
let file = kind == Kind::File;
|
||||
match pattern.to_lowercase().as_str() {
|
||||
"@any" => true,
|
||||
"@dir" => is_dir,
|
||||
"@text" => !is_dir && class_text(&name_lower),
|
||||
"@document" => !is_dir && class_document(&name_lower),
|
||||
"@image" => !is_dir && class_image(&name_lower),
|
||||
"@media" => !is_dir && class_media(&name_lower),
|
||||
"@dir" => kind == Kind::Dir,
|
||||
"@text" => file && class_text(&name_lower),
|
||||
"@document" => file && class_document(&name_lower),
|
||||
"@image" => file && class_image(&name_lower),
|
||||
"@media" => kind == Kind::Video || (file && class_media(&name_lower)),
|
||||
other if other.starts_with('@') => false,
|
||||
other => glob_match(other, &name_lower),
|
||||
}
|
||||
@@ -533,6 +565,37 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ts_file_opens_as_media_or_text_by_its_size() {
|
||||
let policy = OpenWith {
|
||||
terminal: Some(vec!["alacritty".into(), "-e".into()]),
|
||||
default: None,
|
||||
rules: vec![
|
||||
rule(&["@text"], &["nvim"], true),
|
||||
rule(&["@media"], &["mpv"], false),
|
||||
],
|
||||
};
|
||||
let dir = std::env::temp_dir().join(format!("pdfs-opener-ts-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let source = dir.join("blob-source");
|
||||
std::fs::write(&source, "export const answer = 42;\n").unwrap();
|
||||
let recording = dir.join("blob-recording");
|
||||
std::fs::File::create(&recording)
|
||||
.unwrap()
|
||||
.set_len(crate::control::TS_VIDEO_MIN_BYTES)
|
||||
.unwrap();
|
||||
|
||||
let source_argv = policy.command_for_named(&source, "app.ts", false);
|
||||
let recording_argv = policy.command_for_named(&recording, "recording.ts", false);
|
||||
// A file that is not on this machine yet has no size to go by.
|
||||
let unknown_argv = policy.command_for_named(&dir.join("missing"), "recording.ts", false);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
|
||||
assert_eq!(source_argv[..3], ["alacritty", "-e", "nvim"]);
|
||||
assert_eq!(recording_argv[0], "mpv");
|
||||
assert_eq!(unknown_argv[0], "mpv");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_policy_is_xdg_open() {
|
||||
let policy = OpenWith::default();
|
||||
|
||||
Reference in new issue
Block a user