rust-embed icon indicating copy to clipboard operation
rust-embed copied to clipboard

"GET /img/menu_logo.76408655.png HTTP/1.1" 404

Open qxqxq opened this issue 3 years ago • 8 comments
trafficstars

"GET /img/menu_logo.76408655.png HTTP/1.1" 404 [2022-05-12T10:29:03Z INFO xdls::controller::statics] img-params===Path("menu_logo.76408655.png") [2022-05-12T10:29:03Z INFO xdls::controller::statics] handle_embedded_file path====img/menu_logo.76408655.png

"GET /fonts/element-icons.535877f5.woff HTTP/1.1" 404 [2022-05-12T10:30:10Z INFO xdls::controller::statics] fonts-params===Path("element-icons.535877f5.woff") [2022-05-12T10:30:10Z INFO xdls::controller::statics] handle_embedded_file path====fonts/element-icons.535877f5.woff

"GET /css/menu_logo.76408655.png HTTP/1.1" 200 [2022-05-12T10:27:20Z INFO xdls::controller::statics] handle_embedded_file path====css/menu_logo.76408655.png

"GET /img/logs.fa1e7ddd.css HTTP/1.1" 200 [2022-05-12T10:25:39Z INFO xdls::controller::statics] img-params===Path("logs.fa1e7ddd.css") [2022-05-12T10:25:39Z INFO xdls::controller::statics] handle_embedded_file path====img/logs.fa1e7ddd.css

Currently all files in the Fonts folder will be 404. Currently all files in the IMG folder except logs.fa1e7ddd.css are 404.

actix-web = "4.0.0" rust-embed = "6.4.0"

MacBookPro:static qxq$ tree . ├── css │   ├── app.2748f048.css │   ├── chunk-vendors.4ba11380.css │   ├── chunk-vendors.4ba11380.css.gz │   ├── config.39fdc8fc.css │   ├── config~home.7ae7c0ef.css │   ├── home.ad65ad08.css │   ├── login.6d5cd834.css │   ├── logs.fa1e7ddd.css │   ├── menu_logo.76408655.png │   └── system.9e578feb.css ├── favicon.ico ├── fonts │   ├── element-icons.535877f5.woff │   └── element-icons.732389de.ttf ├── img │   ├── detection1.0aea7e16.jpg │   ├── detection2.b13e79df.jpg │   ├── icon_car.b91f3b70.png │   ├── icon_radar.e1631f11.png │   ├── img_1.8229e084.png │   ├── logo.734d0fe1.png │   ├── logs.fa1e7ddd.css │   └── menu_logo.76408655.png ├── index.html └── js ├── app.e012053f.js ├── app.e012053f.js.gz ├── chunk-vendors.17c8ec50.js ├── chunk-vendors.17c8ec50.js.gz ├── config.7094d2aa.js ├── config.7094d2aa.js.gz ├── config~home.fe5be4d4.js ├── config~home.fe5be4d4.js.gz ├── config~home~login~logs~system.5fa4ace5.js ├── config~home~login~logs~system.5fa4ace5.js.gz ├── home.17d84104.js ├── home.17d84104.js.gz ├── login.f2d94fbe.js ├── logs.d11034de.js ├── system.98ea62e3.js └── system.98ea62e3.js.gz

4 directories, 38 files

#[derive(RustEmbed)]
#[folder = "static/"]
struct Asset;


fn handle_embedded_file(path: &str) -> HttpResponse {
    log::info!("handle_embedded_file path===={}", path);
    match Asset::get(path) {
        Some(content) => {
            // let body: Vec<u8> = match content.data {
            //     Cow::Borrowed(bytes) => {
            //         bytes.into()
            //     },
            //     Cow::Owned(bytes) => {
            //         bytes.into()
            //     }
            // };
            let body = content.data.into_owned();

            HttpResponse::Ok().content_type(from_path(path).first_or_octet_stream().as_ref()).body(body)
        }
        None => HttpResponse::NotFound().body("404 Not Found"),
    }
}

pub async fn index() -> HttpResponse {
    handle_embedded_file("index.html")
}

pub async fn css(params: web::Path<String>) -> HttpResponse {
    let path = format!("css/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn js(params: web::Path<String>) -> HttpResponse {
    let path = format!("js/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn fonts(params: web::Path<String>) -> HttpResponse {
    log::info!("fonts-params==={:?}", params);
    let path = format!("fonts/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn img(params: web::Path<String>) -> HttpResponse {
    log::info!("img-params==={:?}", params);
    let path = format!("img/{}", params.into_inner());
    handle_embedded_file(&path)
}

pub async fn ico() -> HttpResponse {
    handle_embedded_file("favicon.ico")
}

.service(
            web::resource("/")
                .route(web::get().to(statics::index)),
        )
        .route("/css/{path}", web::get().to(statics::css))
        .route("/js/{path}", web::get().to(statics::js))
        .route("/fonts/{path}", web::get().to(statics::fonts))
        .route("/img/{path}", web::get().to(statics::img))
        .route("/favicon.ico", web::get().to(statics::ico))

qxqxq avatar May 12 '22 10:05 qxqxq

@qxqxq have you taken a look at the example, You should be able to handle it with the single route handler. It seems to me that you could be missing the /static prefix in the path when calling handle_embedded_file.

pyrossh avatar May 12 '22 12:05 pyrossh

@qxqxq have you taken a look at the example, You should be able to handle it with the single route handler. It seems to me that you could be missing the /static prefix in the path when calling handle_embedded_file.

How to handle it using a single routing handler, I don't know. In my opinion, the current code is just not very elegant. I don't think it's the absence of the /static prefix in the path. You can see my log in handle_Embedded_FILE. You can't explain why only a few files failed.

"GET /img/menu_logo.76408655.png HTTP/1.1" 404 [2022-05-12T10:29:03Z INFO xdls::controller::statics] img-params===Path("menu_logo.76408655.png") [2022-05-12T10:29:03Z INFO xdls::controller::statics] handle_embedded_file path====img/menu_logo.76408655.png

"GET /img/logs.fa1e7ddd.css HTTP/1.1" 200 [2022-05-12T10:25:39Z INFO xdls::controller::statics] img-params===Path("logs.fa1e7ddd.css") [2022-05-12T10:25:39Z INFO xdls::controller::statics] handle_embedded_file path====img/logs.fa1e7ddd.css

Take a look at the file in the static directory. Why one can succeed and the other can't.

qxqxq avatar May 12 '22 22:05 qxqxq

@pyrossh There are 4 directories and 38 files in the static folder. There are 8 files in the IMG folder. Why only 7 files in img folder and 2 files in fonts folder will fail while the other 29 files are successful?

qxqxq avatar May 12 '22 23:05 qxqxq

@pyrossh The example route prefix is dist, while my route prefix is JS and CSS and IMG and fonts.So I don't know how to handle it with the single route handle.

qxqxq avatar May 12 '22 23:05 qxqxq

In this case you would use /static instead of /dist and also you would need to prefix the html/webpack to use /static/js instead of just js a simpler approach. Maybe that would be easier to figure out this bug. But anyways those files and images which are missing could have an issue in the path/name I'm guessing. A simple example repo to reproduce this will help debugging it.

pyrossh avatar May 13 '22 01:05 pyrossh

@pyrossh All files were successful in Cargo Build. An error occurs only when cargo Build --release is used.

qxqxq avatar May 13 '22 02:05 qxqxq

In this case you would use /static instead of /dist and also you would need to prefix the html/webpack to use /static/js instead of just js a simpler approach. Maybe that would be easier to figure out this bug. But anyways those files and images which are missing could have an issue in the path/name I'm guessing. A simple example repo to reproduce this will help debugging it.

I tried to work with HTML/Webpack in the same way as the example.

qxqxq avatar May 13 '22 02:05 qxqxq

Ahh ok it seems to fail in release only. Also it seems .css file in img directory works. Can you try with another css file in that directory as well as in the fonts directory and see if that works. Seems to me maybe in release maybe it's failing to load other content types because css is a text format whereas images are binary format.

Also do disable your cache while testing maybe that would help in finding out.

pyrossh avatar May 13 '22 02:05 pyrossh

Closing this due to inactivity.

pyrossh avatar Nov 23 '22 17:11 pyrossh