From 5fdbe2bc6c90d252ffd3493cdced80c8a37e3425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 27 May 2025 20:00:21 +0000 Subject: [PATCH] minor fixes and formatting --- .vscode/tasks.json | 9 ------- src/main.rs | 2 +- src/sharry/file/chunks.rs | 3 +-- src/sharry/file/mod.rs | 49 ++++++++++++++++++--------------------- src/sharry/share.rs | 10 +++----- 5 files changed, 27 insertions(+), 46 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 1b38da9..932b214 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -15,13 +15,6 @@ "label": "Run Project", "type": "cargo", "command": "run", - "dependsOn": [ - "Build Project" - ], - "args": [ - "--bin=shrupl", - "--package=shrupl" - ], "env": { "RUST_LOG": "shrupl=info", }, @@ -34,8 +27,6 @@ "command": "clippy", "args": [ "--fix", - "--bin=shrupl", - "--package=shrupl", "--allow-dirty", "--allow-staged", "--", diff --git a/src/main.rs b/src/main.rs index f904a2d..0fa8e29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,7 @@ fn main() { let alias = Alias::new(Uri::with_protocol(args.proto, args.url), args.alias); - let share = NewShareRequest::new(args.name, args.desc, 10); + let share = NewShareRequest::new(args.name, args.desc, args.views); let share = Share::create(&agent, &alias, share).unwrap(); info!("share: {share:?}"); diff --git a/src/sharry/file/chunks.rs b/src/sharry/file/chunks.rs index c3e10e7..957f0fb 100644 --- a/src/sharry/file/chunks.rs +++ b/src/sharry/file/chunks.rs @@ -37,8 +37,7 @@ impl Iterator for FileChunks<'_> { f.seek(SeekFrom::Start(offset)).ok()?; let mut bytes = vec![0; self.csize]; - let read_len = f - .read(&mut bytes) + let read_len = (f.read(&mut bytes)) .inspect_err(|e| error!("Error reading file: {e}")) .ok()?; bytes.truncate(read_len); diff --git a/src/sharry/file/mod.rs b/src/sharry/file/mod.rs index 2ab36a0..1148405 100644 --- a/src/sharry/file/mod.rs +++ b/src/sharry/file/mod.rs @@ -1,6 +1,11 @@ mod chunks; -use std::{ffi::OsStr, fs::metadata, io, path::PathBuf}; +use std::{ + ffi::OsStr, + fs::metadata, + io::{self, ErrorKind}, + path::PathBuf, +}; use log::{debug, error}; use ureq::{Error::Other, http::StatusCode}; @@ -23,18 +28,19 @@ impl File { pub fn new(path: impl Into) -> io::Result { let path: PathBuf = path.into(); - let name = path - .file_name() - .and_then(OsStr::to_str) - .unwrap_or("file.bin") - .to_string(); + let m = metadata(&path)?; + if !m.is_file() { + return Err(io::Error::new(ErrorKind::NotFound, "not a file")); + } - let size = metadata(&path)?.len(); + let name = (path.file_name().and_then(OsStr::to_str)) + .ok_or_else(|| io::Error::new(ErrorKind::NotFound, "bad file name"))? + .to_string(); Ok(Self { path, name, - size, + size: m.len(), patch_uri: None, }) } @@ -44,12 +50,9 @@ impl File { return Err(Other("patch_uri already set".into())); } - let endpoint = share - .alias - .get_endpoint(format!("alias/upload/{}/files/tus", share.id)); + let endpoint = (share.alias).get_endpoint(format!("alias/upload/{}/files/tus", share.id)); - let res = http - .post(endpoint) + let res = (http.post(endpoint)) .sharry_header(share.alias) .header("Sharry-File-Name", &self.name) .header("Upload-Length", self.size) @@ -59,9 +62,7 @@ impl File { return Err(Other("unexpected response status".into())); } - let location = res - .headers() - .get("Location") + let location = (res.headers().get("Location")) .ok_or_else(|| Other("Location header not found".into()))? .to_str() .map_err(|_| Other("Location header invalid".into()))? @@ -87,15 +88,11 @@ impl File { alias: &Alias, chunk: &Chunk, ) -> Result<(), ureq::Error> { - let patch_uri = self - .patch_uri - .as_ref() - .ok_or_else(|| Other("unset patch_uri".into()))?; + let patch_uri = (self.patch_uri.as_ref()).ok_or_else(|| Other("unset patch_uri".into()))?; debug!("upload uri: {patch_uri:?}"); - let res = http - .patch(patch_uri) + let res = (http.patch(patch_uri)) .sharry_header(alias) .header("Upload-Offset", chunk.offset) .send(&chunk.bytes)?; @@ -104,14 +101,12 @@ impl File { return Err(Other("unexpected response status".into())); } - let offset = res - .headers() - .get("Upload-Offset") + let offset = (res.headers().get("Upload-Offset")) .ok_or_else(|| Other("Upload-Offset header not found".into()))? .to_str() - .map_err(|_| Other("Upload-Offset header invalid".into()))? + .map_err(|e| Other(e.into()))? .parse::() - .map_err(|_| Other("Upload-Offset header not an integer".into()))?; + .map_err(|e| Other(e.into()))?; if chunk.after() != offset { return Err(Other("unexpected offset response".into())); diff --git a/src/sharry/share.rs b/src/sharry/share.rs index f5b385b..b374284 100644 --- a/src/sharry/share.rs +++ b/src/sharry/share.rs @@ -17,8 +17,7 @@ impl<'t> Share<'t> { alias: &'t Alias, data: NewShareRequest, ) -> Result { - let res = http - .post(alias.get_endpoint("alias/upload/new")) + let res = (http.post(alias.get_endpoint("alias/upload/new"))) .sharry_header(alias) .send_json(data)? .body_mut() @@ -34,12 +33,9 @@ impl<'t> Share<'t> { } pub fn notify(&self, http: &ureq::Agent) -> Result<(), ureq::Error> { - let endpoint = self - .alias - .get_endpoint(format!("alias/mail/notify/{}", self.id)); + let endpoint = (self.alias).get_endpoint(format!("alias/mail/notify/{}", self.id)); - let res = http - .post(endpoint) + let res = (http.post(endpoint)) .sharry_header(self.alias) .send_empty()? .body_mut()