77 lines
2.2 KiB
Rust
77 lines
2.2 KiB
Rust
use std::convert::identity;
|
|
|
|
use actix_identity::Identity;
|
|
/*
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
use actix_web::{http::header::ContentType, web, HttpRequest, HttpResponse, Responder};
|
|
use serde::{Serialize, Deserialize};
|
|
use tokio::fs;
|
|
|
|
use crate::errors::*;
|
|
use crate::pages;
|
|
use crate::AppCtx;
|
|
|
|
pub mod routes {
|
|
use serde::Serialize;
|
|
|
|
#[derive(Serialize)]
|
|
pub struct Serve {
|
|
pub catch_all: &'static str,
|
|
}
|
|
|
|
impl Serve {
|
|
pub const fn new() -> Self {
|
|
Self {
|
|
catch_all: "/archive",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
struct Q {
|
|
url: url::Url,
|
|
}
|
|
|
|
#[actix_web_codegen_const_routes::get(path = "crate::pages::PAGES.serve.catch_all")]
|
|
#[tracing::instrument(name = "Serve webpages", skip(ctx, id, q))]
|
|
async fn serve_webpage(q: web::Query<(Q)>, ctx: AppCtx, id: Identity) -> ServiceResult<impl Responder> {
|
|
let url = q.into_inner().url;
|
|
|
|
|
|
let id = id.identity().unwrap();
|
|
if ctx.db.url_exists(url.as_str()).await? {
|
|
let path = crate::utils::get_website_path(&ctx.settings, &id, &url);
|
|
let content = fs::read(&path).await?;
|
|
let mime = if let Some(mime) = mime_guess::from_path(&path).first_raw() {
|
|
mime
|
|
} else {
|
|
"text/html; charset=utf-8"
|
|
};
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type(mime)
|
|
.body(content))
|
|
} else {
|
|
Err(ServiceError::WebsiteNotFound)
|
|
}
|
|
}
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(serve_webpage);
|
|
}
|