Create macro to make HTTP response of image
authorMegaBrutal <code+git@megabrutal.com>
Sun, 27 Nov 2022 01:45:00 +0000 (02:45 +0100)
committerMegaBrutal <code+git@megabrutal.com>
Sun, 27 Nov 2022 01:45:00 +0000 (02:45 +0100)
src/main.rs

index 0405e228365a2954edf816918cf0e681675848c0..854f2c0ae366799a6615a88c45156d3d398c7817 100644 (file)
@@ -22,6 +22,14 @@ use actix_web::cookie::time::OffsetDateTime;
 const TIMEAVATAR_SIZE_U32: u32 = 6;
 const TIMEAVATAR_SIZE_I64: i64 = TIMEAVATAR_SIZE_U32 as i64;
 
+macro_rules! response_image {
+    ($cursor:expr) => {
+        HttpResponse::build(StatusCode::OK)
+            .content_type("image/png")
+            .body($cursor.into_inner())
+    }
+}
+
 #[derive(Debug)]
 struct ImageError(image::ImageError);
 
@@ -243,9 +251,7 @@ fn image_to_cursor(img: RgbImage, scale: u32) -> Result<Cursor<Vec<u8>>, ImageEr
 async fn img_gen0(req: HttpRequest) -> Result<impl Responder> {
     let data = req.uri().path().split('/').nth(3).unwrap();
     let cursor = make_png(32, 32, 16, percent_decode_str(data).into_iter().borrow_mut())?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/gen/1/{dim_x}/{dim_y}/{scale}/{data}")]
@@ -253,9 +259,7 @@ async fn img_gen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<
     let (dim_x, dim_y, scale) = path.into_inner();
     let data = req.uri().path().split('/').nth(6).unwrap();
     let cursor = make_png(dim_x, dim_y, scale, percent_decode_str(data).into_iter().borrow_mut())?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/gen/2/{dim_x}/{dim_y}/{scale}")]
@@ -266,9 +270,7 @@ async fn img_gen2(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<
         _ => ""
     };
     let cursor = make_png(dim_x, dim_y, scale, &mut data.bytes())?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/gen/3/{scale}")]
@@ -284,9 +286,7 @@ async fn img_gen3(req: HttpRequest, path: web::Path<u32>) -> Result<impl Respond
     let (hour, minute): (i64, i64) = (time.hour().into(), time.minute().into());
     overlay(&mut resimg, &rgbimg, minute * TIMEAVATAR_SIZE_I64, hour * TIMEAVATAR_SIZE_I64);
     let cursor = image_to_cursor(resimg, scale)?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/pgen/0/{data}")]
@@ -298,9 +298,7 @@ async fn img_pgen0(req: HttpRequest, canvas0: web::Data<Arc<Mutex<Canvas0>>>) ->
         Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, None, data)?.img.clone())
     } as Result<RgbImage, CanvasError>)?;
     let cursor = image_to_cursor(img, scale)?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/pgen/1/{dim_x}/{dim_y}/{scale}/{data}")]
@@ -312,9 +310,7 @@ async fn img_pgen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1:
         Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone())
     } as Result<RgbImage, CanvasError>)?;
     let cursor = image_to_cursor(img, scale)?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/pgen/2/{dim_x}/{dim_y}/{scale}")]
@@ -329,9 +325,7 @@ async fn img_pgen2(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1:
         Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone())
     } as Result<RgbImage, CanvasError>)?;
     let cursor = image_to_cursor(img, scale)?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/pgen/3/{scale}")]
@@ -354,9 +348,7 @@ async fn img_pgen3(req: HttpRequest, path: web::Path<u32>, canvas2: web::Data<Ar
         Ok(canvas_option.0.insert(canvas).img.clone())
     } as Result<RgbImage, CanvasError>)?;
     let cursor = image_to_cursor(resimg, scale)?;
-    Ok(HttpResponse::build(StatusCode::OK)
-       .content_type("image/png")
-       .body(cursor.into_inner()))
+    Ok(response_image!(cursor))
 }
 
 #[get("/pdrop/{canvas_id}")]