Sharable file

This commit is contained in:
2025-07-26 15:17:01 +08:00
parent 186e9c00aa
commit cf9903e500
8 changed files with 127 additions and 72 deletions

View File

@@ -8,7 +8,7 @@ pnpm-debug.log*
lerna-debug.log*
node_modules
node_modules/highlight.js
**/node_modules/highlight.js/
.DS_Store
dist
dist-ssr

View File

@@ -2,7 +2,7 @@
<html lang="">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Solar Network Drive</title>
<app-data />

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

View File

@@ -12,7 +12,8 @@
</n-alert>
</div>
<div v-else>
<n-image v-if="fileType === 'image'" :src="fileSource" />
<n-image v-if="fileType === 'image'" :src="fileSource" class="w-full" />
<video v-else-if="fileType === 'video'" :src="fileSource" controls class="w-full" />
</div>
</n-gi>
@@ -83,7 +84,25 @@
v-model:value="filePass"
type="password"
/>
<n-button @click="downloadFile">Download</n-button>
<div class="flex gap-2">
<n-button class="flex-grow-1" @click="downloadFile">Download</n-button>
<n-popover placement="bottom" trigger="hover">
<template #trigger>
<n-button>
<n-icon>
<qr-code-round />
</n-icon>
</n-button>
</template>
<n-qr-code
type="svg"
:value="currentUrl"
:size="160"
icon-src="/favicon.png"
error-correction-level="H"
/>
</n-popover>
</div>
</div>
<div v-else>
<n-progress processing :percentage="progress" />
@@ -109,9 +128,17 @@ import {
NCode,
NGrid,
NGi,
NPopover,
NQrCode,
useMessage,
} from 'naive-ui'
import { DataUsageRound, InfoRound, DetailsRound, FileUploadOutlined } from '@vicons/material'
import {
DataUsageRound,
InfoRound,
DetailsRound,
FileUploadOutlined,
QrCodeRound,
} from '@vicons/material'
import { useRoute } from 'vue-router'
import { computed, onMounted, ref } from 'vue'
@@ -135,6 +162,8 @@ const showTechDetails = ref<boolean>(false)
const messageDisplay = useMessage()
const currentUrl = window.location.href
const fileInfo = ref<any>(null)
async function fetchFileInfo() {
try {
@@ -161,10 +190,11 @@ function downloadFile() {
return
}
if (fileInfo.value.is_encrypted) {
downloadAndDecryptFile(fileSource.value, filePass.value, (p: number) => {
downloadAndDecryptFile(fileSource.value, filePass.value, fileInfo.value.name, (p: number) => {
progress.value = p * 100
}).catch((err) => {
messageDisplay.error('Download failed: ' + err.message, { closable: true, duration: 10000 })
progress.value = undefined
})
} else {
window.open(fileSource.value, '_blank')

View File

@@ -60,8 +60,11 @@
with-credentials
show-preview-button
list-type="image"
show-download-button
:custom-request="customRequest"
:custom-download="customDownload"
:create-thumbnail-url="createThumbnailUrl"
@preview="customPreview"
>
<n-upload-dragger>
<div style="margin-bottom: 12px">
@@ -102,11 +105,11 @@ import {
NSelect,
NTag,
NCollapseTransition,
NFormItem,
type UploadCustomRequestOptions,
type UploadSettledFileInfo,
type SelectOption,
type SelectRenderTag,
type UploadFileInfo,
} from 'naive-ui'
import { computed, h, onMounted, ref } from 'vue'
import { CloudUploadRound } from '@vicons/material'
@@ -283,4 +286,18 @@ function createThumbnailUrl(
if (!fileInfo) return undefined
return fileInfo.url ?? undefined
}
function customDownload(file: UploadFileInfo) {
const { url, name } = file
if (!url)
return
window.open(url.replace('/api', ''), '_blank')
}
function customPreview(file: UploadFileInfo, detail: { event: MouseEvent }) {
detail.event.preventDefault()
const { url, type } = file
if (!url) return
window.open(url.replace('/api', ''), '_blank')
}
</script>

View File

@@ -1,92 +1,94 @@
export async function downloadAndDecryptFile(
url: string,
password: string,
onProgress?: (progress: number) => void
fileName: string,
onProgress?: (progress: number) => void,
): Promise<void> {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch: ${response.status}`);
const response = await fetch(url)
if (!response.ok) throw new Error(`Failed to fetch: ${response.status}`)
const contentLength = +(response.headers.get('Content-Length') || 0);
const reader = response.body!.getReader();
const chunks: Uint8Array[] = [];
let received = 0;
const contentLength = +(response.headers.get('Content-Length') || 0)
const reader = response.body!.getReader()
const chunks: Uint8Array[] = []
let received = 0
while (true) {
const { done, value } = await reader.read();
if (done) break;
const { done, value } = await reader.read()
if (done) break
if (value) {
chunks.push(value);
received += value.length;
chunks.push(value)
received += value.length
if (contentLength && onProgress) {
onProgress(received / contentLength);
onProgress(received / contentLength)
}
}
}
const fullBuffer = new Uint8Array(received);
let offset = 0;
const fullBuffer = new Uint8Array(received)
let offset = 0
for (const chunk of chunks) {
fullBuffer.set(chunk, offset);
offset += chunk.length;
fullBuffer.set(chunk, offset)
offset += chunk.length
}
const decryptedBytes = await decryptFile(fullBuffer, password);
const decryptedBytes = await decryptFile(fullBuffer, password)
// Create a blob and trigger a download
const blob = new Blob([decryptedBytes]);
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = 'decrypted_file'; // You may allow customization
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(downloadUrl);
const blob = new Blob([decryptedBytes])
const downloadUrl = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = downloadUrl
a.download = fileName
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(downloadUrl)
}
export async function decryptFile(
fileBuffer: Uint8Array,
password: string
): Promise<Uint8Array> {
const salt = fileBuffer.slice(0, 16);
const nonce = fileBuffer.slice(16, 28);
const tag = fileBuffer.slice(28, 44);
const ciphertext = fileBuffer.slice(44);
export async function decryptFile(fileBuffer: Uint8Array, password: string): Promise<Uint8Array> {
const salt = fileBuffer.slice(0, 16)
const nonce = fileBuffer.slice(16, 28)
const tag = fileBuffer.slice(28, 44)
const ciphertext = fileBuffer.slice(44)
const enc = new TextEncoder();
const enc = new TextEncoder()
const keyMaterial = await crypto.subtle.importKey(
'raw', enc.encode(password), { name: 'PBKDF2' }, false, ['deriveKey']
);
'raw',
enc.encode(password),
{ name: 'PBKDF2' },
false,
['deriveKey'],
)
const key = await crypto.subtle.deriveKey(
{ name: 'PBKDF2', salt, iterations: 100000, hash: 'SHA-256' },
keyMaterial,
{ name: 'AES-GCM', length: 256 },
false,
['decrypt']
);
['decrypt'],
)
const fullCiphertext = new Uint8Array(ciphertext.length + tag.length);
fullCiphertext.set(ciphertext);
fullCiphertext.set(tag, ciphertext.length);
const fullCiphertext = new Uint8Array(ciphertext.length + tag.length)
fullCiphertext.set(ciphertext)
fullCiphertext.set(tag, ciphertext.length)
let decrypted: ArrayBuffer;
let decrypted: ArrayBuffer
try {
decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: nonce, tagLength: 128 },
key,
fullCiphertext
);
fullCiphertext,
)
} catch {
throw new Error("Incorrect password or corrupted file.");
throw new Error('Incorrect password or corrupted file.')
}
const magic = new TextEncoder().encode("DYSON1");
const decryptedBytes = new Uint8Array(decrypted);
const magic = new TextEncoder().encode('DYSON1')
const decryptedBytes = new Uint8Array(decrypted)
for (let i = 0; i < magic.length; i++) {
if (decryptedBytes[i] !== magic[i]) {
throw new Error("Incorrect password or corrupted file.");
throw new Error('Incorrect password or corrupted file.')
}
}
return decryptedBytes.slice(magic.length);
return decryptedBytes.slice(magic.length)
}