creating empty share works
This commit is contained in:
parent
4ee17e2188
commit
08f27b3644
7 changed files with 1184 additions and 1 deletions
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
|
|
@ -20,6 +20,9 @@
|
|||
}
|
||||
},
|
||||
"args": [],
|
||||
"env": {
|
||||
"RUST_LOG": "shrupl=info",
|
||||
},
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
// {
|
||||
|
|
|
|||
1014
Cargo.lock
generated
1014
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,3 +4,7 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.11.8"
|
||||
log = "0.4.27"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
ureq = { version = "3.0.11", features = ["json"] }
|
||||
|
|
|
|||
37
doc/notes.md
Normal file
37
doc/notes.md
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
outline of sharry uploading
|
||||
|
||||
1. POST to "new" route
|
||||
- uri: https://sharry.yavook.de/api/v2/alias/upload/new
|
||||
- hdr: `Sharry-Alias: $alias_id`
|
||||
- res.status == 200
|
||||
- res_json.success == true
|
||||
- res_json.message == "Share created."
|
||||
- $share_id := res_json.id
|
||||
|
||||
1. POST to "tus" route
|
||||
- uri: https://sharry.yavook.de/api/v2/alias/upload/$share_id/files/tus
|
||||
- hdr: `Sharry-Alias: $alias_id`
|
||||
- hdr: `Sharry-File-Length: $file_bytes`
|
||||
- hdr: `Sharry-File-Name: $file_name`
|
||||
- hdr: `Sharry-File-Type: $file_mimetype`
|
||||
- hdr: `Tus-Resumable: 1.0.0`
|
||||
- hdr: `Upload-Length: $file_bytes`
|
||||
- res.status == 201
|
||||
- $patch_uri := res_hdr.Location
|
||||
|
||||
1. (multi) PATCH to "$patch_uri"
|
||||
- hdr: `Sharry-Alias`, `Sharry-File-Length`, `Sharry-File-Name`, `Sharry-File-Type`, `Tus-Resumable`
|
||||
- hdr: `Upload-Offset: 10485760 * (n-1)` for nth chunk
|
||||
- body: up to 10 MiB of binary data
|
||||
- res.status == 204
|
||||
- better use res_hdr.Upload-Offset
|
||||
|
||||
1. (opt) POST to "notify" route
|
||||
- uri: https://sharry.yavook.de/api/v2/alias/mail/notify/$share_id
|
||||
- hdr: `Sharry-Alias`
|
||||
- res.status == 200
|
||||
- res_json.success, res_json.message
|
||||
|
||||
hints
|
||||
|
||||
- https://stackoverflow.com/questions/59586787/rust-how-to-do-http-put-of-large-files
|
||||
24
src/main.rs
24
src/main.rs
|
|
@ -1,3 +1,25 @@
|
|||
mod sharry;
|
||||
|
||||
use log::info;
|
||||
use sharry::{
|
||||
Alias,
|
||||
api::{NewShareRequest, SharryAPI},
|
||||
};
|
||||
use std::time::Duration;
|
||||
use ureq::Agent;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
env_logger::init();
|
||||
|
||||
let agent: Agent = Agent::config_builder()
|
||||
.timeout_global(Some(Duration::from_secs(5)))
|
||||
.build()
|
||||
.into();
|
||||
|
||||
let api = SharryAPI::new(None, "sharry.yavook.de");
|
||||
|
||||
let alias = Alias::new(&api, "G7RYoWME1W7-pcgipemJcr8-39FcMd92gBu-RgufeHc51z6");
|
||||
|
||||
let share = alias.create_share(&agent, &NewShareRequest::new(Some("foo"), "", 10));
|
||||
info!("share: {:?}", share)
|
||||
}
|
||||
|
|
|
|||
53
src/sharry/api.rs
Normal file
53
src/sharry/api.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SharryAPI {
|
||||
protocol: String,
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl SharryAPI {
|
||||
pub fn new(protocol: Option<&str>, base_url: &str) -> Self {
|
||||
Self {
|
||||
protocol: protocol.unwrap_or("https").into(),
|
||||
base_url: base_url.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_uri(&self, endpoint: &str) -> String {
|
||||
let uri = format!("{}://{}/api/v2/{}", self.protocol, self.base_url, endpoint);
|
||||
debug!("uri: {:?}", uri);
|
||||
|
||||
uri
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct NewShareRequest<'t> {
|
||||
name: Option<&'t str>,
|
||||
validity: u32,
|
||||
description: &'t str,
|
||||
maxViews: u32,
|
||||
password: Option<&'t str>,
|
||||
}
|
||||
|
||||
impl<'t> NewShareRequest<'t> {
|
||||
pub fn new(name: Option<&'t str>, description: &'t str, max_views: u32) -> Self {
|
||||
Self {
|
||||
name: name,
|
||||
validity: 0,
|
||||
description: description,
|
||||
maxViews: max_views,
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct NewShareResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
pub id: String,
|
||||
}
|
||||
50
src/sharry/mod.rs
Normal file
50
src/sharry/mod.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
pub mod api;
|
||||
|
||||
use api::{NewShareRequest, NewShareResponse, SharryAPI};
|
||||
use log::debug;
|
||||
use ureq;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Alias<'t> {
|
||||
api: &'t SharryAPI,
|
||||
id: String,
|
||||
}
|
||||
|
||||
impl<'t> Alias<'t> {
|
||||
pub fn new(api: &'t SharryAPI, id: &str) -> Self {
|
||||
Self {
|
||||
api: api,
|
||||
id: id.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_share(
|
||||
&self,
|
||||
http: &ureq::Agent,
|
||||
data: &NewShareRequest,
|
||||
) -> Result<Share, ureq::Error> {
|
||||
let foo = http
|
||||
.post(self.api.get_uri("alias/upload/new"))
|
||||
.header("Sharry-Alias", &self.id)
|
||||
.send_json(data)?
|
||||
.body_mut()
|
||||
.read_json::<NewShareResponse>()?;
|
||||
|
||||
debug!("foo: {:?}", foo);
|
||||
|
||||
Ok(Share {
|
||||
alias: self,
|
||||
id: foo.id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Share<'t> {
|
||||
alias: &'t Alias<'t>,
|
||||
id: String,
|
||||
}
|
||||
|
||||
impl<'t> Share<'t> {
|
||||
pub fn add_file(&self) {}
|
||||
}
|
||||
Loading…
Reference in a new issue