minor fixes and formatting

This commit is contained in:
Jörn-Michael Miehe 2025-05-27 20:00:21 +00:00
parent 9a1cabcb06
commit 5fdbe2bc6c
5 changed files with 27 additions and 46 deletions

9
.vscode/tasks.json vendored
View file

@ -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",
"--",

View file

@ -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:?}");

View file

@ -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);

View file

@ -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<PathBuf>) -> io::Result<Self> {
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::<u64>()
.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()));

View file

@ -17,8 +17,7 @@ impl<'t> Share<'t> {
alias: &'t Alias,
data: NewShareRequest,
) -> Result<Self, ureq::Error> {
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()