Files
photoreport/apps/desktop/src-tauri/src/camera_gphoto.rs
T
spark e6b5f2e390
ci / rust (pull_request) Successful in 11m45s
ci / typescript (pull_request) Failing after 14m55s
Let a Windows release, a Linux or Windows cable, and a future Developer ID join the desktop build.
The installer job was waiting on a commented matrix row, USB stayed on Image Capture, face detection could not read the Windows CPU, and notarisation had nowhere to put an Apple account.
2026-11-26 15:09:36 +00:00

183 lines
4.8 KiB
Rust

//! Linux USB via libgphoto2. The camera is opened for this pass only and dropped before we return,
//! which closes the PTP session.
use std::path::Path;
pub struct Shot {
pub folder: String,
pub name: String,
pub size: u64,
pub created_ms: Option<i64>,
}
pub struct Report {
pub model: Option<String>,
pub id: String,
pub error: Option<String>,
pub shots: Vec<Shot>,
pub downloaded: Vec<(String, u64)>,
pub clock: Option<String>,
pub clock_unsupported: bool,
}
pub fn pulse(dir: &Path, wanted: &[String], _sync_clock: bool) -> Result<Report, String> {
let context = gphoto2::Context::new().map_err(|err| err.to_string())?;
let mut listed = context
.list_cameras()
.wait()
.map_err(|err| err.to_string())?;
let Some(desc) = listed.next() else {
return Ok(empty());
};
let camera = context
.get_camera(&desc)
.wait()
.map_err(|err| err.to_string())?;
let model = camera.abilities().model().to_string();
let roots = card_roots(&camera);
let fs = camera.fs();
let mut shots = Vec::new();
for root in roots {
walk(&fs, &root, 0, &mut shots)?;
}
let mut downloaded = Vec::new();
let mut error = None;
for shot in &shots {
let asked = wanted.iter().any(|name| name == &shot.name);
if !asked {
continue;
}
match copy_one(&fs, dir, shot) {
Ok(size) => downloaded.push((shot.name.clone(), size)),
Err(err) => error = Some(err),
}
}
// Dropping `camera` closes the session before the caller waits.
drop(fs);
drop(camera);
Ok(Report {
model: Some(model),
id: "gphoto".into(),
error,
shots,
downloaded,
clock: None,
// libgphoto2 clock widgets differ per body; the panel says the clock was not set.
clock_unsupported: true,
})
}
fn empty() -> Report {
Report {
model: None,
id: "gphoto".into(),
error: None,
shots: Vec::new(),
downloaded: Vec::new(),
clock: None,
clock_unsupported: false,
}
}
fn card_roots(camera: &gphoto2::Camera) -> Vec<String> {
let mut roots = Vec::new();
if let Ok(storages) = camera.storages().wait() {
for storage in storages {
let Some(base) = storage.base_directory() else {
continue;
};
let text = base.to_string();
if text.is_empty() {
continue;
}
if !roots.iter().any(|have| have == &text) {
roots.push(text);
}
}
}
if roots.is_empty() {
roots.push("/".to_string());
}
roots
}
fn walk(
fs: &gphoto2::filesys::CameraFS<'_>,
folder: &str,
depth: u32,
shots: &mut Vec<Shot>,
) -> Result<(), String> {
if depth > 12 {
return Ok(());
}
let files = match fs.list_files(folder).wait() {
Ok(files) => files,
Err(err) if folder != "/" => {
let _ = err;
return Ok(());
}
Err(err) => return Err(err.to_string()),
};
for name in files {
if !is_still(&name) {
continue;
}
let info = fs.file_info(folder, &name).wait().ok();
let size = info
.as_ref()
.and_then(|info| info.file().size())
.unwrap_or(0);
let created_ms = info
.as_ref()
.and_then(|info| info.file().mtime())
.map(|mtime| {
let seconds = mtime as i64;
seconds.saturating_mul(1000)
});
shots.push(Shot {
folder: folder.to_string(),
name,
size,
created_ms,
});
}
let folders = match fs.list_folders(folder).wait() {
Ok(folders) => folders,
Err(_) => return Ok(()),
};
for name in folders {
let child = child_folder(folder, &name);
walk(fs, &child, depth + 1, shots)?;
}
Ok(())
}
fn copy_one(fs: &gphoto2::filesys::CameraFS<'_>, dir: &Path, shot: &Shot) -> Result<u64, String> {
let dest = dir.join(&shot.name);
if let Ok(meta) = std::fs::metadata(&dest) {
if meta.len() > 0 {
return Ok(meta.len());
}
}
let _ = std::fs::create_dir_all(dir);
fs.download_to(&shot.folder, &shot.name, &dest)
.wait()
.map_err(|err| err.to_string())?;
let meta = std::fs::metadata(&dest).map_err(|err| err.to_string())?;
Ok(meta.len())
}
fn child_folder(parent: &str, name: &str) -> String {
if name.starts_with('/') {
return name.to_string();
}
if parent == "/" {
return format!("/{name}");
}
format!("{parent}/{name}")
}
fn is_still(name: &str) -> bool {
crate::ftp::is_jpeg_name(name) || crate::ftp::is_raw_name(name)
}