mirror of
https://github.com/chatmail/core.git
synced 2026-05-04 05:46:29 +03:00
Move file upload into SMTP send job.
- This adds params for the upload URL and local file path, so that the actual upload can happen in the send job. - This also moves the URL generation to the client side so that we can generate a valid URL before the upload (because the MIME rendering of the mail message happens earlier and we want to include the URL there)
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use crate::context::Context;
|
||||
use crate::error::{bail, Result};
|
||||
use async_std::path::PathBuf;
|
||||
use rand::Rng;
|
||||
|
||||
/// Upload file to a HTTP upload endpoint.
|
||||
pub async fn upload_file(_context: &Context, endpoint: String, file: PathBuf) -> Result<String> {
|
||||
pub async fn upload_file(_context: &Context, url: String, filepath: PathBuf) -> Result<String> {
|
||||
// TODO: Use tokens for upload, encrypt file with PGP.
|
||||
let response = surf::post(endpoint).body_file(file)?.await;
|
||||
let response = surf::put(url).body_file(filepath)?.await;
|
||||
if let Err(err) = response {
|
||||
bail!("Upload failed: {}", err);
|
||||
}
|
||||
@@ -15,3 +16,17 @@ pub async fn upload_file(_context: &Context, endpoint: String, file: PathBuf) ->
|
||||
Err(err) => bail!("Invalid response from upload: {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a random URL based on the provided endpoint.
|
||||
pub fn generate_upload_url(_context: &Context, endpoint: String) -> String {
|
||||
const CROCKFORD_ALPHABET: &[u8] = b"0123456789abcdefghjkmnpqrstvwxyz";
|
||||
const FILENAME_LEN: usize = 27;
|
||||
let mut rng = rand::thread_rng();
|
||||
let filename: String = (0..FILENAME_LEN)
|
||||
.map(|_| {
|
||||
let idx = rng.gen_range(0, CROCKFORD_ALPHABET.len());
|
||||
CROCKFORD_ALPHABET[idx] as char
|
||||
})
|
||||
.collect();
|
||||
format!("{}{}", endpoint, filename)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user