♻️ Refactored attachment page

This commit is contained in:
2025-11-06 01:33:49 +08:00
parent 6da1a4d21c
commit 9f82214797
8 changed files with 264 additions and 151 deletions

View File

@@ -24,6 +24,7 @@
@click="openExternally" @click="openExternally"
/> />
</template> </template>
<audio <audio
v-else-if="itemType == 'audio'" v-else-if="itemType == 'audio'"
class="w-full h-auto" class="w-full h-auto"
@@ -59,8 +60,10 @@ const aspectRatio = computed(
) )
const imageLoaded = ref(false) const imageLoaded = ref(false)
const router = useRouter()
function openExternally() { function openExternally() {
window.open(remoteSource.value + "?original=true", "_blank") router.push('/files/' + props.item.id)
} }
const blurCanvas = ref<HTMLCanvasElement | null>(null) const blurCanvas = ref<HTMLCanvasElement | null>(null)

View File

@@ -28,7 +28,7 @@
class="me-4" class="me-4"
color="grey-darken-1" color="grey-darken-1"
size="32" size="32"
icon="mdi-account-circle-outline" icon="mdi-account-circle"
:image=" :image="
user?.profile.picture user?.profile.picture
? `${apiBase}/drive/files/${user?.profile.picture?.id}` ? `${apiBase}/drive/files/${user?.profile.picture?.id}`

View File

@@ -1,137 +1,138 @@
<template> <template>
<div class="d-flex align-center justify-center fill-height"> <div class="lightbox-container">
<v-card class="pa-6" max-width="1200" width="100%"> <!-- Top Toolbar -->
<v-app-bar
v-if="fileInfo"
class="top-toolbar"
flat
height="56"
color="rgba(0,0,0,0.7)"
dark
>
<v-container fluid>
<v-row align="center" class="pa-2">
<v-col cols="12" md="4">
<div class="d-flex align-center gap-2">
<v-icon>mdi-file</v-icon>
<span>{{ fileInfo.name || "File" }}</span>
</div>
</v-col>
<v-col cols="12" md="8">
<div class="d-flex align-center justify-end gap-4">
<span>{{ fileInfo.mimeType }} ({{ fileType }})</span>
<span>{{ formatBytes(fileInfo.size) }}</span>
<span>{{ new Date(fileInfo.createdAt).toLocaleString() }}</span>
<v-btn icon size="small" density="compact" @click="handleDownload">
<v-icon>mdi-download</v-icon>
</v-btn>
<v-btn icon size="small" density="compact" @click="infoDialog = true">
<v-icon>mdi-information</v-icon>
</v-btn>
</div>
</v-col>
</v-row>
</v-container>
</v-app-bar>
<!-- Main Content - File Preview -->
<div class="preview-container">
<v-progress-circular <v-progress-circular
v-if="!fileInfo && !error" v-if="!fileInfo && !error"
indeterminate indeterminate
size="32" size="64"
class="loading-spinner"
></v-progress-circular> ></v-progress-circular>
<v-alert <v-alert
v-else-if="error"
type="error" type="error"
title="No file was found" title="No file was found"
:text="error" :text="error"
v-else-if="error" class="error-alert"
></v-alert> ></v-alert>
<div v-else> <div v-else class="preview-content">
<v-row> <div v-if="fileInfo?.isEncrypted" class="encrypted-notice">
<v-col cols="12" md="6">
<div v-if="fileInfo.isEncrypted">
<v-alert type="info" title="Encrypted file" class="mb-4"> <v-alert type="info" title="Encrypted file" class="mb-4">
The file has been encrypted. Preview not available. Please enter The file has been encrypted. Preview not available. Please enter the
the password to download it. password to download it.
</v-alert> </v-alert>
</div> </div>
<div v-else> <div v-else class="file-preview">
<v-img <v-img
v-if="fileType === 'image'" v-if="fileType === 'image'"
:src="fileSource" :src="fileSource"
class="w-full" class="preview-image"
contain
/> />
<video <video
v-else-if="fileType === 'video'" v-else-if="fileType === 'video'"
:src="fileSource" :src="fileSource"
controls controls
class="w-full" class="preview-video"
/> />
<audio <audio
v-else-if="fileType === 'audio'" v-else-if="fileType === 'audio'"
:src="fileSource" :src="fileSource"
controls controls
class="w-full" class="preview-audio"
/> />
<v-alert <v-alert
v-else
type="warning" type="warning"
title="Preview Unavailable" title="Preview Unavailable"
text="How can you preview this file?" text="How can you preview this file?"
v-else class="preview-unavailable"
/> />
</div> </div>
</v-col> </div>
</div>
<v-col cols="12" md="6"> <!-- Password Dialog -->
<div class="mb-3"> <v-dialog v-model="secretDialog" max-width="400">
<v-card <v-card>
title="File Information" <v-card-title>
prepend-icon="mdi-information-outline" <v-icon left>mdi-lock</v-icon>
variant="tonal" Enter Password
> </v-card-title>
<v-card-text> <v-card-text>
<div class="d-flex gap-2 mb-2">
<span class="flex-grow-1 d-flex align-center gap-2">
<v-icon size="18">mdi-information</v-icon>
File Type
</span>
<span>{{ fileInfo.mimeType }} ({{ fileType }})</span>
</div>
<div class="d-flex gap-2 mb-2">
<span class="flex-grow-1 d-flex align-center gap-2">
<v-icon size="18">mdi-chart-pie</v-icon>
File Size
</span>
<span>{{ formatBytes(fileInfo.size) }}</span>
</div>
<div class="d-flex gap-2 mb-2">
<span class="flex-grow-1 d-flex align-center gap-2">
<v-icon size="18">mdi-upload</v-icon>
Uploaded At
</span>
<span>{{
new Date(fileInfo.createdAt).toLocaleString()
}}</span>
</div>
<div class="d-flex gap-2 mb-2">
<span class="flex-grow-1 d-flex align-center gap-2">
<v-icon size="18">mdi-details</v-icon>
Technical Info
</span>
<v-btn
text
size="x-small"
@click="showTechDetails = !showTechDetails"
>
{{ showTechDetails ? "Hide" : "Show" }}
</v-btn>
</div>
<v-expand-transition>
<div
v-if="showTechDetails"
class="mt-2 d-flex flex-column gap-1"
>
<p class="text-caption opacity-75">#{{ fileInfo.id }}</p>
<v-card class="pa-2" variant="outlined">
<pre
class="overflow-x-auto px-2 py-1"
><code>{{ JSON.stringify(fileInfo.fileMeta, null, 4) }}</code></pre>
</v-card>
</div>
</v-expand-transition>
</v-card-text>
</v-card>
</div>
<div class="d-flex flex-column gap-3">
<v-text-field <v-text-field
v-if="fileInfo.isEncrypted" v-model="dialogPassword"
label="Password" label="Password"
v-model="filePass"
type="password" type="password"
variant="outlined"
autofocus
@keyup.enter="confirmDownload"
/> />
<v-btn class="flex-grow-1" @click="downloadFile">Download</v-btn> </v-card-text>
</div> <v-card-actions>
<v-expand-transition> <v-spacer></v-spacer>
<v-progress-linear <v-btn @click="secretDialog = false">Cancel</v-btn>
v-if="!!progress" <v-btn color="primary" @click="confirmDownload">Download</v-btn>
:model-value="progress" </v-card-actions>
:indeterminate="progress < 100"
class="mt-4"
/>
</v-expand-transition>
</v-col>
</v-row>
</div>
</v-card> </v-card>
</v-dialog>
<!-- Technical Details Dialog -->
<v-dialog v-model="infoDialog" max-width="640">
<v-card title="File Information" prepend-icon="mdi-information">
<v-card-text>
<div class="mb-4">
<b>FID</b> <span class="font-mono">#{{ fileInfo?.id }}</span>
</div>
<div class="mb-4">
<strong>Metadata:</strong>
</div>
<v-card variant="outlined" class="pa-2">
<pre
class="overflow-x-auto text-body-2"
><code>{{ JSON.stringify(fileInfo?.fileMeta, null, 2) }}</code></pre>
</v-card>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn @click="infoDialog = false">Close</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div> </div>
</template> </template>
@@ -141,29 +142,35 @@ import { computed, onMounted, ref } from "vue"
import { downloadAndDecryptFile } from "./secure" import { downloadAndDecryptFile } from "./secure"
import { formatBytes } from "./format" import { formatBytes } from "./format"
import type { SnCloudFile } from "~/types/api/post"
useHead({
title: computed(() => fileInfo.value?.name ? `${fileInfo.value.name} - File Preview` : 'File Preview')
})
const route = useRoute() const route = useRoute()
const error = ref<string | null>(null) const error = ref<string | null>(null)
const filePass = ref<string>("")
const fileId = route.params.id const fileId = route.params.id
const passcode = route.query.passcode as string | undefined const passcode = route.query.passcode as string | undefined
const progress = ref<number | undefined>(0) const progress = ref<number | undefined>(0)
const showTechDetails = ref<boolean>(false) const infoDialog = ref<boolean>(false)
const secretDialog = ref<boolean>(false)
const dialogPassword = ref<string>("")
const api = useSolarNetwork() const api = useSolarNetwork()
const fileInfo = ref<any>(null) const fileInfo = ref<SnCloudFile | null>(null)
async function fetchFileInfo() { async function fetchFileInfo() {
try { try {
let url = "/drive/files/" + fileId + "/info" let url = "/drive/files/" + fileId + "/info"
if (passcode) { if (passcode) {
url += `?passcode=${passcode}` url += `?passcode=${passcode}`
} }
const resp = await api(url) const resp = await api<SnCloudFile>(url)
fileInfo.value = resp fileInfo.value = resp
} catch (err) { } catch (err) {
error.value = (err as Error).message error.value = (err as Error).message
@@ -171,34 +178,47 @@ async function fetchFileInfo() {
} }
onMounted(() => fetchFileInfo()) onMounted(() => fetchFileInfo())
const apiBase = useSolarNetworkUrl(false) const apiBase = useSolarNetworkUrl()
const fileType = computed(() => { const fileType = computed(() => {
if (!fileInfo.value) return "unknown" if (!fileInfo.value) return "unknown"
return fileInfo.value.mimeType?.split("/")[0] || "unknown" return fileInfo.value.mimeType?.split("/")[0] || "unknown"
}) })
const fileSource = computed(() => { const fileSource = computed(() => {
let url = `${apiBase}/drive/files/${fileId}` let url = `${apiBase}/drive/files/${fileId}?original=true`
if (passcode) { if (passcode) {
url += `?passcode=${passcode}` url += `?passcode=${passcode}`
} }
return url return url
}) })
async function downloadFile() { function handleDownload() {
if (fileInfo.value.isEncrypted && !filePass.value) { if (fileInfo.value!.isEncrypted) {
alert("Please enter the password to download the file.") secretDialog.value = true
dialogPassword.value = ""
} else {
performDownload("")
}
}
async function confirmDownload() {
if (!dialogPassword.value.trim()) {
return return
} }
if (fileInfo.value.isEncrypted) { secretDialog.value = false
await performDownload(dialogPassword.value)
}
async function performDownload(password: string) {
if (fileInfo.value!.isEncrypted) {
downloadAndDecryptFile( downloadAndDecryptFile(
fileSource.value, fileSource.value,
filePass.value, password,
fileInfo.value.name, fileInfo.value!.name,
(p: number) => { (p: number) => {
progress.value = p * 100 progress.value = p * 100
} }
).catch((err: any) => { ).catch((err: Error) => {
alert("Download failed: " + err.message) alert("Download failed: " + err.message)
progress.value = undefined progress.value = undefined
}) })
@@ -206,7 +226,7 @@ async function downloadFile() {
const res = await fetch(fileSource.value) const res = await fetch(fileSource.value)
if (!res.ok) { if (!res.ok) {
throw new Error( throw new Error(
`Failed to download ${fileInfo.value.name}: ${res.statusText}` `Failed to download ${fileInfo.value!.name}: ${res.statusText}`
) )
} }
@@ -235,12 +255,98 @@ async function downloadFile() {
const a = document.createElement("a") const a = document.createElement("a")
a.href = blobUrl a.href = blobUrl
a.download = a.download =
fileInfo.value.fileName || fileInfo.value!.name ||
"download." + fileInfo.value.mimeType.split("/")[1] "download." + fileInfo.value!.mimeType.split("/")[1]
document.body.appendChild(a) document.body.appendChild(a)
a.click() a.click()
a.remove() a.remove()
window.URL.revokeObjectURL(blobUrl) window.URL.revokeObjectURL(blobUrl)
} }
} }
definePageMeta({
layout: "minimal"
})
</script> </script>
<style scoped>
.lightbox-container {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.9);
display: flex;
flex-direction: column;
z-index: 1000;
}
.top-toolbar {
position: relative;
z-index: 1001;
backdrop-filter: blur(10px);
}
.preview-container {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
overflow: auto;
}
.preview-content {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.file-preview {
width: 100%;
height: 100%;
max-width: calc(100vw - 40px);
max-height: calc(
100vh - 88px
); /* Account for top toolbar (48px) + padding (40px) */
display: flex;
align-items: center;
justify-content: center;
}
.preview-image {
width: 100%;
height: 100%;
object-fit: contain;
}
.preview-video,
.preview-audio {
max-width: 100%;
max-height: 100%;
}
.preview-unavailable {
max-width: 500px;
}
.loading-spinner {
color: white;
}
.error-alert {
max-width: 500px;
}
.encrypted-notice {
max-width: 500px;
}
/* Ensure toolbar doesn't interfere with content */
.top-toolbar :deep(.v-app-bar__content) {
padding: 0;
}
</style>

View File

@@ -61,7 +61,8 @@
<v-card class="pa-6"> <v-card class="pa-6">
<article <article
v-if="htmlContent" v-if="htmlContent"
class="prose prose-xl dark:prose-invert prose-slate max-w-none mb-8" class="prose dark:prose-invert prose-slate max-w-none mb-8"
:class="classesContent"
> >
<div v-html="htmlContent" /> <div v-html="htmlContent" />
</article> </article>
@@ -165,6 +166,8 @@ const {
const post = computed(() => postData.value?.post || null) const post = computed(() => postData.value?.post || null)
const htmlContent = computed(() => postData.value?.html || "") const htmlContent = computed(() => postData.value?.html || "")
const classesContent = computed(() => postData.value?.post.type == 1 ? 'prose-xl' : 'prose-md');
useHead({ useHead({
title: computed(() => { title: computed(() => {
if (pending.value) return "Loading post..." if (pending.value) return "Loading post..."

View File

@@ -1,5 +1,5 @@
// Re-export all types from separate files for easy importing // Re-export all types from separate files for easy importing
export type { SnFileMeta, SnAttachment, SnPost } from './post' export type { SnFileMeta, SnCloudFile as SnAttachment, SnPost } from './post'
export type { SnVerification, SnPublisher } from './publisher' export type { SnVerification, SnPublisher } from './publisher'
export type { SnActivity } from './activity' export type { SnActivity } from './activity'
export type { SnWalletOrder, OrderStatus } from './order' export type { SnWalletOrder, OrderStatus } from './order'

View File

@@ -23,7 +23,7 @@ export interface SnFileMeta {
} }
// Attachment interface // Attachment interface
export interface SnAttachment { export interface SnCloudFile {
id: string; id: string;
name: string; name: string;
fileMeta: SnFileMeta; fileMeta: SnFileMeta;
@@ -33,6 +33,7 @@ export interface SnAttachment {
hash: string; hash: string;
size: number; size: number;
hasCompression: boolean; hasCompression: boolean;
isEncrypted: boolean;
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
deletedAt: string | null; deletedAt: string | null;
@@ -69,7 +70,7 @@ export interface SnPost {
forwardedPost: SnPost | null; forwardedPost: SnPost | null;
realmId: string | null; realmId: string | null;
realm: unknown | null; realm: unknown | null;
attachments: SnAttachment[]; attachments: SnCloudFile[];
publisherId: string; publisherId: string;
publisher: SnPublisher; publisher: SnPublisher;
awards: unknown | null; awards: unknown | null;

View File

@@ -1,4 +1,4 @@
import type { SnAttachment } from './post' import type { SnCloudFile } from './post'
// Verification interface // Verification interface
export interface SnVerification { export interface SnVerification {
@@ -17,8 +17,8 @@ export interface SnPublisher {
bio: string; bio: string;
pictureId: string; pictureId: string;
backgroundId: string; backgroundId: string;
picture: SnAttachment | null; picture: SnCloudFile | null;
background: SnAttachment | null; background: SnCloudFile | null;
verification: SnVerification | null; verification: SnVerification | null;
accountId: string; accountId: string;
realmId: string | null; realmId: string | null;

View File

@@ -1,4 +1,4 @@
import type { SnAttachment } from './post' import type { SnCloudFile } from './post'
import type { SnVerification } from './publisher' import type { SnVerification } from './publisher'
// Account link interface // Account link interface
@@ -63,8 +63,8 @@ export interface SnAccountProfile {
socialCredits: number; socialCredits: number;
socialCreditsLevel: number; socialCreditsLevel: number;
levelingProgress: number; levelingProgress: number;
picture: SnAttachment | null; picture: SnCloudFile | null;
background: SnAttachment | null; background: SnCloudFile | null;
accountId: string; accountId: string;
resourceIdentifier: string; resourceIdentifier: string;
createdAt: string; createdAt: string;