88 lines
2.3 KiB
Rust
88 lines
2.3 KiB
Rust
|
|
use log::debug;
|
||
|
|
use std::{ffi::OsStr, fs, path::Path};
|
||
|
|
use ureq::{Agent, http::StatusCode};
|
||
|
|
|
||
|
|
use super::{alias::Alias, api};
|
||
|
|
use api::NotifyShareResponse;
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub struct Share<'t> {
|
||
|
|
alias: &'t Alias,
|
||
|
|
id: String,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<'t> Share<'t> {
|
||
|
|
pub(super) fn new(alias: &'t Alias, id: String) -> Self {
|
||
|
|
Self { alias, id }
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn create_file(
|
||
|
|
&self,
|
||
|
|
http: &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()?;
|
||
|
|
|
||
|
|
if res.status() != StatusCode::CREATED {
|
||
|
|
return Err(ureq::Error::Other("unexpected response status".into()));
|
||
|
|
}
|
||
|
|
|
||
|
|
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> {
|
||
|
|
let endpoint = self
|
||
|
|
.alias
|
||
|
|
.uri
|
||
|
|
.get_endpoint(&format!("alias/mail/notify/{}", &self.id));
|
||
|
|
|
||
|
|
let res = self
|
||
|
|
.alias
|
||
|
|
.add_headers(http.post(endpoint))
|
||
|
|
.send_empty()?
|
||
|
|
.body_mut()
|
||
|
|
.read_json::<NotifyShareResponse>()?;
|
||
|
|
|
||
|
|
debug!("response: {:?}", res);
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
}
|