2026-03-04 14:02:15 +01:00
|
|
|
//! Nextcloud-compatible preview/thumbnail endpoint.
|
|
|
|
|
//!
|
|
|
|
|
//! Maps Nextcloud preview requests to OxiCloud's thumbnail service.
|
|
|
|
|
|
|
|
|
|
use axum::{
|
|
|
|
|
body::Body,
|
|
|
|
|
extract::{Query, State},
|
2026-07-20 13:48:47 +00:00
|
|
|
http::{StatusCode, header},
|
2026-03-04 14:02:15 +01:00
|
|
|
response::{IntoResponse, Response},
|
|
|
|
|
};
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2026-07-02 00:45:11 +02:00
|
|
|
use crate::application::ports::authorization_ports::AuthorizationEngine;
|
2026-03-04 14:02:15 +01:00
|
|
|
use crate::application::ports::file_ports::FileRetrievalUseCase;
|
|
|
|
|
use crate::application::ports::storage_ports::FileReadPort;
|
2026-06-21 21:16:08 +02:00
|
|
|
use crate::application::ports::thumbnail_ports::{ThumbnailFormat, ThumbnailPort, ThumbnailSize};
|
2026-03-04 14:02:15 +01:00
|
|
|
use crate::common::di::AppState;
|
2026-07-02 00:45:11 +02:00
|
|
|
use crate::domain::services::authorization::{Permission, Resource, Subject};
|
2026-03-09 00:08:34 +01:00
|
|
|
use crate::interfaces::middleware::auth::AuthUser;
|
2026-07-02 00:45:11 +02:00
|
|
|
use uuid::Uuid;
|
2026-03-04 14:02:15 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct PreviewParams {
|
|
|
|
|
#[serde(rename = "fileId")]
|
|
|
|
|
file_id: String,
|
|
|
|
|
x: Option<u32>,
|
|
|
|
|
y: Option<u32>,
|
|
|
|
|
#[serde(rename = "forceIcon")]
|
|
|
|
|
force_icon: Option<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle Nextcloud preview requests.
|
|
|
|
|
///
|
|
|
|
|
/// Maps:
|
|
|
|
|
/// - `/index.php/core/preview?fileId=X` to thumbnail generation
|
|
|
|
|
/// - Size selection based on request dimensions and forceIcon param
|
|
|
|
|
pub async fn handle_preview(
|
|
|
|
|
State(state): State<Arc<AppState>>,
|
2026-03-09 00:08:34 +01:00
|
|
|
user: AuthUser,
|
2026-03-04 14:02:15 +01:00
|
|
|
Query(params): Query<PreviewParams>,
|
2026-07-20 13:48:47 +00:00
|
|
|
req: axum::extract::Request,
|
2026-03-04 14:02:15 +01:00
|
|
|
) -> impl IntoResponse {
|
2026-03-05 20:42:30 -05:00
|
|
|
// Parse the Nextcloud file ID — the NC app may append an instance suffix
|
|
|
|
|
// (e.g. "00000326ocnca"), so strip non-digit characters first.
|
2026-07-19 17:30:22 +00:00
|
|
|
let digit_end = params
|
2026-03-05 21:07:19 -05:00
|
|
|
.file_id
|
2026-07-19 17:30:22 +00:00
|
|
|
.as_bytes()
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|b| !b.is_ascii_digit())
|
|
|
|
|
.unwrap_or(params.file_id.len());
|
|
|
|
|
let nc_file_id: i64 = match params.file_id[..digit_end].parse() {
|
2026-03-04 14:02:15 +01:00
|
|
|
Ok(id) => id,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::BAD_REQUEST)
|
|
|
|
|
.body(Body::from("Invalid file ID"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Look up the OxiCloud file UUID from the Nextcloud ID
|
|
|
|
|
let object_id = match state.nextcloud.as_ref() {
|
|
|
|
|
Some(nc) => match nc.file_ids.get_oxicloud_id(nc_file_id).await {
|
|
|
|
|
Ok(id) => id,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("File not found"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(Body::from("Nextcloud integration not configured"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Get file details
|
|
|
|
|
let file = match state
|
|
|
|
|
.applications
|
|
|
|
|
.file_retrieval_service
|
|
|
|
|
.get_file(&object_id)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("File not found"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-02 00:45:11 +02:00
|
|
|
// Verify the authenticated user can Read this file. Anti-enum: any
|
|
|
|
|
// AuthZ denial surfaces as 404 (same shape as "unknown file" above),
|
|
|
|
|
// and the engine emits an `authz.denied` audit line internally.
|
|
|
|
|
let file_uuid = match Uuid::parse_str(&file.id) {
|
|
|
|
|
Ok(u) => u,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("File not found"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
if state
|
|
|
|
|
.authorization
|
|
|
|
|
.require(
|
|
|
|
|
Subject::User(user.id),
|
|
|
|
|
Permission::Read,
|
|
|
|
|
Resource::File(file_uuid),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
2026-03-04 14:02:15 +01:00
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("File not found"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine thumbnail size based on request params
|
|
|
|
|
let thumb_size = if params.force_icon == Some(1) {
|
|
|
|
|
ThumbnailSize::Icon
|
|
|
|
|
} else {
|
|
|
|
|
// Map requested dimensions to our thumbnail sizes
|
|
|
|
|
let max_dim = params.x.unwrap_or(400).max(params.y.unwrap_or(400));
|
|
|
|
|
if max_dim <= 150 {
|
|
|
|
|
ThumbnailSize::Icon
|
|
|
|
|
} else if max_dim <= 400 {
|
|
|
|
|
ThumbnailSize::Preview
|
|
|
|
|
} else {
|
|
|
|
|
ThumbnailSize::Large
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-18 20:33:50 +00:00
|
|
|
// Conditional revalidation — the ETag is derived from (object id, size)
|
|
|
|
|
// only, so it is computable right here, BEFORE the blob-hash query and
|
|
|
|
|
// the thumbnail cache/disk read. NC clients revalidate gallery previews
|
|
|
|
|
// constantly; the REST thumbnail endpoint has honoured `If-None-Match`
|
|
|
|
|
// since PHOTOS-ETAG — this endpoint set an immutable ETag but never
|
|
|
|
|
// compared it, so every revalidation re-ran the whole pipeline and
|
|
|
|
|
// re-shipped the body (ROUND10). Authz already passed above; a 304
|
|
|
|
|
// must never skip the Read check.
|
2026-07-18 22:02:00 +00:00
|
|
|
let etag = {
|
|
|
|
|
let s = thumb_size.as_str();
|
|
|
|
|
let mut e = String::with_capacity(9 + object_id.len() + s.len());
|
|
|
|
|
e.push_str("\"thumb-");
|
|
|
|
|
e.push_str(&object_id);
|
|
|
|
|
e.push('-');
|
|
|
|
|
e.push_str(s);
|
|
|
|
|
e.push('"');
|
|
|
|
|
e
|
|
|
|
|
};
|
2026-07-20 13:48:47 +00:00
|
|
|
if let Some(inm) = req.headers().get(header::IF_NONE_MATCH)
|
2026-07-18 20:33:50 +00:00
|
|
|
&& let Ok(client_etag) = inm.to_str()
|
|
|
|
|
&& (client_etag == etag || client_etag == "*")
|
|
|
|
|
{
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_MODIFIED)
|
|
|
|
|
.header(header::CACHE_CONTROL, "public, max-age=31536000, immutable")
|
|
|
|
|
.header(header::ETAG, etag)
|
|
|
|
|
.body(Body::empty())
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 14:02:15 +01:00
|
|
|
// Check if file is an image
|
|
|
|
|
if !state
|
|
|
|
|
.core
|
|
|
|
|
.thumbnail_service
|
|
|
|
|
.is_supported_image(&file.mime_type)
|
|
|
|
|
{
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("Preview not available for this file type"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 11:55:05 +02:00
|
|
|
// Resolve the blob hash (content-addressable storage)
|
2026-03-04 14:02:15 +01:00
|
|
|
let blob_hash = match state
|
|
|
|
|
.repositories
|
|
|
|
|
.file_read_repository
|
|
|
|
|
.get_blob_hash(&object_id)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(hash) => hash,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("File blob not found"))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-04-26 11:55:05 +02:00
|
|
|
if let Some(data) = state
|
|
|
|
|
.core
|
|
|
|
|
.thumbnail_service
|
2026-06-21 21:16:08 +02:00
|
|
|
// NextCloud clients don't advertise WebP and expect JPEG — pin to JPEG
|
|
|
|
|
// (served from the shared lazy `.jpg` fallback).
|
|
|
|
|
.get_cached_thumbnail(
|
|
|
|
|
&object_id,
|
|
|
|
|
Some(&blob_hash),
|
|
|
|
|
thumb_size.into(),
|
|
|
|
|
ThumbnailFormat::Jpeg,
|
2026-08-23 23:10:00 +02:00
|
|
|
Some(&state.core.dedup_service),
|
2026-06-21 21:16:08 +02:00
|
|
|
)
|
2026-04-26 11:55:05 +02:00
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
return Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.header(header::CONTENT_TYPE, "image/jpeg")
|
|
|
|
|
.header(header::CONTENT_LENGTH, data.len())
|
|
|
|
|
.header(header::CACHE_CONTROL, "public, max-age=31536000, immutable")
|
|
|
|
|
.header(header::ETAG, etag)
|
|
|
|
|
.body(Body::from(data))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 13:36:29 +00:00
|
|
|
// Generate/get thumbnail — the blob is read inside the service once a
|
|
|
|
|
// decode permit is held, so preview stampedes cannot stack source
|
|
|
|
|
// images in RAM.
|
2026-03-04 14:02:15 +01:00
|
|
|
match state
|
|
|
|
|
.core
|
|
|
|
|
.thumbnail_service
|
2026-06-10 13:36:29 +00:00
|
|
|
.get_thumbnail_from_blob(
|
|
|
|
|
&object_id,
|
|
|
|
|
&blob_hash,
|
|
|
|
|
thumb_size.into(),
|
2026-06-21 21:16:08 +02:00
|
|
|
ThumbnailFormat::Jpeg,
|
2026-06-10 13:36:29 +00:00
|
|
|
state.core.dedup_service.clone(),
|
|
|
|
|
)
|
2026-03-04 14:02:15 +01:00
|
|
|
.await
|
|
|
|
|
{
|
2026-07-18 20:33:50 +00:00
|
|
|
Ok(data) => Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.header(header::CONTENT_TYPE, "image/jpeg")
|
|
|
|
|
.header(header::CONTENT_LENGTH, data.len())
|
|
|
|
|
.header(header::CACHE_CONTROL, "public, max-age=31536000, immutable")
|
|
|
|
|
.header(header::ETAG, etag)
|
|
|
|
|
.body(Body::from(data))
|
|
|
|
|
.unwrap(),
|
2026-03-04 14:02:15 +01:00
|
|
|
Err(err) => {
|
|
|
|
|
tracing::error!("Thumbnail generation failed for {}: {}", object_id, err);
|
|
|
|
|
Response::builder()
|
|
|
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(Body::from("Failed to generate thumbnail"))
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|