Commit 88cfb041f44fad42920b1ec8395b0fec84905075
1 parent
28f4e90b
Ensure uploads of same files will be cleaned from the database
Showing
1 changed file
with
48 additions
and
31 deletions
... | ... | @@ -22,7 +22,7 @@ use image::{ io::Reader as ImageReader |
22 | 22 | |
23 | 23 | pub fn launch() -> Sender<(Arc<Pool>, Image)> { |
24 | 24 | let (tx_upload_worker, rx_upload_worker) |
25 | - : (Sender<(Arc<Pool>, Image)>, Receiver<(Arc<Pool>, Image)>) = bounded(32); | |
25 | + : (Sender<(Arc<Pool>, Image)>, Receiver<(Arc<Pool>, Image)>) = bounded(100); | |
26 | 26 | |
27 | 27 | actix_rt::spawn(async move { |
28 | 28 | let mut workers = FuturesUnordered::new(); |
... | ... | @@ -69,7 +69,8 @@ async fn store_original(context :&mut ImageContext) -> Result<(), Error> { |
69 | 69 | Ok(()) |
70 | 70 | }, |
71 | 71 | Err(e) => Err(e)?, |
72 | - Ok(_) => Ok(()), | |
72 | + Ok(_) => Err(Error::new( "File already exists" | |
73 | + , StatusCode::CONFLICT )), | |
73 | 74 | } |
74 | 75 | } |
75 | 76 | |
... | ... | @@ -101,48 +102,64 @@ async fn save_resized( original :&DynamicImage |
101 | 102 | |
102 | 103 | let original = original.to_owned(); |
103 | 104 | |
104 | - spawn_blocking(move || -> Result<(), Error> { | |
105 | - let mut scaled = original.resize(width, height, Lanczos3); | |
106 | - overlay(&mut scaled, CONFIG.copyright_image(), 0_u32, 0_u32); | |
107 | - | |
108 | - if let Size::Thumbnail = size { | |
109 | - scaled.save_with_format(&path, Jpeg)?; | |
110 | - } else { | |
111 | - let stegonography = CONFIG.copyright_steganography().as_bytes(); | |
112 | - let encoder = Encoder::new(stegonography, scaled); | |
113 | - let scaled = encoder.encode_alpha(); | |
114 | - scaled.save_with_format(&path, Jpeg)?; | |
115 | - } | |
105 | + match metadata(&path).await { | |
106 | + Err(e) if e.kind() == ErrorKind::NotFound => | |
107 | + spawn_blocking(move || -> Result<(), Error> { | |
108 | + let mut scaled = original.resize(width, height, Lanczos3); | |
109 | + overlay(&mut scaled, CONFIG.copyright_image(), 0_u32, 0_u32); | |
110 | + | |
111 | + if let Size::Thumbnail = size { | |
112 | + scaled.save_with_format(&path, Jpeg)?; | |
113 | + } else { | |
114 | + let stegonography = CONFIG.copyright_steganography().as_bytes(); | |
115 | + let encoder = Encoder::new(stegonography, scaled); | |
116 | + let scaled = encoder.encode_alpha(); | |
117 | + scaled.save_with_format(&path, Jpeg)?; | |
118 | + } | |
116 | 119 | |
117 | - let exiv = Metadata::new_from_path(&path)?; | |
118 | - exiv.set_tag_string("Exif.Image.Copyright", CONFIG.copyright_exiv())?; | |
119 | - exiv.save_to_file(&path)?; | |
120 | + let exiv = Metadata::new_from_path(&path)?; | |
121 | + exiv.set_tag_string("Exif.Image.Copyright", CONFIG.copyright_exiv())?; | |
122 | + exiv.save_to_file(&path)?; | |
120 | 123 | |
121 | - Ok(()) | |
122 | - }).await | |
124 | + Ok(()) | |
125 | + }).await, | |
126 | + Err(e) => Err(e)?, | |
127 | + Ok(_) => Err(Error::new( "File already exists" | |
128 | + , StatusCode::CONFLICT )), | |
129 | + } | |
123 | 130 | } |
124 | 131 | |
125 | 132 | async fn worker(pool :Arc<Pool>, image :Image) -> Result<(), Error> { |
126 | 133 | let mut context = image.context(); |
134 | + let base_path = context.base_path().await | |
135 | + . ok_or(Error::new( "Missing base_path" | |
136 | + , StatusCode::INTERNAL_SERVER_ERROR ))?; | |
127 | 137 | |
128 | 138 | DirBuilder::new() . recursive(true) |
129 | - . create(context.base_path().await.unwrap()) | |
130 | - . await | |
131 | - . unwrap(); | |
139 | + . create(base_path) | |
140 | + . await?; | |
141 | + | |
142 | + store_original(&mut context).await.unwrap_or(()); | |
132 | 143 | |
133 | - store_original(&mut context).await.unwrap(); | |
144 | + if let Ok(original) = load_original(&mut context).await { | |
145 | + let (dim_x, dim_y) = original.dimensions(); | |
134 | 146 | |
135 | - let original = load_original(&mut context).await.unwrap(); | |
136 | - let (dim_x, dim_y) = original.dimensions(); | |
147 | + context.image.dim_x = Some(dim_x as i32); | |
148 | + context.image.dim_y = Some(dim_y as i32); | |
137 | 149 | |
138 | - context.image.dim_x = Some(dim_x as i32); | |
139 | - context.image.dim_y = Some(dim_y as i32); | |
150 | + macro_rules! save_resized{ | |
151 | + ($s:expr) => { save_resized(&original, &mut context, $s) } | |
152 | + } | |
140 | 153 | |
141 | - save_resized(&original, &mut context, Size::Large).await?; | |
142 | - save_resized(&original, &mut context, Size::Medium).await?; | |
143 | - save_resized(&original, &mut context, Size::Small).await?; | |
144 | - save_resized(&original, &mut context, Size::Thumbnail).await?; | |
154 | + save_resized!(Size::Large).await.unwrap_or(()); | |
155 | + save_resized!(Size::Medium).await.unwrap_or(()); | |
156 | + save_resized!(Size::Small).await.unwrap_or(()); | |
157 | + save_resized!(Size::Thumbnail).await.unwrap_or(()); | |
158 | + } | |
145 | 159 | |
160 | + // TODO Think about two simpler functions than finanlize... | |
161 | + // One to update one do remove the new entry depending if the | |
162 | + // entry exists already... | |
146 | 163 | web::block(move || finalize(pool, context.image)).await?; |
147 | 164 | |
148 | 165 | Ok(()) | ... | ... |
Please
register
or
login
to post a comment