Skip to content

Commit

Permalink
fix: Change image API endpoint (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
afifurrohman-id authored Jul 6, 2024
1 parent 3e4784d commit 053d413
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ release: src/lib.rs
test: src/lib.rs
wasm-pack test --node

server: compose.yaml
run: compose.yaml
docker compose up -d


Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ cd enku
> image should meet following requirement:
- [x] Valid jpeg format
- [x] Valid png format
- [x] 512x512 height and width
- [x] Filename is `sample.jpeg`
- [x] Filename is `sample.png`


### Run

- Run Server

```sh
make server
make run
```

- Build app (debug)
Expand Down
33 changes: 23 additions & 10 deletions src/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response, Url};

use crate::util::rand;

use super::common;

const IMAGE_FORMAT: ImageFormat = ImageFormat::Png;

pub fn crop_image(buff: &[u8]) -> Vec<Vec<u8>> {
const DIMENSION_SIZE: u32 = 512;
const PART_SIZE: u32 = 100;
// const PART_SIZE: u32 = 50;

let mut img = image::load_from_memory_with_format(buff, image::ImageFormat::Jpeg).unwrap();
let mut img = image::load_from_memory_with_format(buff, IMAGE_FORMAT).unwrap();

let (width, height) = img.dimensions();
assert_eq!(width, DIMENSION_SIZE, "width must be 512");
Expand All @@ -32,7 +36,7 @@ pub fn crop_image(buff: &[u8]) -> Vec<Vec<u8>> {

let mut buffer = Cursor::new(Vec::new());
cropped_img
.write_to(&mut buffer, ImageFormat::Jpeg)
.write_to(&mut buffer, IMAGE_FORMAT)
.unwrap();
let buffer = buffer.into_inner();
results.push(buffer)
Expand All @@ -45,7 +49,7 @@ pub fn create_imgs_url(imgs: &[Vec<u8>]) -> Vec<String> {
let mut urls = Vec::new();

for buff in imgs {
let blob = common::bytes_to_js_blob(buff, ImageFormat::Jpeg.to_mime_type());
let blob = common::bytes_to_js_blob(buff, IMAGE_FORMAT.to_mime_type());

let url = Url::create_object_url_with_blob(&blob).unwrap();
urls.push(url);
Expand All @@ -56,26 +60,35 @@ pub fn create_imgs_url(imgs: &[Vec<u8>]) -> Vec<String> {

#[allow(unused_variables)]
pub async fn load_image(query: &str) -> Vec<u8> {
const IMAGE_COUNT: usize = 3;

let x = rand(IMAGE_COUNT) + 1;

let ext = IMAGE_FORMAT.extensions_str().first().unwrap();

#[cfg(not(debug_assertions))]
let url = format!(
"https://source.unsplash.com/random/512x512?{}",
"https://acbtwffwdoxzwlfgpisu.supabase.co/storage/v1/object/public/projects/enku/images/random/{}/{}.{}",
query.to_lowercase(),
x,
ext
);

#[cfg(debug_assertions)]
let url = format!(
"{}sample.jpeg",
web_sys::window().unwrap().location().href().unwrap()
"{}sample.{}",
web_sys::window().unwrap().location().href().unwrap(),
ext
);

let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);

let req = Request::new_with_str_and_init(&url, &opts).unwrap();
let jpeg_mime = ImageFormat::Jpeg.to_mime_type();
let mime_img = IMAGE_FORMAT.to_mime_type();

req.headers().set("Accept", jpeg_mime).unwrap();
req.headers().set("Accept", mime_img).unwrap();

let window = web_sys::window().unwrap();

Expand All @@ -87,8 +100,8 @@ pub async fn load_image(query: &str) -> Vec<u8> {
let res = res.dyn_into::<Response>().unwrap();

if let Some(ct) = res.headers().get("Content-Type").unwrap_or_default() {
if !ct.contains(jpeg_mime) {
panic!("image must be jpeg format");
if !ct.contains(mime_img) {
panic!("image must be {} format", mime_img);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Display;

use web_sys::js_sys::Math;

/// add a random number between 0 and count-1
pub fn rand(count: usize) -> usize {
Math::floor(Math::random() * count as f64) as usize
}
Expand Down

0 comments on commit 053d413

Please sign in to comment.