rename non-normalized filenames when downloading

This commit is contained in:
Damien Elmes 2020-02-05 16:16:44 +10:00
parent 0fb70dab0f
commit 23f5c7cb9b
2 changed files with 51 additions and 26 deletions

View file

@ -267,24 +267,26 @@ pub(super) fn add_file_from_ankiweb(
fname: &str, fname: &str,
data: &[u8], data: &[u8],
) -> Result<AddedFile> { ) -> Result<AddedFile> {
let sha1 = sha1_of_data(data);
let normalized = normalize_filename(fname); let normalized = normalize_filename(fname);
let path = media_folder.join(normalized.as_ref()); // if the filename is already valid, we can write the file directly
fs::write(&path, data)?; let (renamed_from, path) = if let Cow::Borrowed(_) = normalized {
let path = media_folder.join(normalized.as_ref());
let sha1 = sha1_of_data(data); fs::write(&path, data)?;
(None, path)
} else {
debug!("non-normalized filename received {}", fname);
// ankiweb sent us a non-normalized filename, so we'll rename it
let new_name = add_data_to_folder_uniquely(media_folder, fname, data, sha1)?;
(
Some(new_name.to_string()),
media_folder.join(new_name.as_ref()),
)
};
let mtime = mtime_as_i64(path)?; let mtime = mtime_as_i64(path)?;
// fixme: could we use the info sent from the server for the hash instead
// of hashing it here and returning hash?
let renamed_from = if let Cow::Borrowed(_) = normalized {
None
} else {
Some(fname.to_string())
};
Ok(AddedFile { Ok(AddedFile {
fname: normalized.to_string(), fname: normalized.to_string(),
sha1, sha1,

View file

@ -24,7 +24,7 @@ static SYNC_MAX_FILES: usize = 25;
static SYNC_MAX_BYTES: usize = (2.5 * 1024.0 * 1024.0) as usize; static SYNC_MAX_BYTES: usize = (2.5 * 1024.0 * 1024.0) as usize;
static SYNC_SINGLE_FILE_MAX_BYTES: usize = 100 * 1024 * 1024; static SYNC_SINGLE_FILE_MAX_BYTES: usize = 100 * 1024 * 1024;
// fixme: non-normalized filenames on ankiweb // fixme: dir mod handling when downloading files in a sync
// fixme: concurrent modifications during upload step // fixme: concurrent modifications during upload step
/// The counts are not cumulative - the progress hook should accumulate them. /// The counts are not cumulative - the progress hook should accumulate them.
@ -507,18 +507,41 @@ fn record_removals(ctx: &mut MediaDatabaseContext, removals: &[&String]) -> Resu
fn record_additions(ctx: &mut MediaDatabaseContext, additions: Vec<AddedFile>) -> Result<()> { fn record_additions(ctx: &mut MediaDatabaseContext, additions: Vec<AddedFile>) -> Result<()> {
for file in additions { for file in additions {
let entry = MediaEntry { if let Some(renamed) = file.renamed_from {
fname: file.fname.to_string(), // the file AnkiWeb sent us wasn't normalized, so we need to record
sha1: Some(file.sha1), // the old file name as a deletion
mtime: file.mtime, debug!("marking non-normalized file as deleted: {}", renamed);
sync_required: false, let mut entry = MediaEntry {
}; fname: renamed,
debug!( sha1: None,
"marking added: {} {}", mtime: 0,
entry.fname, sync_required: true,
hex::encode(entry.sha1.as_ref().unwrap()) };
); ctx.set_entry(&entry)?;
ctx.set_entry(&entry)?; // and upload the new filename to ankiweb
debug!("marking renamed file as needing upload: {}", file.fname);
entry = MediaEntry {
fname: file.fname.to_string(),
sha1: Some(file.sha1),
mtime: file.mtime,
sync_required: true,
};
ctx.set_entry(&entry)?;
} else {
// a normal addition
let entry = MediaEntry {
fname: file.fname.to_string(),
sha1: Some(file.sha1),
mtime: file.mtime,
sync_required: false,
};
debug!(
"marking added: {} {}",
entry.fname,
hex::encode(entry.sha1.as_ref().unwrap())
);
ctx.set_entry(&entry)?;
}
} }
Ok(()) Ok(())