Skip to content

Commit

Permalink
Merge pull request #41 from nick-cd/stdin-upload
Browse files Browse the repository at this point in the history
Upload file from stdin
  • Loading branch information
prasmussen authored Jul 16, 2023
2 parents dc9a81a + 3999bf0 commit 5735c25
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 23 deletions.
34 changes: 32 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hyper = { version = "0.14.23", features = ["stream"] }
md5 = "0.7.0"
mime = "0.3.16"
mime_guess = "2.0.4"
mktemp = "0.5.0"
rustc_version_runtime = "0.2.1"
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.89"
Expand Down
27 changes: 27 additions & 0 deletions src/common/file_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::io;
use std::fs::File;
use std::path::PathBuf;
use mktemp::Temp;

pub fn stdin_to_file() -> Result<Temp, io::Error> {
let tmp_file = Temp::new_file()?;
let path = tmp_file.as_ref().to_path_buf();
let mut file = File::create(&path)?;
io::copy(&mut io::stdin(), &mut file)?;
Ok(tmp_file)
}

pub fn open_file(path: &Option<PathBuf>) -> Result<(File, PathBuf), io::Error> {
match path {
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();
let file = File::open(&path)?;
Ok((file, path))
}
}
}
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod id_gen;
pub mod md5_writer;
pub mod permission;
pub mod table;
pub mod file_helper;
13 changes: 7 additions & 6 deletions src/files/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::common::delegate::UploadDelegate;
use crate::common::delegate::UploadDelegateConfig;
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 All @@ -13,14 +14,13 @@ use mime::Mime;
use std::error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::time::Duration;

pub struct Config {
pub file_id: String,
pub file_path: PathBuf,
pub file_path: Option<PathBuf>,
pub mime_type: Option<Mime>,
pub chunk_size: ChunkSize,
pub print_chunk_errors: bool,
Expand All @@ -41,8 +41,9 @@ pub async fn update(config: Config) -> Result<(), Error> {
print_chunk_info: config.print_chunk_info,
};

let file = fs::File::open(&config.file_path)
.map_err(|err| Error::OpenFile(config.file_path.clone(), 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 @@ -51,7 +52,7 @@ pub async fn update(config: Config) -> Result<(), Error> {
let file_info = FileInfo::from_file(
&file,
&file_info::Config {
file_path: config.file_path.clone(),
file_path: file_path.clone(),
mime_type: config.mime_type,
parents: drive_file.parents.clone(),
},
Expand All @@ -63,7 +64,7 @@ pub async fn update(config: Config) -> Result<(), Error> {
println!(
"Updating {} with {}",
config.file_id,
config.file_path.display()
file_path.display()
);

let file = update_file(&hub, reader, &config.file_id, file_info, delegate_config)
Expand Down
41 changes: 28 additions & 13 deletions src/files/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 All @@ -23,7 +24,7 @@ use std::path::PathBuf;
use std::time::Duration;

pub struct Config {
pub file_path: PathBuf,
pub file_path: Option<PathBuf>,
pub mime_type: Option<Mime>,
pub parents: Option<Vec<String>>,
pub chunk_size: ChunkSize,
Expand All @@ -47,13 +48,26 @@ pub async fn upload(config: Config) -> Result<(), Error> {
print_chunk_info: config.print_chunk_info,
};

err_if_directory(&config.file_path, &config)?;
match &config.file_path {
Some(path) => {
err_if_directory(&path, &config)?;

if config.file_path.is_dir() {
upload_directory(&hub, &config, delegate_config).await?;
} else {
upload_regular(&hub, &config, delegate_config).await?;
}
if path.is_dir() {
upload_directory(&hub, &config, delegate_config).await?;
} 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?;
}
};

Ok(())
}
Expand All @@ -63,13 +77,14 @@ pub async fn upload_regular(
config: &Config,
delegate_config: UploadDelegateConfig,
) -> Result<(), Error> {
let file = fs::File::open(&config.file_path)
.map_err(|err| Error::OpenFile(config.file_path.clone(), err))?;
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_info = FileInfo::from_file(
&file,
&file_info::Config {
file_path: config.file_path.clone(),
file_path: file_path.clone(),
mime_type: config.mime_type.clone(),
parents: config.parents.clone(),
},
Expand All @@ -79,7 +94,7 @@ pub async fn upload_regular(
let reader = std::io::BufReader::new(file);

if !config.print_only_id {
println!("Uploading {}", config.file_path.display());
println!("Uploading {}", file_path.display());
}

let file = upload_file(&hub, reader, None, file_info, delegate_config)
Expand All @@ -103,7 +118,7 @@ pub async fn upload_directory(
delegate_config: UploadDelegateConfig,
) -> Result<(), Error> {
let mut ids = IdGen::new(hub, &delegate_config);
let tree = FileTree::from_path(&config.file_path, &mut ids)
let tree = FileTree::from_path(config.file_path.as_ref().unwrap(), &mut ids)
.await
.map_err(Error::CreateFileTree)?;

Expand Down Expand Up @@ -155,7 +170,7 @@ pub async fn upload_directory(

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ enum FileCommand {
/// Upload file
Upload {
/// Path of file to upload
file_path: PathBuf,
file_path: Option<PathBuf>,

/// Force mime type [default: auto-detect]
#[arg(long, value_name = "MIME_TYPE")]
Expand Down Expand Up @@ -220,7 +220,7 @@ enum FileCommand {
file_id: String,

/// Path of file to upload
file_path: PathBuf,
file_path: Option<PathBuf>,

/// Force mime type [default: auto-detect]
#[arg(long, value_name = "MIME_TYPE")]
Expand Down

0 comments on commit 5735c25

Please sign in to comment.