Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏎️ Speed up uploading by parallelizing uploads #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/common/file_helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use mktemp::Temp;
use std::fs::File;
use std::io;
use std::path::PathBuf;
use mktemp::Temp;

pub fn stdin_to_file() -> Result<Temp, io::Error> {
let tmp_file = Temp::new_file()?;
Expand All @@ -16,7 +16,7 @@ pub fn open_file(path: &Option<PathBuf>) -> Result<(File, PathBuf), io::Error> {
Some(path) => {
let file = File::open(path)?;
Ok((file, path.clone()))
},
}
None => {
let tmp_file = stdin_to_file()?;
let path = tmp_file.as_ref().to_path_buf();
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod account_archive;
pub mod delegate;
pub mod drive_file;
pub mod empty_file;
pub mod file_helper;
pub mod file_info;
pub mod file_tree;
pub mod file_tree_drive;
Expand All @@ -10,4 +11,3 @@ pub mod id_gen;
pub mod md5_writer;
pub mod permission;
pub mod table;
pub mod file_helper;
17 changes: 8 additions & 9 deletions src/files/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::common::delegate::BackoffConfig;
use crate::common::delegate::ChunkSize;
use crate::common::delegate::UploadDelegate;
use crate::common::delegate::UploadDelegateConfig;
use crate::common::file_helper;
use crate::common::file_info;
use crate::common::file_info::FileInfo;
use crate::common::file_helper;
use crate::common::hub_helper;
use crate::files;
use crate::files::info;
Expand Down Expand Up @@ -41,9 +41,12 @@ pub async fn update(config: Config) -> Result<(), Error> {
print_chunk_info: config.print_chunk_info,
};

let (file, file_path) = file_helper::open_file(&config.file_path)
.map_err(|err| Error::OpenFile(
config.file_path.unwrap_or_else(|| PathBuf::from("<stdin>")), err))?;
let (file, file_path) = file_helper::open_file(&config.file_path).map_err(|err| {
Error::OpenFile(
config.file_path.unwrap_or_else(|| PathBuf::from("<stdin>")),
err,
)
})?;

let drive_file = info::get_file(&hub, &config.file_id)
.await
Expand All @@ -61,11 +64,7 @@ pub async fn update(config: Config) -> Result<(), Error> {

let reader = std::io::BufReader::new(file);

println!(
"Updating {} with {}",
config.file_id,
file_path.display()
);
println!("Updating {} with {}", config.file_id, file_path.display());

let file = update_file(&hub, reader, &config.file_id, file_info, delegate_config)
.await
Expand Down
38 changes: 21 additions & 17 deletions src/files/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::common::delegate::BackoffConfig;
use crate::common::delegate::ChunkSize;
use crate::common::delegate::UploadDelegate;
use crate::common::delegate::UploadDelegateConfig;
use crate::common::file_helper;
use crate::common::file_info;
use crate::common::file_info::FileInfo;
use crate::common::file_tree;
use crate::common::file_tree::FileTree;
use crate::common::file_helper;
use crate::common::hub_helper;
use crate::common::id_gen::IdGen;
use crate::files;
Expand Down Expand Up @@ -57,15 +57,20 @@ pub async fn upload(config: Config) -> Result<(), Error> {
} else {
upload_regular(&hub, &config, delegate_config).await?;
}
},
}
None => {
let tmp_file = file_helper::stdin_to_file()
.map_err(|err| Error::OpenFile(PathBuf::from("<stdin>"), err))?;

upload_regular(&hub, &Config {
file_path: Some(tmp_file.as_ref().to_path_buf()),
..config
}, delegate_config).await?;
upload_regular(
&hub,
&Config {
file_path: Some(tmp_file.as_ref().to_path_buf()),
..config
},
delegate_config,
)
.await?;
}
};

Expand All @@ -78,8 +83,7 @@ pub async fn upload_regular(
delegate_config: UploadDelegateConfig,
) -> Result<(), Error> {
let file_path = config.file_path.as_ref().unwrap();
let file = fs::File::open(file_path)
.map_err(|err| Error::OpenFile(file_path.clone(), err))?;
let file = fs::File::open(file_path).map_err(|err| Error::OpenFile(file_path.clone(), err))?;

let file_info = FileInfo::from_file(
&file,
Expand Down Expand Up @@ -168,9 +172,10 @@ pub async fn upload_directory(
let folder_id = drive_folder.id.ok_or(Error::DriveFolderMissingId)?;
let parents = Some(vec![folder_id.clone()]);

for file in folder.files() {
futures::future::join_all(folder.files().into_iter().map(|file| {
let os_file = fs::File::open(&file.path)
.map_err(|err| Error::OpenFile(config.file_path.as_ref().unwrap().clone(), err))?;
.map_err(|err| Error::OpenFile(config.file_path.as_ref().unwrap().clone(), err))
.unwrap();

let file_info = file.info(parents.clone());

Expand All @@ -182,20 +187,19 @@ pub async fn upload_directory(
);
}

if config.print_only_id {
println!("{}: {}", file.relative_path().display(), file.drive_id);
}

upload_file(
hub,
os_file,
Some(file.drive_id.clone()),
file_info,
delegate_config.clone(),
)
.await
.map_err(Error::Upload)?;

if config.print_only_id {
println!("{}: {}", file.relative_path().display(), file.drive_id);
}
}
}))
.await;
}

if !config.print_only_id {
Expand Down