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 support for determining folder size #126

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
24 changes: 20 additions & 4 deletions src/files/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::fmt::Display;
use std::fmt::Formatter;
use std::io;
use std::str::FromStr;
use crate::common::file_tree_drive::FileTreeDrive;

const MAX_PAGE_SIZE: usize = 1000;

Expand All @@ -22,6 +23,7 @@ pub struct Config {
pub skip_header: bool,
pub truncate_name: bool,
pub field_separator: String,
pub folder_size: bool,
}

pub async fn list(config: Config) -> Result<(), Error> {
Expand All @@ -34,21 +36,33 @@ pub async fn list(config: Config) -> Result<(), Error> {
max_files: config.max_files,
},
)
.await?;
.await?;

let mut values: Vec<[String; 5]> = vec![];

for file in files {
let file_type = simplified_file_type(&file);
let file_name = format_file_name(&config, &file);

let file_size = if config.folder_size && drive_file::is_directory(&file) {
// If the file is a directory, calculate the total size of the directory
let tree = FileTreeDrive::from_file(&hub, &file).await.map_err(|_| Error::FolderSize)?;
let tree_info = tree.info();

if tree_info.total_file_size > i64::MAX as u128 {
Some(i64::MAX)
} else {
Some(tree_info.total_file_size as i64)
}
} else {
file.size
};

values.push([
file.id.unwrap_or_default(),
file_name,
file_type,
file.size
.map(|bytes| files::info::format_bytes(bytes, &DisplayConfig::default()))
.unwrap_or_default(),
file_size.map(|bytes| files::info::format_bytes(bytes, &DisplayConfig::default())).unwrap_or_default(),
file.created_time
.map(files::info::format_date_time)
.unwrap_or_default(),
Expand Down Expand Up @@ -214,6 +228,7 @@ impl fmt::Display for ListSortOrder {
pub enum Error {
Hub(hub_helper::Error),
ListFiles(google_drive3::Error),
FolderSize,
}

impl error::Error for Error {}
Expand All @@ -223,6 +238,7 @@ impl Display for Error {
match self {
Error::Hub(e) => write!(f, "{}", e),
Error::ListFiles(e) => write!(f, "Failed to list files: {}", e),
Error::FolderSize => write!(f, "Failed to determine folder size: "),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ enum FileCommand {
/// Field separator
#[arg(long, default_value_t = String::from("\t"))]
field_separator: String,

#[arg(long)]
folder_size: bool
},

/// Download file
Expand Down Expand Up @@ -471,6 +474,7 @@ async fn main() {
skip_header,
full_name,
field_separator,
folder_size
} => {
let parent_query =
parent.map(|folder_id| ListQuery::FilesInFolder { folder_id });
Expand All @@ -486,6 +490,7 @@ async fn main() {
skip_header,
truncate_name: !full_name,
field_separator,
folder_size
})
.await
.unwrap_or_else(handle_error)
Expand Down