This commit is contained in:
parent
1c27714455
commit
f5ee1c75ee
48
Cargo.lock
generated
48
Cargo.lock
generated
|
@ -2,6 +2,31 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "actix"
|
||||||
|
version = "0.13.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "de7fa236829ba0841304542f7614c42b80fca007455315c45c785ccfa873a85b"
|
||||||
|
dependencies = [
|
||||||
|
"actix-macros",
|
||||||
|
"actix-rt",
|
||||||
|
"actix_derive",
|
||||||
|
"bitflags 2.6.0",
|
||||||
|
"bytes",
|
||||||
|
"crossbeam-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
"log",
|
||||||
|
"once_cell",
|
||||||
|
"parking_lot",
|
||||||
|
"pin-project-lite",
|
||||||
|
"smallvec",
|
||||||
|
"tokio",
|
||||||
|
"tokio-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-codec"
|
name = "actix-codec"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
|
@ -223,6 +248,17 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "actix_derive"
|
||||||
|
version = "0.6.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6ac1e58cded18cb28ddc17143c4dea5345b3ad575e14f32f66e4054a56eb271"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "addr2line"
|
name = "addr2line"
|
||||||
version = "0.24.1"
|
version = "0.24.1"
|
||||||
|
@ -1075,6 +1111,15 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-channel"
|
||||||
|
version = "0.5.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-deque"
|
name = "crossbeam-deque"
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
|
@ -1228,8 +1273,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "discoursio-quoter"
|
name = "discoursio-quoter"
|
||||||
version = "0.0.7"
|
version = "0.0.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"actix",
|
||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"aws-config",
|
"aws-config",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "discoursio-quoter"
|
name = "discoursio-quoter"
|
||||||
version = "0.0.7"
|
version = "0.0.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -23,6 +23,7 @@ aws-config = "1.5.5"
|
||||||
actix-multipart = "0.7.2"
|
actix-multipart = "0.7.2"
|
||||||
log = "0.4.22"
|
log = "0.4.22"
|
||||||
env_logger = "0.11.5"
|
env_logger = "0.11.5"
|
||||||
|
actix = "0.13.5"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "quoter"
|
name = "quoter"
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result};
|
use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result};
|
||||||
|
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
use crate::thumbnail::{find_closest_width, generate_thumbnails, parse_image_request};
|
|
||||||
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
|
|
||||||
use crate::handlers::serve_file::serve_file;
|
use crate::handlers::serve_file::serve_file;
|
||||||
|
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
|
||||||
|
use crate::thumbnail::{find_closest_width, generate_thumbnails, parse_image_request};
|
||||||
|
|
||||||
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
|
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
|
||||||
pub async fn proxy_handler(
|
pub async fn proxy_handler(
|
||||||
|
@ -17,7 +17,8 @@ pub async fn proxy_handler(
|
||||||
};
|
};
|
||||||
|
|
||||||
// парсим GET запрос
|
// парсим GET запрос
|
||||||
if let Some((base_filename, requested_width, extension)) = parse_image_request(&normalized_path) {
|
if let Some((base_filename, requested_width, extension)) = parse_image_request(&normalized_path)
|
||||||
|
{
|
||||||
let filekey = format!("{}.{}", base_filename, extension);
|
let filekey = format!("{}.{}", base_filename, extension);
|
||||||
let content_type = match extension.as_str() {
|
let content_type = match extension.as_str() {
|
||||||
"jpg" | "jpeg" => "image/jpeg",
|
"jpg" | "jpeg" => "image/jpeg",
|
||||||
|
@ -30,12 +31,11 @@ pub async fn proxy_handler(
|
||||||
"aac" => "audio/aac",
|
"aac" => "audio/aac",
|
||||||
"m4a" => "audio/m4a",
|
"m4a" => "audio/m4a",
|
||||||
"flac" => "audio/flac",
|
"flac" => "audio/flac",
|
||||||
_ => return Err(ErrorInternalServerError("unsupported file format"))
|
_ => return Err(ErrorInternalServerError("unsupported file format")),
|
||||||
};
|
};
|
||||||
|
|
||||||
return match state.get_path(&filekey).await {
|
return match state.get_path(&filekey).await {
|
||||||
Ok(Some(stored_path)) => {
|
Ok(Some(stored_path)) => {
|
||||||
|
|
||||||
// we have stored file path in storj
|
// we have stored file path in storj
|
||||||
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
||||||
if content_type.starts_with("image") {
|
if content_type.starts_with("image") {
|
||||||
|
@ -44,72 +44,105 @@ pub async fn proxy_handler(
|
||||||
false => {
|
false => {
|
||||||
// find closest thumb width
|
// find closest thumb width
|
||||||
let closest: u32 = find_closest_width(requested_width as u32);
|
let closest: u32 = find_closest_width(requested_width as u32);
|
||||||
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, extension);
|
let thumb_filename =
|
||||||
|
&format!("{}_{}.{}", base_filename, closest, extension);
|
||||||
return match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
|
|
||||||
|
return match check_file_exists(
|
||||||
|
&state.storj_client,
|
||||||
|
&state.bucket,
|
||||||
|
thumb_filename,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(true) => serve_file(thumb_filename, &state).await,
|
Ok(true) => serve_file(thumb_filename, &state).await,
|
||||||
Ok(false) => {
|
Ok(false) => {
|
||||||
if let Ok(filedata) = load_file_from_s3(
|
if let Ok(filedata) = load_file_from_s3(
|
||||||
&state.storj_client, &state.bucket, &stored_path).await {
|
&state.storj_client,
|
||||||
thumbdata_save(&filedata, &state, &base_filename, content_type).await;
|
&state.bucket,
|
||||||
serve_file(thumb_filename, &state).await
|
&stored_path,
|
||||||
} else {
|
)
|
||||||
Err(ErrorInternalServerError("cannot generate thumbnail"))
|
.await
|
||||||
}
|
{
|
||||||
|
thumbdata_save(
|
||||||
|
filedata.clone(),
|
||||||
|
&state,
|
||||||
|
&base_filename,
|
||||||
|
content_type.to_string(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
serve_file(thumb_filename, &state).await
|
||||||
|
} else {
|
||||||
|
Err(ErrorInternalServerError(
|
||||||
|
"cannot generate thumbnail",
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(ErrorInternalServerError("failed to load thumbnail"))
|
Err(_) => {
|
||||||
}
|
Err(ErrorInternalServerError("failed to load thumbnail"))
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
// not image passing thumb generation
|
// not image passing thumb generation
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to download what stored_path keeping in aws
|
// we need to download what stored_path keeping in aws
|
||||||
return match load_file_from_s3(
|
return match load_file_from_s3(&state.aws_client, &state.bucket, &stored_path).await
|
||||||
&state.aws_client,
|
{
|
||||||
&state.bucket,
|
Ok(filedata) => {
|
||||||
&stored_path).await {
|
let _ = upload_to_s3(
|
||||||
Ok(filedata) => {
|
&state.storj_client,
|
||||||
let _ = upload_to_s3(&state.storj_client, &state.bucket, &filekey, filedata.clone(), content_type).await;
|
&state.bucket,
|
||||||
thumbdata_save(&filedata, &state, &base_filename, content_type).await;
|
&filekey,
|
||||||
|
filedata.clone(),
|
||||||
Ok(HttpResponse::Ok()
|
content_type,
|
||||||
.content_type(content_type)
|
)
|
||||||
.body(filedata)
|
.await;
|
||||||
)
|
thumbdata_save(filedata.clone(), &state, &base_filename, content_type.to_string())
|
||||||
},
|
.await;
|
||||||
Err(err) => Err(ErrorInternalServerError(err)),
|
|
||||||
|
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
|
||||||
}
|
}
|
||||||
},
|
Err(err) => Err(ErrorInternalServerError(err)),
|
||||||
Ok(None) => Err(ErrorInternalServerError("requested file path was not found")),
|
};
|
||||||
Err(e) => Err(ErrorInternalServerError(e))
|
}
|
||||||
}
|
Ok(None) => Err(ErrorInternalServerError(
|
||||||
|
"requested file path was not found",
|
||||||
|
)),
|
||||||
|
Err(e) => Err(ErrorInternalServerError(e)),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(ErrorInternalServerError("invalid file key"))
|
Err(ErrorInternalServerError("invalid file key"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn thumbdata_save(original_data: &[u8], state: &AppState, original_filename: &str, content_type: &str) {
|
async fn thumbdata_save(
|
||||||
|
original_data: Vec<u8>,
|
||||||
|
state: &AppState,
|
||||||
|
original_filename: &str,
|
||||||
|
content_type: String,
|
||||||
|
) {
|
||||||
|
let state = state.clone();
|
||||||
if content_type.starts_with("image") {
|
if content_type.starts_with("image") {
|
||||||
let ext = original_filename.split('.').last().unwrap();
|
let filename = original_filename.to_string();
|
||||||
|
let ext = filename.split('.').last().unwrap().to_string();
|
||||||
let img = image::load_from_memory(&original_data).unwrap();
|
let img = image::load_from_memory(&original_data).unwrap();
|
||||||
|
actix::spawn(async move {
|
||||||
if let Ok(thumbnails_bytes) = generate_thumbnails(&img).await {
|
if let Ok(thumbnails_bytes) = generate_thumbnails(&img).await {
|
||||||
for (thumb_width, thumbnail) in thumbnails_bytes {
|
for (thumb_width, thumbnail) in thumbnails_bytes {
|
||||||
let thumb_filename = &format!("{}_{}.{}", original_filename, thumb_width, ext);
|
let thumb_filename = format!("{}_{}.{}", filename, thumb_width, ext);
|
||||||
let thumbnail_bytes = thumbnail.clone();
|
// Загружаем миниатюру в S3
|
||||||
// Загружаем миниатюру в S3
|
let _ = upload_to_s3(
|
||||||
let _ = upload_to_s3(
|
&state.storj_client,
|
||||||
&state.storj_client,
|
&state.bucket,
|
||||||
&state.bucket,
|
&thumb_filename,
|
||||||
thumb_filename,
|
thumbnail,
|
||||||
thumbnail_bytes,
|
&content_type,
|
||||||
content_type,
|
)
|
||||||
)
|
.await;
|
||||||
.await;
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user