[wip] impl Client for ureq::Agent
- use `sharry_file_create` in `AppState`
This commit is contained in:
parent
09af480379
commit
c9528a9ac1
2 changed files with 7 additions and 40 deletions
|
|
@ -47,12 +47,13 @@ impl FileState {
|
||||||
|
|
||||||
fn start_upload(
|
fn start_upload(
|
||||||
self,
|
self,
|
||||||
http: &ureq::Agent,
|
http: &impl Client,
|
||||||
alias: &Alias,
|
uri: &Uri,
|
||||||
share: &Share,
|
alias_id: &str,
|
||||||
) -> io::Result<FileUploading> {
|
share_id: &str,
|
||||||
|
) -> Result<FileUploading, ClientError> {
|
||||||
match self {
|
match self {
|
||||||
FileState::C(checked) => checked.start_upload(http, alias, share),
|
FileState::C(checked) => http.sharry_file_create(uri, alias_id, share_id, checked),
|
||||||
FileState::U(uploading) => Ok(uploading),
|
FileState::U(uploading) => Ok(uploading),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +131,7 @@ impl AppState {
|
||||||
) -> Result<Option<()>, UploadError> {
|
) -> Result<Option<()>, UploadError> {
|
||||||
let uploading = if let Some(state) = self.files.pop_front() {
|
let uploading = if let Some(state) = self.files.pop_front() {
|
||||||
state
|
state
|
||||||
.start_upload(http, &self.alias_id, &self.share_id)
|
.start_upload(http, &self.uri, &self.alias_id, &self.share_id)
|
||||||
.unwrap() // HACK unwrap
|
.unwrap() // HACK unwrap
|
||||||
} else {
|
} else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|
|
||||||
|
|
@ -29,40 +29,6 @@ impl FileChecked {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_upload(
|
|
||||||
self,
|
|
||||||
http: &ureq::Agent,
|
|
||||||
alias: &Alias,
|
|
||||||
share: &Share,
|
|
||||||
) -> io::Result<FileUploading> {
|
|
||||||
let size = self.get_size();
|
|
||||||
|
|
||||||
let res = {
|
|
||||||
let endpoint = alias.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
|
|
||||||
|
|
||||||
(http.post(endpoint))
|
|
||||||
.sharry_header(alias)
|
|
||||||
.header("Sharry-File-Name", self.get_name())
|
|
||||||
.header("Upload-Length", size)
|
|
||||||
.send_empty()
|
|
||||||
.map_err(ureq::Error::into_io)?
|
|
||||||
};
|
|
||||||
|
|
||||||
if res.status() != StatusCode::CREATED {
|
|
||||||
return Err(io::Error::other("unexpected response status"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let location = (res.headers().get("Location"))
|
|
||||||
.ok_or_else(|| io::Error::other("Location header not found"))?
|
|
||||||
.to_str()
|
|
||||||
.map_err(|_| io::Error::other("Location header invalid"))?
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
debug!("patch uri: {location}");
|
|
||||||
|
|
||||||
Ok(FileUploading::new(self.path, size, location))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> SharryFile<'t> for FileChecked {
|
impl<'t> SharryFile<'t> for FileChecked {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue