better file previews

This commit is contained in:
2025-07-13 13:33:14 +02:00
parent 596d7ac35d
commit b0628fe0bd
9 changed files with 71 additions and 33 deletions

1
backend/Cargo.lock generated
View File

@@ -1960,6 +1960,7 @@ dependencies = [
"dotenv", "dotenv",
"env_logger", "env_logger",
"log", "log",
"mime_guess",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",

View File

@@ -8,6 +8,7 @@ axum = { version = "0.8.4", features = ["multipart", "query"] }
dotenv = "0.15.0" dotenv = "0.15.0"
env_logger = "0.11.8" env_logger = "0.11.8"
log = "0.4.27" log = "0.4.27"
mime_guess = "2.0.5"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140" serde_json = "1.0.140"
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio", "uuid"] } sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio", "uuid"] }

View File

@@ -5,7 +5,7 @@ pub use file::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum FileType { pub enum FileType {
File, File,
@@ -17,10 +17,22 @@ pub enum FileType {
pub struct DirectoryEntry { pub struct DirectoryEntry {
name: String, name: String,
file_type: FileType, file_type: FileType,
mime_type: Option<String>,
} }
impl DirectoryEntry { impl DirectoryEntry {
pub fn new(name: String, file_type: FileType) -> Self { pub fn new(name: String, file_type: FileType) -> Self {
Self { name, file_type } let mime_type = match name.split("/").last() {
Some(last) if last.contains(".") => {
mime_guess::from_path(&name).first_raw().map(str::to_owned)
}
_ => None,
};
Self {
name,
file_type,
mime_type,
}
} }
} }

View File

@@ -6,26 +6,22 @@ import {
ContextMenuItem, ContextMenuItem,
} from '@/components/ui/context-menu'; } from '@/components/ui/context-menu';
import { deleteWarrenEntry } from '~/lib/api/warrens'; import { deleteWarrenEntry } from '~/lib/api/warrens';
import type { FileType } from '~/types'; import type { DirectoryEntry } from '~/types';
const route = useRoute(); const route = useRoute();
const warrenRoute = useWarrenRoute(); const warrenRoute = useWarrenRoute();
const { name, fileType, disabled } = defineProps<{ const { entry, disabled } = defineProps<{
name: string; entry: DirectoryEntry;
fileType: FileType;
disabled: boolean; disabled: boolean;
}>(); }>();
const iconName = computed(() =>
fileType === 'file' ? 'lucide:file' : 'lucide:folder'
);
const deleting = ref(false); const deleting = ref(false);
async function submitDelete() { async function submitDelete() {
deleting.value = true; deleting.value = true;
await deleteWarrenEntry(warrenRoute.value, name, fileType); await deleteWarrenEntry(warrenRoute.value, entry.name, entry.fileType);
deleting.value = false; deleting.value = false;
} }
@@ -35,7 +31,7 @@ async function submitDelete() {
<ContextMenu> <ContextMenu>
<ContextMenuTrigger> <ContextMenuTrigger>
<NuxtLink <NuxtLink
:to="joinPaths(route.path, name)" :to="joinPaths(route.path, entry.name)"
:class="['select-none', { 'pointer-events-none': disabled }]" :class="['select-none', { 'pointer-events-none': disabled }]"
> >
<Button <Button
@@ -44,8 +40,8 @@ async function submitDelete() {
size="lg" size="lg"
:disabled="disabled" :disabled="disabled"
> >
<Icon :name="iconName" /> <Icon :name="getFileIcon(entry.mimeType)" />
<span class="truncate">{{ name }}</span> <span class="truncate">{{ entry.name }}</span>
</Button> </Button>
</NuxtLink> </NuxtLink>
</ContextMenuTrigger> </ContextMenuTrigger>

View File

@@ -18,8 +18,7 @@ const sortedEntries = computed(() =>
<DirectoryEntry <DirectoryEntry
v-for="entry in sortedEntries" v-for="entry in sortedEntries"
:key="entry.name" :key="entry.name"
:name="entry.name" :entry="entry"
:file-type="entry.fileType"
:disabled="isLoading" :disabled="isLoading"
/> />
</div> </div>

View File

@@ -216,14 +216,13 @@ async function submit() {
class="h-full w-full" class="h-full w-full"
> >
<div <div
class="flex h-full w-full flex-col items-stretch gap-1 text-left p-2" class="flex h-full w-full flex-col items-stretch gap-2 text-left p-4"
> >
<UploadListEntry <UploadListEntry
v-for="(file, i) in uploadStore.files" v-for="(file, i) in uploadStore.files"
:key="file.data.name" :key="file.data.name"
:file="file" :file="file"
:uploading="uploading" :uploading="uploading"
class="flex flex-row items-center justify-between gap-4 rounded-lg border px-4 py-2"
@remove-file="() => removeFile(i)" @remove-file="() => removeFile(i)"
/> />
</div> </div>

View File

@@ -8,18 +8,34 @@ const { file, uploading } = defineProps<{
file: UploadFile; file: UploadFile;
uploading: boolean; uploading: boolean;
}>(); }>();
function createObjectUrl(file: File): string {
return URL.createObjectURL(file);
}
</script> </script>
<template> <template>
<div <div
class="flex flex-row items-center justify-between gap-4 rounded-lg border px-4 py-2" class="flex border rounded-lg flex-row items-center justify-between gap-4 px-3 py-3"
> >
<div class="rounded-sm border p-3"> <div
<Icon :class="[
v-if="file.status === 'not_uploaded'" 'rounded-md border overflow-hidden w-12 h-12 min-w-12 min-h-12 items-center justify-center flex',
class="h-5 w-5" { 'p-2': !file.data.type.startsWith('image/') },
:name="getFileIcon(file.data.type)" ]"
/> >
<div v-if="file.status === 'not_uploaded'" class="w-full h-full">
<img
v-if="file.data.type.startsWith('image/')"
:src="createObjectUrl(file.data)"
class="h-full w-full object-cover"
/>
<Icon
v-else
class="h-5 w-5"
:name="getFileIcon(file.data.type)"
/>
</div>
<Icon <Icon
v-else-if="file.status === 'uploading'" v-else-if="file.status === 'uploading'"
class="h-5 w-5" class="h-5 w-5"

View File

@@ -8,6 +8,7 @@ export type BreadcrumbData = {
export type DirectoryEntry = { export type DirectoryEntry = {
name: string; name: string;
fileType: FileType; fileType: FileType;
mimeType: string | null;
}; };
export type UploadStatus = export type UploadStatus =

View File

@@ -1,28 +1,41 @@
export function getFileIcon(fileType: string) { export function getFileIcon(mimeType: string | null) {
if (fileType.startsWith('image/')) { if (mimeType == null) {
return 'lucide:folder';
}
if (mimeType.startsWith('image/')) {
return 'lucide:file-image'; return 'lucide:file-image';
} }
if (fileType.startsWith('video/')) { if (mimeType.startsWith('video/')) {
return 'lucide:file-video-2'; return 'lucide:file-video-2';
} }
if (fileType.startsWith('audio/')) { if (mimeType.startsWith('audio/')) {
return 'lucide:file-audio-2'; return 'lucide:file-audio-2';
} }
if (fileType.startsWith('application/')) { if (mimeType.startsWith('application/')) {
if (fileType === 'application/x-msdownload') { if (mimeType === 'application/x-msdownload') {
return 'lucide:file-box'; return 'lucide:file-box';
} }
if (fileType === 'application/json') { if (mimeType === 'application/json') {
return 'lucide:file-json'; return 'lucide:file-json';
} }
if (fileType === 'application/pdf') { if (mimeType === 'application/pdf') {
return 'lucide:file-text';
}
if (mimeType === 'application/zip') {
return 'lucide:file-archive';
}
if (
mimeType === 'application/epub+zip' ||
mimeType === 'application/x-mobipocket-ebook'
) {
return 'lucide:file-text'; return 'lucide:file-text';
} }
} }
if (fileType === 'text/html') { if (mimeType === 'text/html') {
return 'lucide:file-code'; return 'lucide:file-code';
} }