don't duplicate file if it already exists
parent
3cc0dcff45
commit
1f058670f8
|
@ -214,28 +214,34 @@ pub async fn put_object(
|
|||
}
|
||||
let path = PathBuf::from(file.path());
|
||||
let hash = web::block(move || path.hash()).await?;
|
||||
|
||||
let address = Address::Hash(hash.clone());
|
||||
|
||||
let addr_str = encode(address.encode().map_err(ErrorInternalServerError)?);
|
||||
let final_name = if let Some(filename) = filename {
|
||||
format!("{addr_str}_{filename}")
|
||||
} else {
|
||||
addr_str
|
||||
};
|
||||
|
||||
let final_path = state.upend.vault_path.join(&final_name);
|
||||
|
||||
let (_, tmp_path) = file.keep().map_err(ErrorInternalServerError)?;
|
||||
let final_path = web::block::<_, _, io::Error>(move || {
|
||||
fs::copy(&tmp_path, &final_path)?;
|
||||
fs::remove_file(tmp_path)?;
|
||||
Ok(final_path)
|
||||
})
|
||||
.await?;
|
||||
|
||||
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
|
||||
add_file(&connection, &final_path, hash).map_err(ErrorInternalServerError)?;
|
||||
|
||||
let existing_files = connection
|
||||
.retrieve_file(hash.clone())
|
||||
.map_err(ErrorInternalServerError)?;
|
||||
|
||||
if existing_files.is_empty() {
|
||||
let addr_str = encode(address.encode().map_err(ErrorInternalServerError)?);
|
||||
let final_name = if let Some(filename) = filename {
|
||||
format!("{addr_str}_{filename}")
|
||||
} else {
|
||||
addr_str
|
||||
};
|
||||
|
||||
let final_path = state.upend.vault_path.join(&final_name);
|
||||
|
||||
let (_, tmp_path) = file.keep().map_err(ErrorInternalServerError)?;
|
||||
let final_path = web::block::<_, _, io::Error>(move || {
|
||||
fs::copy(&tmp_path, &final_path)?;
|
||||
fs::remove_file(tmp_path)?;
|
||||
Ok(final_path)
|
||||
})
|
||||
.await?;
|
||||
|
||||
add_file(&connection, &final_path, hash).map_err(ErrorInternalServerError)?;
|
||||
}
|
||||
|
||||
if let Some(filename) = filename {
|
||||
let _ = upend_insert_val!(&connection, address, "LBL", filename);
|
||||
|
|
Loading…
Reference in New Issue