upend/webui/src/components/display/BlobPreview.svelte

105 lines
2.6 KiB
Svelte

<script lang="ts">
import { useEntity } from "../../lib/entity";
import Spinner from "../utils/Spinner.svelte";
import FragmentViewer from "./blobs/FragmentViewer.svelte";
import ModelViewer from "./blobs/ModelViewer.svelte";
import VideoViewer from "./blobs/VideoViewer.svelte";
import HashBadge from "./HashBadge.svelte";
export let address: string;
$: ({ entity, entityInfo } = useEntity(address));
$: mimeType = String($entity?.get("FILE_MIME"));
$: audio = ["audio", "application/x-riff"].some((p) =>
mimeType.startsWith(p)
);
$: video = ["video", "application/x-matroska"].some((p) =>
mimeType.startsWith(p)
);
$: image = mimeType.startsWith("image");
$: text = mimeType.startsWith("text");
$: pdf = mimeType.startsWith("application/pdf");
$: model =
mimeType?.startsWith("model") ||
$entity?.identify().some((l) => l.endsWith(".stl"));
$: web = $entityInfo?.t == "Url";
$: fragment = Boolean($entity?.get("ANNOTATES"));
$: handled =
audio || video || image || text || pdf || model || web || fragment;
let imageLoaded = null;
</script>
{#if handled}
<div class="preview">
{#if model}
<ModelViewer lookonly src="api/raw/{address}" />
{:else if web}
{#if imageLoaded != address}
<Spinner />
{/if}
<img
src={String($entity?.get("OG_IMAGE"))}
alt="OpenGraph image for {$entityInfo?.t == 'Url' && $entityInfo?.c}"
on:load={() => (imageLoaded = address)}
on:error={() => (handled = false)}
/>
{:else if fragment}
<FragmentViewer {address} detail={false} />
{:else if video}
<VideoViewer {address} detail={false} />
{:else}
<div class="image" class:loaded={imageLoaded == address || !handled}>
{#if handled && imageLoaded != address}
<div class="spinner">
<Spinner centered />
</div>
{/if}
<img
src="api/thumb/{address}"
alt="Thumbnail for {address}..."
on:load={() => (imageLoaded = address)}
on:error={() => (handled = false)}
/>
</div>
{/if}
</div>
{:else}
<div class="hashbadge">
<HashBadge {address} />
</div>
{/if}
<style lang="scss">
.preview {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
max-height: 25em;
}
.image {
display: flex;
min-height: 0;
justify-content: center;
padding: 0.5em;
img {
max-width: 100%;
object-fit: contain;
}
}
.hashbadge {
font-size: 48px;
opacity: 0.25;
text-align: center;
}
</style>