file creation works

This commit is contained in:
Jörn-Michael Miehe 2025-05-23 01:11:11 +00:00
parent 9436e2afe9
commit 485c2cf21c
3 changed files with 73 additions and 19 deletions

View file

@ -1,10 +1,7 @@
mod sharry; mod sharry;
use log::info; use log::info;
use sharry::{ use sharry::{Alias, NewShareRequest, URI};
Alias,
api::{NewShareRequest, URI},
};
use std::time::Duration; use std::time::Duration;
use ureq::Agent; use ureq::Agent;
@ -26,5 +23,7 @@ fn main() {
let share = share.unwrap(); let share = share.unwrap();
info!("share: {:?}", share); info!("share: {:?}", share);
share.create_file(&agent, "./myfile.dat").unwrap();
share.notify(&agent).unwrap(); share.notify(&agent).unwrap();
} }

View file

@ -1,3 +1,5 @@
use std::fmt::Display;
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -8,18 +10,18 @@ pub struct URI {
} }
impl URI { impl URI {
pub fn new_protocol(base_url: &str, protocol: &str) -> Self { pub fn new_protocol(base_url: impl Into<String>, protocol: impl Into<String>) -> Self {
Self { Self {
protocol: protocol.into(), protocol: protocol.into(),
base_url: base_url.into(), base_url: base_url.into(),
} }
} }
pub fn new(base_url: &str) -> Self { pub fn new(base_url: impl Into<String>) -> Self {
Self::new_protocol(base_url, "https") Self::new_protocol(base_url, "https")
} }
pub fn get_endpoint(&self, endpoint: &str) -> String { pub fn get_endpoint(&self, endpoint: impl Display) -> String {
let uri = format!("{}://{}/api/v2/{}", self.protocol, self.base_url, endpoint); let uri = format!("{}://{}/api/v2/{}", self.protocol, self.base_url, endpoint);
debug!("uri: {:?}", uri); debug!("uri: {:?}", uri);
@ -29,20 +31,24 @@ impl URI {
#[derive(Serialize)] #[derive(Serialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub struct NewShareRequest<'t> { pub struct NewShareRequest {
name: Option<&'t str>, name: Option<String>,
validity: u32, validity: u32,
description: &'t str, description: String,
maxViews: u32, maxViews: u32,
password: Option<&'t str>, password: Option<String>,
} }
impl<'t> NewShareRequest<'t> { impl NewShareRequest {
pub fn new(name: Option<&'t str>, description: &'t str, max_views: u32) -> Self { pub fn new(
name: Option<impl Into<String>>,
description: impl Into<String>,
max_views: u32,
) -> Self {
Self { Self {
name: name, name: name.map(Into::into),
validity: 0, validity: 0,
description: description, description: description.into(),
maxViews: max_views, maxViews: max_views,
password: None, password: None,
} }

View file

@ -1,7 +1,9 @@
pub mod api; mod api;
use api::{NewShareRequest, NewShareResponse, NotifyShareResponse, URI}; pub use api::{NewShareRequest, URI};
use api::{NewShareResponse, NotifyShareResponse};
use log::debug; use log::debug;
use std::{ffi::OsStr, fs, path::Path};
use ureq::{self, RequestBuilder}; use ureq::{self, RequestBuilder};
#[derive(Debug)] #[derive(Debug)]
@ -11,7 +13,7 @@ pub struct Alias {
} }
impl Alias { impl Alias {
pub fn new(uri: URI, id: &str) -> Self { pub fn new(uri: URI, id: impl Into<String>) -> Self {
Self { uri, id: id.into() } Self { uri, id: id.into() }
} }
@ -50,7 +52,54 @@ pub struct Share<'t> {
} }
impl<'t> Share<'t> { impl<'t> Share<'t> {
pub fn add_file(&self) {} pub fn create_file(
&self,
http: &ureq::Agent,
path: impl AsRef<OsStr> + AsRef<Path>,
) -> Result<(), ureq::Error> {
let filename = Path::new(&path)
.file_name()
.and_then(OsStr::to_str)
.unwrap_or("file.bin");
let size = fs::metadata(&path)?.len();
let endpoint = self
.alias
.uri
.get_endpoint(&format!("alias/upload/{}/files/tus", &self.id));
let res = self
.alias
.add_headers(http.post(endpoint))
.header("Sharry-File-Name", filename)
// .header("Sharry-File-Type", "application/octet-stream")
// .header("Sharry-File-Length", size)
// .header("Tus-Resumable", "1.0.0")
.header("Upload-Length", size)
.send_empty()?;
assert_eq!(res.status(), ureq::http::StatusCode::CREATED);
let location = res
.headers()
.get("Location")
.ok_or_else(|| ureq::Error::Other("Location header not found".into()))?
.to_str()
.map_err(|_| ureq::Error::Other("Location header invalid".into()))?;
debug!("location: {}", location);
// let start = 10;
// let count = 10;
// let mut f = File::open(path)?;
// f.seek(SeekFrom::Start(0));
// let mut buf = vec![0; count];
// f.read_exact(&mut buf)?;
Ok(())
}
pub fn notify(&self, http: &ureq::Agent) -> Result<(), ureq::Error> { pub fn notify(&self, http: &ureq::Agent) -> Result<(), ureq::Error> {
let endpoint = self let endpoint = self