Implement archive feature use flag

This commit is contained in:
timvisee 2018-05-17 14:35:08 +02:00
parent b62f3fd343
commit 358bc6816a
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
7 changed files with 95 additions and 61 deletions

View file

@ -13,11 +13,14 @@ path = "src/main.rs"
name = "ffsend"
[features]
default = ["clipboard", "history"]
default = ["archive", "clipboard", "history"]
# Compile with file history support
history = []
# Compile with file archiving support
archive = []
# Compile without colored output support
no-color = ["colored/no-color"]
@ -39,7 +42,7 @@ prettytable-rs = "0.6"
rpassword = "2.0"
serde = "1.0"
serde_derive = "1.0"
tar = "0.4"
tar = { version = "0.4", option = true }
tempfile = "3"
toml = "0.4"
version-compare = "0.0.6"

View file

@ -22,6 +22,7 @@ use self::tempfile::{
NamedTempFile,
};
#[cfg(feature = "archive")]
use archive::archiver::Archiver;
use cmd::matcher::{Matcher, MainMatcher, UploadMatcher};
#[cfg(feature = "history")]
@ -131,52 +132,55 @@ impl<'a> Upload<'a> {
// A temporary archive file, only used when archiving
// The temporary file is stored here, to ensure it's lifetime exceeds the upload process
let mut tmp_archive: Option<NamedTempFile> = None;
let archive_extention = ".tar";
// Archive the file if specified
if matcher_upload.archive() {
println!("Archiving file...");
#[cfg(feature = "archive")]
{
// Archive the file if specified
if matcher_upload.archive() {
println!("Archiving file...");
let archive_extention = ".tar";
// Create a new temporary file to write the archive to
tmp_archive = Some(
TempBuilder::new()
.prefix(&format!(".{}-archive-", crate_name!()))
.suffix(archive_extention)
.tempfile()
.map_err(ArchiveError::TempFile)?
);
if let Some(tmp_archive) = &tmp_archive {
// Get the path, and the actual file
let archive_path = tmp_archive.path().to_path_buf();
let archive_file = tmp_archive.as_file()
.try_clone()
.map_err(ArchiveError::CloneHandle)?;
// Create a new temporary file to write the archive to
tmp_archive = Some(
TempBuilder::new()
.prefix(&format!(".{}-archive-", crate_name!()))
.suffix(archive_extention)
.tempfile()
.map_err(ArchiveError::TempFile)?
);
if let Some(tmp_archive) = &tmp_archive {
// Get the path, and the actual file
let archive_path = tmp_archive.path().to_path_buf();
let archive_file = tmp_archive.as_file()
.try_clone()
.map_err(ArchiveError::CloneHandle)?;
// Select the file name to use if not set
if file_name.is_none() {
// TODO: use canonical path here
file_name = Some(
path.file_name()
.ok_or(ArchiveError::FileName)?
.to_str()
.map(|s| s.to_owned())
.expect("failed to create string from file name")
);
// Select the file name to use if not set
if file_name.is_none() {
// TODO: use canonical path here
file_name = Some(
path.file_name()
.ok_or(ArchiveError::FileName)?
.to_str()
.map(|s| s.to_owned())
.expect("failed to create string from file name")
);
}
// Build an archiver and append the file
let mut archiver = Archiver::new(archive_file);
archiver.append_path(file_name.as_ref().unwrap(), &path)
.map_err(ArchiveError::AddFile)?;
// Finish the archival process, writes the archive file
archiver.finish().map_err(ArchiveError::Write)?;
// Append archive extention to name, set to upload archived file
if let Some(ref mut file_name) = file_name {
file_name.push_str(archive_extention);
}
path = archive_path;
}
// Build an archiver and append the file
let mut archiver = Archiver::new(archive_file);
archiver.append_path(file_name.as_ref().unwrap(), &path)
.map_err(ArchiveError::AddFile)?;
// Finish the archival process, writes the archive file
archiver.finish().map_err(ArchiveError::Write)?;
// Append archive extention to name, set to upload archived file
if let Some(ref mut file_name) = file_name {
file_name.push_str(archive_extention);
}
path = archive_path;
}
}
@ -218,12 +222,15 @@ impl<'a> Upload<'a> {
}
}
// Close the temporary zip file, to ensure it's removed
if let Some(tmp_archive) = tmp_archive.take() {
if let Err(err) = tmp_archive.close() {
print_error(
err.context("failed to clean up temporary archive file, ignoring").compat(),
);
#[cfg(feature = "archive")]
{
// Close the temporary zip file, to ensure it's removed
if let Some(tmp_archive) = tmp_archive.take() {
if let Err(err) = tmp_archive.close() {
print_error(
err.context("failed to clean up temporary archive file, ignoring").compat(),
);
}
}
}

View file

@ -73,6 +73,7 @@ impl<'a: 'b, 'b> UploadMatcher<'a> {
/// Check whether to archive the file to upload.
/// TODO: infer to use this flag if a directory is selected
#[cfg(feature = "archive")]
pub fn archive(&self) -> bool {
self.matches.is_present("archive")
}

View file

@ -32,16 +32,20 @@ impl CmdUpload {
.alias("f")
.value_name("NAME")
.help("Rename the file being uploaded"))
.arg(Arg::with_name("archive")
.long("archive")
.short("a")
.alias("arch")
.help("Package the file as an archive"))
.arg(Arg::with_name("open")
.long("open")
.short("o")
.help("Open the share link in your browser"));
// Optional archive support
#[cfg(feature = "archive")] {
cmd = cmd.arg(Arg::with_name("archive")
.long("archive")
.short("a")
.alias("arch")
.help("Package the file as an archive"))
}
// Optional clipboard support
#[cfg(feature = "clipboard")] {
cmd = cmd.arg(Arg::with_name("copy")

View file

@ -17,6 +17,7 @@ extern crate serde;
extern crate serde_derive;
mod action;
#[cfg(feature = "archive")]
mod archive;
mod cmd;
mod error;