mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-17 07:28:46 +00:00
/api/file/[id]/thumbnail, /api/file/[id]/thumbnail/download, /api/file/[id]/thumbnail/upload Endpoint 구현
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
getAllFileCategories,
|
||||
type NewFile,
|
||||
} from "$lib/server/db/file";
|
||||
import { getFileThumbnail, updateFileThumbnail } from "$lib/server/db/media";
|
||||
import type { Ciphertext } from "$lib/server/db/schema";
|
||||
import env from "$lib/server/loadenv";
|
||||
|
||||
@@ -85,6 +86,59 @@ export const renameFile = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const getFileThumbnailInformation = async (userId: number, fileId: number) => {
|
||||
const thumbnail = await getFileThumbnail(userId, fileId);
|
||||
if (!thumbnail) {
|
||||
error(404, "File or its thumbnail not found");
|
||||
}
|
||||
|
||||
return { encContentIv: thumbnail.encContentIv };
|
||||
};
|
||||
|
||||
export const getFileThumbnailStream = async (userId: number, fileId: number) => {
|
||||
const thumbnail = await getFileThumbnail(userId, fileId);
|
||||
if (!thumbnail) {
|
||||
error(404, "File or its thumbnail not found");
|
||||
}
|
||||
|
||||
const { size } = await stat(thumbnail.path);
|
||||
return {
|
||||
encContentStream: Readable.toWeb(createReadStream(thumbnail.path)),
|
||||
encContentSize: size,
|
||||
};
|
||||
};
|
||||
|
||||
export const uploadFileThumbnail = async (
|
||||
userId: number,
|
||||
fileId: number,
|
||||
dekVersion: Date,
|
||||
encContentIv: string,
|
||||
encContentStream: Readable,
|
||||
) => {
|
||||
const path = `${env.thumbnailsPath}/${userId}/${uuidv4()}`;
|
||||
await mkdir(dirname(path), { recursive: true });
|
||||
|
||||
try {
|
||||
await pipeline(encContentStream, createWriteStream(path, { flags: "wx", mode: 0o600 }));
|
||||
|
||||
const oldPath = await updateFileThumbnail(userId, fileId, dekVersion, path, encContentIv);
|
||||
if (oldPath) {
|
||||
safeUnlink(oldPath); // Intended
|
||||
}
|
||||
} catch (e) {
|
||||
await safeUnlink(path);
|
||||
|
||||
if (e instanceof IntegrityError) {
|
||||
if (e.message === "File not found") {
|
||||
error(404, "File not found");
|
||||
} else if (e.message === "Invalid DEK version") {
|
||||
error(400, "Mismatched DEK version");
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
export const scanDuplicateFiles = async (
|
||||
userId: number,
|
||||
hskVersion: number,
|
||||
|
||||
Reference in New Issue
Block a user