DEVELOPMENT ENVIRONMENT

~liljamo/wallhavenapirandom

e987e8714a6c9e92b5c519cf3b0f476d58a293d0 — Jonni Liljamo 1 year, 10 months ago 83e6254
feat: serve the image straight, dont redirect
1 files changed, 29 insertions(+), 6 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +29 -6
@@ 1,4 1,6 @@
use actix_web::{self, get, web, App, HttpResponse, HttpServer, Responder};
use actix_web::{
    self, get, http::header::ContentType, web, App, HttpResponse, HttpServer, Responder,
};
use rand::seq::SliceRandom;
use reqwest;
use serde::Deserialize;


@@ 42,7 44,7 @@ struct WHSearchRes {
    pub data: Vec<WHImgInfo>,
}

/// get redirected to random img
/// get a random image
/// e.g. localhost:3000/random/2560x1440/16x9/100/001/+forest
#[get("/random/{atleast}/{ratios}/{purity}/{categories}/{q}")]
async fn random(args: web::Path<RandomArgs>) -> impl Responder {


@@ 59,7 61,9 @@ async fn random(args: web::Path<RandomArgs>) -> impl Responder {

    log::info!("doing the thing with args 'atl={atl} rats={rats} pur={pur} cats={cats} q={q}'");

    let search_resp = reqwest::get(wh_url).await;
    let client = reqwest::Client::new();

    let search_resp = client.get(wh_url).send().await;

    match search_resp {
        Err(err) => {


@@ 77,9 81,28 @@ async fn random(args: web::Path<RandomArgs>) -> impl Responder {
                Ok(res) => {
                    let imgs = res.data;
                    let url = imgs.choose(&mut rand::thread_rng()).unwrap();
                    return HttpResponse::TemporaryRedirect()
                        .append_header(("Location", url.path.clone()))
                        .finish();

                    let img_resp = client.get(url.path.clone()).send().await;

                    match img_resp {
                        Err(err) => {
                            log::error!("no voi vittu ny... err is '{err}'");
                        }
                        Ok(img_res) => {
                            let img_bytes_res = img_res.bytes().await;

                            match img_bytes_res {
                                Err(err) => {
                                    log::error!("how lol. err is '{err}'");
                                }
                                Ok(img_bytes) => {
                                    return HttpResponse::Ok()
                                        .content_type(ContentType::jpeg())
                                        .body(img_bytes);
                                }
                            }
                        }
                    }
                }
            }
        }