Commit 7093450de53ec62ced4a1d98f07d24d7fd3b8567

Authored by Georg Hopp
1 parent 445ad0fa

Some fixes in upload handling

@@ -51,7 +51,7 @@ pub fn launch() -> Sender<(Arc<Pool>, Image)> { @@ -51,7 +51,7 @@ pub fn launch() -> Sender<(Arc<Pool>, Image)> {
51 tx_upload_worker 51 tx_upload_worker
52 } 52 }
53 53
54 -async fn store_original(context :&mut ImageContext) -> Result<(), Error> { 54 +async fn store_original(context :&mut ImageContext) -> Result<u64, Error> {
55 let upload_path = context 55 let upload_path = context
56 . upload_path().await 56 . upload_path().await
57 . ok_or(Error::new( "Can't retreive upload path" 57 . ok_or(Error::new( "Can't retreive upload path"
@@ -62,16 +62,21 @@ async fn store_original(context :&mut ImageContext) -> Result<(), Error> { @@ -62,16 +62,21 @@ async fn store_original(context :&mut ImageContext) -> Result<(), Error> {
62 . ok_or(Error::new( "No path for given size" 62 . ok_or(Error::new( "No path for given size"
63 , StatusCode::INTERNAL_SERVER_ERROR ))?; 63 , StatusCode::INTERNAL_SERVER_ERROR ))?;
64 64
65 - match metadata(&original_path).await { 65 + let result = match metadata(&original_path).await {
66 Err(e) if e.kind() == ErrorKind::NotFound => { 66 Err(e) if e.kind() == ErrorKind::NotFound => {
67 - copy(&upload_path, &original_path).await?;  
68 - remove_file(&upload_path).await?;  
69 - Ok(()) 67 + match copy(&upload_path, &original_path).await {
  68 + Ok(size) => Ok(size),
  69 + Err(e) => Err(Error::from(e)),
  70 + }
70 }, 71 },
71 - Err(e) => Err(e)?, 72 + Err(e) => Err(Error::from(e)),
72 Ok(_) => Err(Error::new( "File already exists" 73 Ok(_) => Err(Error::new( "File already exists"
73 , StatusCode::CONFLICT )), 74 , StatusCode::CONFLICT )),
74 - } 75 + };
  76 +
  77 + remove_file(&upload_path).await?;
  78 +
  79 + result
75 } 80 }
76 81
77 async fn load_original(context :&mut ImageContext) -> Result<DynamicImage, Error> { 82 async fn load_original(context :&mut ImageContext) -> Result<DynamicImage, Error> {
@@ -139,7 +144,7 @@ async fn worker(pool :Arc<Pool>, image :Image) -> Result<(), Error> { @@ -139,7 +144,7 @@ async fn worker(pool :Arc<Pool>, image :Image) -> Result<(), Error> {
139 . create(base_path) 144 . create(base_path)
140 . await?; 145 . await?;
141 146
142 - store_original(&mut context).await.unwrap_or(()); 147 + store_original(&mut context).await.unwrap_or(0);
143 148
144 if let Ok(original) = load_original(&mut context).await { 149 if let Ok(original) = load_original(&mut context).await {
145 let (dim_x, dim_y) = original.dimensions(); 150 let (dim_x, dim_y) = original.dimensions();
@@ -104,28 +104,23 @@ pub(super) async fn upload_logic( mut rx_logic :broadcast::Receiver<UploadLogic> @@ -104,28 +104,23 @@ pub(super) async fn upload_logic( mut rx_logic :broadcast::Receiver<UploadLogic>
104 } 104 }
105 }, 105 },
106 UploadLogic::Upload => { 106 UploadLogic::Upload => {
107 - let mut remove_ids = vec![]; 107 + let mut uploads_vec = vec![];
  108 + let mut index = 0;
108 109
109 for upload in uploads.read().await.iter() { 110 for upload in uploads.read().await.iter() {
110 - match api.store(upload).await {  
111 - Ok(_) => remove_ids.push(upload.id),  
112 - Err(e) => log::error!("{:?}", e),  
113 - } 111 + uploads_vec.push(upload.clone());
114 } 112 }
115 113
116 - for id in remove_ids.iter() {  
117 - let mut found = None;  
118 -  
119 - for (upload, index) in uploads.read().await.iter().zip(0..) {  
120 - if upload.id == *id {  
121 - found = Some(index);  
122 - break; 114 + for upload in uploads_vec.iter() {
  115 + match api.store(upload).await {
  116 + Ok(_) => {
  117 + uploads.list_patch_remove(index).unwrap();
  118 + },
  119 + Err(e) => {
  120 + log::error!("{:?}", e);
  121 + index += 1;
123 } 122 }
124 } 123 }
125 -  
126 - if let Some(index) = found {  
127 - uploads.list_patch_remove(index).unwrap();  
128 - }  
129 } 124 }
130 } 125 }
131 } 126 }
1 -mod logic; 1 +pub(crate) mod logic;
2 pub(crate) mod upload; 2 pub(crate) mod upload;
3 mod view; 3 mod view;
4 4
Please register or login to post a comment