diff --git a/pallets/file-bank/src/impls/receptionist.rs b/pallets/file-bank/src/impls/receptionist.rs index 30e5bd9c..05ed995a 100644 --- a/pallets/file-bank/src/impls/receptionist.rs +++ b/pallets/file-bank/src/impls/receptionist.rs @@ -3,23 +3,32 @@ use crate::*; pub struct Receptionist(PhantomData); impl Receptionist { - pub fn fly_upload_file(file_hash: Hash, user_brief: UserBrief, used_space: u128) -> DispatchResult { - T::StorageHandle::update_user_space(&user_brief.user, 1, used_space)?; + pub fn fly_upload_file(file_hash: Hash, user_brief: UserBrief) -> DispatchResult { - if >::contains_key(&user_brief.user, &user_brief.bucket_name) { - Pallet::::add_file_to_bucket(&user_brief.user, &user_brief.bucket_name, &file_hash)?; - } else { - Pallet::::create_bucket_helper(&user_brief.user, &user_brief.bucket_name, Some(file_hash))?; - } - - Pallet::::add_user_hold_fileslice(&user_brief.user, file_hash, used_space)?; >::try_mutate(&file_hash, |file_opt| -> DispatchResult { let file = file_opt.as_mut().ok_or(Error::::FileNonExistent)?; + let needed_space = SEGMENT_SIZE + .checked_mul(15).ok_or(Error::::Overflow)? + .checked_div(10).ok_or(Error::::Overflow)? + .checked_mul(file.segment_list.len() as u128).ok_or(Error::::Overflow)?; + ensure!(T::StorageHandle::get_user_avail_space(&user_brief.user)? > needed_space, Error::::InsufficientAvailableSpace); + T::StorageHandle::update_user_space(&user_brief.user, 1, needed_space)?; + + if >::contains_key(&user_brief.user, &user_brief.bucket_name) { + Pallet::::add_file_to_bucket(&user_brief.user, &user_brief.bucket_name, &file_hash)?; + } else { + Pallet::::create_bucket_helper(&user_brief.user, &user_brief.bucket_name, Some(file_hash))?; + } + + Pallet::::add_user_hold_fileslice(&user_brief.user, file_hash, needed_space)?; file.owner.try_push(user_brief.clone()).map_err(|_e| Error::::BoundedVecError)?; + Ok(()) })?; + + Ok(()) } diff --git a/pallets/file-bank/src/lib.rs b/pallets/file-bank/src/lib.rs index ddccf3d6..b19b3e49 100755 --- a/pallets/file-bank/src/lib.rs +++ b/pallets/file-bank/src/lib.rs @@ -435,15 +435,14 @@ pub mod pallet { ensure!(user_brief.file_name.len() as u32 >= minimum, Error::::SpecError); ensure!(user_brief.bucket_name.len() as u32 >= minimum, Error::::SpecError); - let needed_space = SEGMENT_SIZE - .checked_mul(15).ok_or(Error::::Overflow)? - .checked_div(10).ok_or(Error::::Overflow)? - .checked_mul(deal_info.len() as u128).ok_or(Error::::Overflow)?; - ensure!(T::StorageHandle::get_user_avail_space(&user_brief.user)? > needed_space, Error::::InsufficientAvailableSpace); - if >::contains_key(&file_hash) { - Receptionist::::fly_upload_file(file_hash, user_brief.clone(), needed_space)?; + Receptionist::::fly_upload_file(file_hash, user_brief.clone())?; } else { + let needed_space = SEGMENT_SIZE + .checked_mul(15).ok_or(Error::::Overflow)? + .checked_div(10).ok_or(Error::::Overflow)? + .checked_mul(deal_info.len() as u128).ok_or(Error::::Overflow)?; + ensure!(T::StorageHandle::get_user_avail_space(&user_brief.user)? > needed_space, Error::::InsufficientAvailableSpace); Receptionist::::generate_deal(file_hash, deal_info, user_brief.clone(), needed_space, file_size)?; }