mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-11 00:11:46 +00:00
address review: read footage frames by Path so non-UTF-8 file names still import
This commit is contained in:
1 parent
2577b7fc87
commit
cf54235760
2 files changed
+14
-5
No files matched your search
@@ -18,7 +18,7 @@ pub(crate) fn filled<T: Clone>(len: usize, value: T, purpose: &str) -> Result<Ve
|
||||
/// Reads a regular file of at most `max` bytes. The opened handle must be a regular file (a
|
||||
/// device such as `/dev/zero` never ends) and reading stops one byte past `max`, so neither a
|
||||
/// special file nor one that grows after a size check can fill memory.
|
||||
pub(crate) fn read_file_within(path: &str, max: u64) -> std::io::Result<Vec<u8>> {
|
||||
pub(crate) fn read_file_within(path: impl AsRef<std::path::Path>, max: u64) -> std::io::Result<Vec<u8>> {
|
||||
use std::io::{Error, ErrorKind, Read};
|
||||
let file = std::fs::File::open(path)?;
|
||||
if !file.metadata()?.is_file() {
|
||||
@@ -150,4 +150,16 @@ mod tests {
|
||||
#[cfg(unix)]
|
||||
assert!(read_file_within("/dev/zero", 1 << 30).is_err_and(|e| e.kind() == std::io::ErrorKind::InvalidInput));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn read_file_within_reads_a_path_that_is_not_utf8() {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
let dir = std::env::temp_dir().join(format!("pc-read-within-nonutf8-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join(std::ffi::OsStr::from_bytes(b"frame-\xff.png"));
|
||||
std::fs::write(&path, [3u8; 4]).unwrap();
|
||||
assert_eq!(read_file_within(&path, 10).unwrap(), vec![3u8; 4]);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -387,10 +387,7 @@ fn load_frames(path: &str, fmt: photocraft_color::PixelFormat, opts: &photocraft
|
||||
Ok(())
|
||||
};
|
||||
for f in &files {
|
||||
let bytes = f
|
||||
.to_str()
|
||||
.map_or_else(|| Err(std::io::Error::other("path is not UTF-8")), |p| crate::allocation::read_file_within(p, max_footage_bytes))
|
||||
.map_err(|e| EngineError::Other(format!("read `{}`: {e}", f.display())))?;
|
||||
let bytes = crate::allocation::read_file_within(f, max_footage_bytes).map_err(|e| EngineError::Other(format!("read `{}`: {e}", f.display())))?;
|
||||
let name = f.to_string_lossy();
|
||||
// A GIF (a still one is its only frame) or an animated WebP brings every frame.
|
||||
let animation = match photocraft_codecs::detect(&bytes) {
|
||||
|
||||
Reference in new issue
Block a user