feat: 添加download路由用于展示文档信息
All checks were successful
deploy to server / build-and-deploy (push) Successful in 5m4s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 5m4s
- 添加/download/documentID路由用于文档下载 - server端添加文档元数据获取与下载API - 将app中的types移至shared,与server共享
This commit is contained in:
22
server/api/download/[id].ts
Normal file
22
server/api/download/[id].ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { getFileMeta } from '../../utils/file';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, message: '缺少文件ID' });
|
||||
}
|
||||
const file = await getFileMeta(id);
|
||||
if (!file) {
|
||||
throw createError({ statusCode: 404, message: '文件不存在' });
|
||||
}
|
||||
|
||||
const res = await $fetch<ArrayBuffer>(file.url, {
|
||||
responseType: 'arrayBuffer',
|
||||
});
|
||||
return new Response(res, {
|
||||
headers: {
|
||||
'Content-Disposition': `attachment; filename="${encodeURIComponent(file.filename_download)}"`,
|
||||
'Content-Type': file.type,
|
||||
},
|
||||
});
|
||||
});
|
||||
24
server/api/file/[id].ts
Normal file
24
server/api/file/[id].ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { getFileMeta } from '../../utils/file';
|
||||
|
||||
/**
|
||||
* 用于处理文件相关的API请求
|
||||
* 返回指定ID的文件信息
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'File ID is required',
|
||||
});
|
||||
|
||||
const file = await getFileMeta(id);
|
||||
if (!file) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'File not found',
|
||||
});
|
||||
}
|
||||
|
||||
return file;
|
||||
});
|
||||
53
server/utils/file.ts
Normal file
53
server/utils/file.ts
Normal file
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 判断文件是否支持预览
|
||||
*/
|
||||
export function isPreviewable(mime: string | null | undefined): boolean {
|
||||
if (!mime || mime === undefined) return false;
|
||||
return (
|
||||
mime.startsWith('image/') ||
|
||||
mime.startsWith('video/') ||
|
||||
mime === 'application/pdf' ||
|
||||
mime.startsWith('text/')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Directus 获取文件元信息
|
||||
*/
|
||||
export async function getFileMeta(id: string): Promise<FileMeta | null> {
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
const baseUrl = runtimeConfig.public.directus.url;
|
||||
const access_token = runtimeConfig.public.directus.token;
|
||||
|
||||
try {
|
||||
const response = await $fetch<{ data: DirectusFile }>(
|
||||
`${baseUrl}/files/${id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: access_token ? `Bearer ${access_token}` : '',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const file = response.data;
|
||||
if (!file) return null;
|
||||
|
||||
return {
|
||||
id: file.id,
|
||||
title: file.filename_disk ?? '',
|
||||
filename_download: file.filename_download ?? '',
|
||||
type: file.type ?? '',
|
||||
filesize: Number(file.filesize),
|
||||
width: file.width ?? undefined,
|
||||
height: file.height ?? undefined,
|
||||
uploaded_on: file.uploaded_on ?? undefined,
|
||||
url: `${baseUrl}/assets/${file.id}`,
|
||||
previewable: isPreviewable(file.type),
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error('Error fetching file metadata:', error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user