Skip to content

Commit 68f4fbf

Browse files
metamejoepio
authored andcommitted
use tokio streams in upload
1 parent 4ef7d8f commit 68f4fbf

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ features = ["derive"]
104104
version = "1"
105105

106106
[dependencies.tokio]
107-
features = ["time"]
107+
features = ["time", "fs"]
108108
version = "1"
109109

110110
[dependencies.tracing-subscriber]

server/src/files.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fmt, path::PathBuf, time::Duration};
22

33
use opendal::{services::S3, Operator};
4+
use tokio::fs::File;
45

56
use crate::{appstate::AppState, config::Config, errors::AtomicServerResult};
67

@@ -39,7 +40,6 @@ impl FileStore {
3940
pub fn encoded(&self) -> String {
4041
urlencoding::encode(self.prefix()).into_owned()
4142
}
42-
4343
}
4444

4545
impl fmt::Display for FileStore {
@@ -78,9 +78,13 @@ pub async fn s3_upload_object(
7878

7979
let op: Operator = Operator::new(builder)?.finish();
8080

81-
let buffer = std::fs::read(file_path)?;
82-
op.write(&format!("{}/{}", path, &file_id), buffer).await?;
81+
let mut tmp_file = File::open(file_path).await?;
82+
let length = tmp_file.metadata().await?.len();
8383

84+
let s3_path = format!("{}/{}", path, &file_id);
85+
let mut w = op.writer_with(&s3_path).content_length(length).await?;
86+
tokio::io::copy(&mut tmp_file, &mut w).await?;
87+
w.close().await?;
8488
Ok(())
8589
}
8690

@@ -123,7 +127,11 @@ pub async fn get_s3_signed_url(
123127

124128
let op: Operator = Operator::new(builder)?.finish();
125129

126-
let uri = op.presign_read(&format!("{}/{}", &path, file_id), duration).await?.uri().to_string();
130+
let uri = op
131+
.presign_read(&format!("{}/{}", &path, file_id), duration)
132+
.await?
133+
.uri()
134+
.to_string();
127135

128136
Ok(uri)
129137
}

server/src/handlers/download.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ use actix_files::NamedFile;
44
use actix_web::{web, HttpRequest, HttpResponse, Responder};
55
use atomic_lib::{urls, Storelike};
66

7-
use crate::{appstate::AppState, errors::AtomicServerResult, files::{self, FileStore}, helpers::get_client_agent};
7+
use crate::{
8+
appstate::AppState,
9+
errors::AtomicServerResult,
10+
files::{self, FileStore},
11+
helpers::get_client_agent,
12+
};
813

914
/// Downloads the File of the Resource that matches the same URL minus the `/download` path.
1015
#[tracing::instrument(skip(appstate, req))]
@@ -60,8 +65,7 @@ async fn signed_url_redirect_handler(
6065
req: &HttpRequest,
6166
appstate: &AppState,
6267
) -> AtomicServerResult<HttpResponse> {
63-
let signed_url = files::get_s3_signed_url(appstate, Duration::from_secs(3600), file_id)
64-
.await?;
68+
let signed_url = files::get_s3_signed_url(appstate, Duration::from_secs(3600), file_id).await?;
6569
Ok(web::Redirect::to(signed_url)
6670
.respond_to(req)
6771
.map_into_boxed_body())

server/src/handlers/upload.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ use atomic_lib::{
1313
use futures::{StreamExt, TryStreamExt};
1414
use serde::Deserialize;
1515

16-
use crate::{appstate::AppState, errors::AtomicServerResult, files::{self, FileStore}, helpers::get_client_agent};
16+
use crate::{
17+
appstate::AppState,
18+
errors::AtomicServerResult,
19+
files::{self, FileStore},
20+
helpers::get_client_agent,
21+
};
1722

1823
#[derive(Deserialize, Debug)]
1924
pub struct UploadQuery {

0 commit comments

Comments
 (0)