Skip to content
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
8 changes: 8 additions & 0 deletions src/common/drive_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const EXTENSION_PPTX: &str = "pptx";
pub const EXTENSION_ODP: &str = "odp";
pub const EXTENSION_EPUB: &str = "epub";
pub const EXTENSION_TXT: &str = "txt";
pub const EXTENSION_MD: &str = "md";

pub const MIME_TYPE_DOC: &str = "application/msword";
pub const MIME_TYPE_DOCX: &str =
Expand All @@ -52,6 +53,7 @@ pub const MIME_TYPE_PPTX: &str =
pub const MIME_TYPE_ODP: &str = "application/vnd.oasis.opendocument.presentation";
pub const MIME_TYPE_EPUB: &str = "application/epub+zip";
pub const MIME_TYPE_TXT: &str = "text/plain";
pub const MIME_TYPE_MD: &str = "text/markdown";

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DocType {
Expand All @@ -72,6 +74,7 @@ impl DocType {
(FileExtension::Rtf, DocType::Document),
(FileExtension::Pdf, DocType::Document),
(FileExtension::Html, DocType::Document),
(FileExtension::Md, DocType::Document),
(FileExtension::Xls, DocType::Spreadsheet),
(FileExtension::Xlsx, DocType::Spreadsheet),
(FileExtension::Csv, DocType::Spreadsheet),
Expand Down Expand Up @@ -134,6 +137,7 @@ impl DocType {
FileExtension::Rtf,
FileExtension::Txt,
FileExtension::Html,
FileExtension::Md,
],

DocType::Spreadsheet => vec![
Expand Down Expand Up @@ -194,6 +198,7 @@ pub enum FileExtension {
Odp,
Epub,
Txt,
Md,
}

impl fmt::Display for FileExtension {
Expand All @@ -219,6 +224,7 @@ impl fmt::Display for FileExtension {
FileExtension::Odp => write!(f, "{}", EXTENSION_ODP),
FileExtension::Epub => write!(f, "{}", EXTENSION_EPUB),
FileExtension::Txt => write!(f, "{}", EXTENSION_TXT),
FileExtension::Md => write!(f, "{}", EXTENSION_MD),
}
}
}
Expand Down Expand Up @@ -248,6 +254,7 @@ impl FileExtension {
EXTENSION_ODP => Some(FileExtension::Odp),
EXTENSION_EPUB => Some(FileExtension::Epub),
EXTENSION_TXT => Some(FileExtension::Txt),
EXTENSION_MD => Some(FileExtension::Md),
_ => None,
}
}
Expand All @@ -274,6 +281,7 @@ impl FileExtension {
FileExtension::Odp => MIME_TYPE_ODP.parse().ok(),
FileExtension::Epub => MIME_TYPE_EPUB.parse().ok(),
FileExtension::Txt => MIME_TYPE_TXT.parse().ok(),
FileExtension::Md => MIME_TYPE_MD.parse().ok(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::PathBuf;
pub struct FileInfo {
pub name: String,
pub mime_type: mime::Mime,
pub source_mime_type: Option<mime::Mime>,
pub parents: Option<Vec<String>>,
pub size: u64,
}
Expand Down Expand Up @@ -36,6 +37,7 @@ impl FileInfo {
Ok(FileInfo {
name: file_name,
mime_type,
source_mime_type: None,
parents: config.parents.clone(),
size: file_size,
})
Expand Down
1 change: 1 addition & 0 deletions src/common/file_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl File {
name: self.name.clone(),
size: self.size,
mime_type: self.mime_type.clone(),
source_mime_type: None,
parents,
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/files/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ pub async fn import(config: Config) -> Result<(), Error> {

let doc_type =
drive_file::DocType::from_file_path(&config.file_path).ok_or(Error::UnsupportedFileType)?;
let mime_type = doc_type.mime().ok_or(Error::GetMime(doc_type.clone()))?;
let target_mime_type = doc_type.mime().ok_or(Error::GetMime(doc_type.clone()))?;

// Get source file's MIME type based on extension
let source_mime_type = drive_file::FileExtension::from_path(&config.file_path)
.and_then(|ext| ext.get_export_mime());

let file = fs::File::open(&config.file_path)
.map_err(|err| Error::OpenFile(config.file_path.clone(), err))?;

let file_info = FileInfo::from_file(
let mut file_info = FileInfo::from_file(
&file,
&file_info::Config {
file_path: config.file_path.clone(),
mime_type: Some(mime_type),
mime_type: Some(target_mime_type),
parents: config.parents.clone(),
},
)
.map_err(Error::FileInfo)?;

// Set the source MIME type for proper import conversion
file_info.source_mime_type = source_mime_type;

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

if !config.print_only_id {
Expand Down
7 changes: 5 additions & 2 deletions src/files/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ where
..google_drive3::api::File::default()
};

// For import operations, use source_mime_type for the upload content type
let upload_mime_type = file_info.source_mime_type.unwrap_or(file_info.mime_type);

let chunk_size_bytes = delegate_config.chunk_size.in_bytes();
let mut delegate = UploadDelegate::new(delegate_config);

Expand All @@ -240,9 +243,9 @@ where
.supports_all_drives(true);

let (_, file) = if file_info.size > chunk_size_bytes {
req.upload_resumable(src_file, file_info.mime_type).await?
req.upload_resumable(src_file, upload_mime_type).await?
} else {
req.upload(src_file, file_info.mime_type).await?
req.upload(src_file, upload_mime_type).await?
};

Ok(file)
Expand Down