show PDFs in BlobPreview, add ?inline query param to /raw/

feat/vaults
Tomáš Mládek 2022-01-27 15:55:48 +01:00
parent 538ccc27ec
commit 19b25666c6
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
2 changed files with 26 additions and 7 deletions

View File

@ -11,6 +11,7 @@ use actix_files::NamedFile;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound}; use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound};
use actix_web::http; use actix_web::http;
use actix_web::http::header::{ContentDisposition, DispositionType};
use actix_web::{delete, error, get, post, put, web, Either, Error, HttpResponse}; use actix_web::{delete, error, get, post, put, web, Either, Error, HttpResponse};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use futures_util::TryStreamExt; use futures_util::TryStreamExt;
@ -42,6 +43,7 @@ pub struct State {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct RawRequest { pub struct RawRequest {
native: Option<String>, native: Option<String>,
inline: Option<String>,
} }
#[get("/api/raw/{hash}")] #[get("/api/raw/{hash}")]
@ -50,8 +52,9 @@ pub async fn get_raw(
web::Query(query): web::Query<RawRequest>, web::Query(query): web::Query<RawRequest>,
hash: web::Path<String>, hash: web::Path<String>,
) -> Result<Either<NamedFile, HttpResponse>, Error> { ) -> Result<Either<NamedFile, HttpResponse>, Error> {
let address = Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?) let address =
.map_err(ErrorInternalServerError)?; Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
.map_err(ErrorInternalServerError)?;
if let Address::Hash(hash) = address { if let Address::Hash(hash) = address {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?; let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
@ -71,7 +74,14 @@ pub async fn get_raw(
let file_path = state.upend.vault_path.join(&file.path); let file_path = state.upend.vault_path.join(&file.path);
if query.native.is_none() { if query.native.is_none() {
Ok(Either::A(NamedFile::open(file_path)?)) Ok(Either::A(if query.inline.is_some() {
NamedFile::open(file_path)?.set_content_disposition(ContentDisposition {
disposition: DispositionType::Inline,
parameters: vec![],
})
} else {
NamedFile::open(file_path)?
}))
} else if state.desktop_enabled { } else if state.desktop_enabled {
#[cfg(feature = "desktop")] #[cfg(feature = "desktop")]
{ {
@ -370,8 +380,9 @@ pub async fn get_file(
state: web::Data<State>, state: web::Data<State>,
hash: web::Path<String>, hash: web::Path<String>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let address = Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?) let address =
.map_err(ErrorInternalServerError)?; Address::decode(&b58_decode(hash.into_inner()).map_err(ErrorInternalServerError)?)
.map_err(ErrorInternalServerError)?;
if let Address::Hash(hash) = address { if let Address::Hash(hash) = address {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?; let connection = state.upend.connection().map_err(ErrorInternalServerError)?;

View File

@ -10,8 +10,8 @@
$: mimeType = String($entity?.get("FILE_MIME")); $: mimeType = String($entity?.get("FILE_MIME"));
$: handled = $: handled =
Boolean(mimeType) && Boolean(mimeType) &&
["audio", "video", "image", "model", "text"].some((prefix) => ["audio", "video", "image", "model", "text", "application/pdf"].some(
mimeType.startsWith(prefix) (prefix) => mimeType.startsWith(prefix)
); );
let imageLoaded = null; let imageLoaded = null;
@ -48,6 +48,9 @@
/> />
</a> </a>
{/if} {/if}
{#if mimeType == "application/pdf"}
<iframe src="/api/raw/{address}?inline" title="PDF document of {address}"/>
{/if}
{#if mimeType?.startsWith("model")} {#if mimeType?.startsWith("model")}
<ModelViewer src="/api/raw/{address}" /> <ModelViewer src="/api/raw/{address}" />
{/if} {/if}
@ -69,6 +72,11 @@
max-height: 25em; max-height: 25em;
} }
iframe {
width: 100%;
min-height: 25em;
}
.text { .text {
display: flex; display: flex;
margin-bottom: 1rem; margin-bottom: 1rem;