0.1.0-overlay

This commit is contained in:
2024-10-23 20:06:34 +03:00
parent 0d4f21c79b
commit 6e90529420
9 changed files with 556 additions and 237 deletions

View File

@@ -9,7 +9,7 @@ use crate::thumbnail::{find_closest_width, parse_file_path, thumbdata_save};
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
pub async fn proxy_handler(
_req: HttpRequest,
req: HttpRequest,
requested_res: web::Path<String>,
state: web::Data<AppState>,
) -> Result<HttpResponse, actix_web::Error> {
@@ -48,6 +48,11 @@ pub async fn proxy_handler(
warn!("content_type: {}", content_type);
let shout_id = match req.query_string().contains("s=") {
true => req.query_string().split("s=").collect::<Vec<&str>>().pop().unwrap_or(""),
false => ""
};
return match state.get_path(&filekey).await {
Ok(Some(stored_path)) => {
warn!("stored_path: {}", stored_path);
@@ -55,7 +60,7 @@ pub async fn proxy_handler(
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
if content_type.starts_with("image") {
return match requested_width == 0 {
true => serve_file(&stored_path, &state).await,
true => serve_file(&stored_path, &state, shout_id).await,
false => {
// find closest thumb width
let closest: u32 = find_closest_width(requested_width as u32);
@@ -71,7 +76,7 @@ pub async fn proxy_handler(
{
Ok(true) => {
warn!("serve existed thumb file: {}", thumb_filename);
serve_file(thumb_filename, &state).await
serve_file(thumb_filename, &state, shout_id).await
},
Ok(false) => {
if let Ok(filedata) = load_file_from_s3(
@@ -91,7 +96,7 @@ pub async fn proxy_handler(
)
.await;
warn!("serve new thumb file: {}", thumb_filename);
serve_file(thumb_filename, &state).await
serve_file(thumb_filename, &state, shout_id).await
} else {
error!("cannot generate thumbnail");
Err(ErrorInternalServerError(

View File

@@ -2,10 +2,11 @@ use actix_web::{error::ErrorInternalServerError, HttpResponse, Result};
use mime_guess::MimeGuess;
use crate::app_state::AppState;
use crate::overlay::generate_overlay;
use crate::s3_utils::check_file_exists;
/// Функция для обслуживания файла по заданному пути.
pub async fn serve_file(filepath: &str, state: &AppState) -> Result<HttpResponse, actix_web::Error> {
pub async fn serve_file(filepath: &str, state: &AppState, shout_id: &str) -> Result<HttpResponse, actix_web::Error> {
if filepath.is_empty() {
return Err(ErrorInternalServerError("Filename is empty".to_string()));
}
@@ -32,7 +33,12 @@ pub async fn serve_file(filepath: &str, state: &AppState) -> Result<HttpResponse
.await
.map_err(|_| ErrorInternalServerError("Failed to read object body"))?;
let data_bytes = data.into_bytes();
let data_bytes = match shout_id.is_empty() {
true => data.into_bytes(),
false => generate_overlay(shout_id, data.into_bytes()).await?
};
let mime_type = MimeGuess::from_path(&filepath).first_or_octet_stream();
Ok(HttpResponse::Ok()