mirror of
https://github.com/Nezumi-2711/next-gdrive-index.git
synced 2026-09-23 04:10:03 +00:00
Update file preview
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
|
||||
import Cors, { CorsRequest } from "cors";
|
||||
import { ErrorResponse } from "@/types/googleapis";
|
||||
|
||||
function _initMiddleware(middleware: CorsRequest | any) {
|
||||
return (req: NextApiRequest, res: NextApiResponse) =>
|
||||
new Promise((resolve, reject) => {
|
||||
middleware(req, res, (result: any) => {
|
||||
if (result instanceof Error) {
|
||||
return reject(result);
|
||||
}
|
||||
return resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
const initMiddleware =
|
||||
(handler: NextApiHandler) =>
|
||||
async (request: NextApiRequest, response: NextApiResponse) => {
|
||||
try {
|
||||
const allowedMethods = ["GET"];
|
||||
const initCORS = _initMiddleware(
|
||||
Cors({
|
||||
methods: allowedMethods,
|
||||
}),
|
||||
);
|
||||
await initCORS(request, response);
|
||||
|
||||
return handler(request, response);
|
||||
} catch (error: any) {
|
||||
const payload: ErrorResponse = {
|
||||
success: false,
|
||||
timestamp: new Date().toISOString(),
|
||||
code: error.code || 500,
|
||||
errors: {
|
||||
message: error.message || "Internal Server Error",
|
||||
reason: error.reason || "internalError",
|
||||
},
|
||||
};
|
||||
return response.status(error.code || 500).json(payload);
|
||||
}
|
||||
};
|
||||
|
||||
export default initMiddleware;
|
||||
@@ -29,6 +29,11 @@ import CodePreview from "@components/FilePreview/CodePreview";
|
||||
import TextPreview from "@components/FilePreview/TextPreview";
|
||||
import VideoPreview from "@components/FilePreview/VideoPreview";
|
||||
|
||||
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
|
||||
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
|
||||
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
|
||||
import { STLLoader } from "three/examples/jsm/loaders/STLLoader";
|
||||
|
||||
function findMimeType(extension: string): string {
|
||||
return mime.lookup(extension) || "application/octet-stream";
|
||||
}
|
||||
@@ -37,7 +42,7 @@ const type = {
|
||||
"3d": "3d", // model preview
|
||||
"audio": "audio", // audio preview
|
||||
"archive": "archive", // default preview
|
||||
"rich_text": "markdown", // markdown preview
|
||||
"rich_text": "rich_text", // markdown preview
|
||||
"officeWord": "officeWord", // office preview
|
||||
"officeExcel": "officeExcel", // office preview
|
||||
"officePowerPoint": "officePowerPoint", // office preview
|
||||
@@ -163,6 +168,8 @@ const extToTypeMap: { [key: string]: string } = {
|
||||
"tsx": type.code,
|
||||
"xml": type.code,
|
||||
"yaml": type.code,
|
||||
"vue": type.code,
|
||||
"toml": type.code,
|
||||
|
||||
// Text
|
||||
"txt": type.text,
|
||||
@@ -189,17 +196,17 @@ const extToTypeMap: { [key: string]: string } = {
|
||||
"msi": type.binary,
|
||||
};
|
||||
|
||||
export function getFilePreview(extension: string) {
|
||||
export function getFilePreview(extension: string, mimeType?: string) {
|
||||
if (overlapVideo.includes(extension)) {
|
||||
const isVideo = findMimeType(extension).startsWith("video");
|
||||
const isVideo = !!mimeType?.startsWith("video");
|
||||
if (isVideo) {
|
||||
return VideoPreview;
|
||||
}
|
||||
}
|
||||
const category = extToTypeMap[extension] || type.default;
|
||||
switch (category) {
|
||||
case type["3d"]:
|
||||
return ModelPreview;
|
||||
// case type["3d"]:
|
||||
// return ModelPreview;
|
||||
case type.audio:
|
||||
return AudioPreview;
|
||||
case type.rich_text:
|
||||
@@ -223,9 +230,9 @@ export function getFilePreview(extension: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getFileIcon(extension: string): IconType {
|
||||
export function getFileIcon(extension: string, mimeType?: string): IconType {
|
||||
if (overlapVideo.includes(extension)) {
|
||||
const isVideo = findMimeType(extension).startsWith("video");
|
||||
const isVideo = !!mimeType?.startsWith("video");
|
||||
if (isVideo) {
|
||||
return iconsForType["video"];
|
||||
}
|
||||
@@ -233,3 +240,47 @@ export function getFileIcon(extension: string): IconType {
|
||||
const category = extToTypeMap[extension] || type.default;
|
||||
return iconsForType[category];
|
||||
}
|
||||
|
||||
// Not all extensions are included here
|
||||
// Taken from onedrive-vercel-index by SpencerWoo
|
||||
// https://github.com/spencerwooo/onedrive-vercel-index/blob/main/src/utils/getPreviewType.ts
|
||||
export function getCodeLanguage(extension: string) {
|
||||
switch (extension) {
|
||||
case "ts":
|
||||
case "tsx":
|
||||
return "typescript";
|
||||
case "rs":
|
||||
return "rust";
|
||||
case "js":
|
||||
case "jsx":
|
||||
case "vue":
|
||||
return "javascript";
|
||||
case "sh":
|
||||
return "shell";
|
||||
case "cs":
|
||||
return "csharp";
|
||||
case "py":
|
||||
return "python";
|
||||
case "yml":
|
||||
return "yaml";
|
||||
default:
|
||||
return extension;
|
||||
}
|
||||
}
|
||||
|
||||
export function get3DLoader(extension: string) {
|
||||
switch (extension) {
|
||||
case "fbx":
|
||||
return FBXLoader;
|
||||
case "gltf":
|
||||
return GLTFLoader;
|
||||
case "glb":
|
||||
return GLTFLoader;
|
||||
case "obj":
|
||||
return OBJLoader;
|
||||
case "stl":
|
||||
return STLLoader;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user