refactor!: setup file proxy for projects

This commit is contained in:
2026-01-20 19:10:01 -08:00
parent 5eccfe32da
commit a2afc3fa05
17 changed files with 279 additions and 103 deletions

View File

@@ -0,0 +1,47 @@
use actix_web::{HttpResponse, web};
use crate::{
AppState,
error::{Error, Result},
};
#[derive(serde::Deserialize)]
pub struct Params {
repo_id: String,
file: String,
}
pub async fn proxy_file(
app_state: web::Data<AppState>,
path: web::Path<Params>,
) -> Result<HttpResponse> {
let repo = app_state
.user
.get_approved_repository(&path.repo_id)
.await?
.ok_or(Error::NotFound)?;
let token = app_state.user.get_access_token(&repo.owner_id).await?;
let url = format!(
"https://raw.githubusercontent.com/{}/HEAD/dist/{}",
repo.full_name, path.file
);
let response = app_state
.reqwest_client
.get(&url)
.bearer_auth(token)
.send()
.await?;
response.error_for_status_ref()?;
let bytes = response.bytes().await?;
let mime = mime_guess::from_path(&path.file)
.first_or_octet_stream()
.to_string();
Ok(HttpResponse::Ok()
.content_type(mime)
.insert_header(("Cache-Control", "public, max-age=3600"))
.body(bytes))
}