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

Add ability to trash files for when deleting is not an option #67

Open
wants to merge 7 commits 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;
4 changes: 4 additions & 0 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod list;
pub mod mkdir;
pub mod mv;
pub mod rename;
pub mod trash;
pub mod untrash;
pub mod update;
pub mod upload;

Expand All @@ -23,5 +25,7 @@ pub use list::list;
pub use mkdir::mkdir;
pub use mv::mv;
pub use rename::rename;
pub use trash::trash;
pub use untrash::untrash;
pub use update::update;
pub use upload::upload;
14 changes: 13 additions & 1 deletion src/files/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn get_file(
let (_, file) = hub
.files()
.get(file_id)
.param("fields", "id,name,size,createdTime,modifiedTime,md5Checksum,mimeType,parents,shared,description,webContentLink,webViewLink,shortcutDetails(targetId,targetMimeType)")
.param("fields", "id,name,size,createdTime,modifiedTime,md5Checksum,mimeType,parents,shared,description,webContentLink,webViewLink,shortcutDetails(targetId,targetMimeType),trashed,trashedTime")
.supports_all_drives(true)
.add_scope(google_drive3::api::Scope::Full)
.doit()
Expand Down Expand Up @@ -108,6 +108,18 @@ pub fn prepare_fields(file: &google_drive3::api::File, config: &DisplayConfig) -
name: String::from("ViewUrl"),
value: file.web_view_link.clone(),
},
Field {
name: String::from("Trashed"),
value: if file.trashed.is_some() && file.trashed.unwrap() {Some(format_bool(file.trashed.unwrap()))} else {None},
},
Field {
name: String::from("TrashedTime"),
value: file.trashed_time.map(format_date_time),
},
Field {
name: String::from("TrashingUser"),
value: file.trashing_user.clone().map(|user| user.display_name.unwrap_or_default()),
},
]
}

Expand Down
7 changes: 7 additions & 0 deletions src/files/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Config {
pub skip_header: bool,
pub truncate_name: bool,
pub field_separator: String,
pub skip_trashed: bool,
}

pub async fn list(config: Config) -> Result<(), Error> {
Expand All @@ -42,6 +43,12 @@ pub async fn list(config: Config) -> Result<(), Error> {
let file_type = simplified_file_type(&file);
let file_name = format_file_name(&config, &file);

if config.skip_trashed{
if file.trashed.is_some_and(|trashed| trashed == true){
continue;
}
}

values.push([
file.id.unwrap_or_default(),
file_name,
Expand Down
125 changes: 125 additions & 0 deletions src/files/trash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use crate::common::{drive_file, hub_helper};
use crate::files::info;
use crate::hub::Hub;
use std::error;
use std::fmt::Display;
use std::fmt::Formatter;

pub struct Config {
pub file_id: String,
pub trash_directories: bool,
}

pub async fn trash(config: Config) -> Result<(), Error> {
let hub = hub_helper::get_hub().await.map_err(Error::Hub)?;

let exists = info::get_file(&hub, &config.file_id)
.await
.map_err(Error::GetFile)?;

err_if_directory(&exists, &config)?;

if exists.trashed.is_some_and(|trashed| trashed == true) {
println!("File is already trashed, exiting");
return Ok(());
}

println!("Trashing {}", config.file_id);

trash_file(&hub, &config.file_id)
.await
.map_err(Error::Update)?;

println!("File successfully updated");

Ok(())
}

pub async fn trash_file(hub: &Hub, file_id: &str) -> Result<(), google_drive3::Error> {
let dst_file = google_drive3::api::File {
trashed: Some(true),
..google_drive3::api::File::default()
};

let req = hub
.files()
.update(dst_file, &file_id)
.param("fields", "id,name,size,createdTime,modifiedTime,md5Checksum,mimeType,parents,shared,description,webContentLink,webViewLink")
.add_scope(google_drive3::api::Scope::Full)
.supports_all_drives(true);

req.doit_without_upload().await?;

Ok(())
}

#[derive(Debug)]
pub enum Error {
Hub(hub_helper::Error),
GetFile(google_drive3::Error),
Update(google_drive3::Error),
IsDirectory(String),
}

impl error::Error for Error {}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Hub(err) => write!(f, "{}", err),
Error::GetFile(err) => write!(f, "Failed to get file: {}", err),
Error::Update(err) => write!(f, "Failed to trash file: {}", err),
Error::IsDirectory(name) => write!(
f,
"'{}' is a directory, use --recursive to trash directories",
name
),
}
}
}

#[derive(Debug, Clone)]
pub struct PatchFile {
id: String,
file: google_drive3::api::File,
}

impl PatchFile {
pub fn new(id: String) -> Self {
Self {
id,
file: google_drive3::api::File::default(),
}
}

pub fn with_name(&self, name: &str) -> Self {
Self {
file: google_drive3::api::File {
name: Some(name.to_string()),
..self.file.clone()
},
..self.clone()
}
}

pub fn id(&self) -> String {
self.id.clone()
}

pub fn file(&self) -> google_drive3::api::File {
self.file.clone()
}
}

fn err_if_directory(file: &google_drive3::api::File, config: &Config) -> Result<(), Error> {
if drive_file::is_directory(file) && !config.trash_directories {
let name = file
.name
.as_ref()
.map(|s| s.to_string())
.unwrap_or_default();
Err(Error::IsDirectory(name))
} else {
Ok(())
}
}
125 changes: 125 additions & 0 deletions src/files/untrash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use crate::common::{drive_file, hub_helper};
use crate::files::info;
use crate::hub::Hub;
use std::error;
use std::fmt::Display;
use std::fmt::Formatter;

pub struct Config {
pub file_id: String,
pub untrash_directories: bool,
}

pub async fn untrash(config: Config) -> Result<(), Error> {
let hub = hub_helper::get_hub().await.map_err(Error::Hub)?;

let exists = info::get_file(&hub, &config.file_id)
.await
.map_err(Error::GetFile)?;

err_if_directory(&exists, &config)?;

if exists.trashed.is_some_and(|trashed| trashed == false) {
println!("File is not trashed, exiting");
return Ok(());
}

println!("Untrashing {}", config.file_id);

untrash_file(&hub, &config.file_id)
.await
.map_err(Error::Update)?;

println!("File successfully updated");

Ok(())
}

pub async fn untrash_file(hub: &Hub, file_id: &str) -> Result<(), google_drive3::Error> {
let dst_file = google_drive3::api::File {
trashed: Some(false),
..google_drive3::api::File::default()
};

let req = hub
.files()
.update(dst_file, &file_id)
.param("fields", "id,name,size,createdTime,modifiedTime,md5Checksum,mimeType,parents,shared,description,webContentLink,webViewLink")
.add_scope(google_drive3::api::Scope::Full)
.supports_all_drives(true);

req.doit_without_upload().await?;

Ok(())
}

#[derive(Debug)]
pub enum Error {
Hub(hub_helper::Error),
GetFile(google_drive3::Error),
Update(google_drive3::Error),
IsDirectory(String),
}

impl error::Error for Error {}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Hub(err) => write!(f, "{}", err),
Error::GetFile(err) => write!(f, "Failed to get file: {}", err),
Error::Update(err) => write!(f, "Failed to update file: {}", err),
Error::IsDirectory(name) => write!(
f,
"'{}' is a directory, use --recursive to trash directories",
name
),
}
}
}

#[derive(Debug, Clone)]
pub struct PatchFile {
id: String,
file: google_drive3::api::File,
}

impl PatchFile {
pub fn new(id: String) -> Self {
Self {
id,
file: google_drive3::api::File::default(),
}
}

pub fn with_name(&self, name: &str) -> Self {
Self {
file: google_drive3::api::File {
name: Some(name.to_string()),
..self.file.clone()
},
..self.clone()
}
}

pub fn id(&self) -> String {
self.id.clone()
}

pub fn file(&self) -> google_drive3::api::File {
self.file.clone()
}
}

fn err_if_directory(file: &google_drive3::api::File, config: &Config) -> Result<(), Error> {
if drive_file::is_directory(file) && !config.untrash_directories {
let name = file
.name
.as_ref()
.map(|s| s.to_string())
.unwrap_or_default();
Err(Error::IsDirectory(name))
} else {
Ok(())
}
}
Loading