mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: fix panel persist over server rendering, etc
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import { Mime } from "mime/lite";
|
||||
import standardTypes from "mime/types/standard.js";
|
||||
import otherTypes from "mime/types/other.js";
|
||||
|
||||
const mime = new Mime(standardTypes, otherTypes);
|
||||
mime.define({ "text/javascript": ["jsx", "tsx"] }, true);
|
||||
|
||||
export const getMimeType = (
|
||||
ext: string,
|
||||
defaultMime: string = "application/octet-stream"
|
||||
) => mime.getType(ext) || defaultMime;
|
||||
@@ -0,0 +1,50 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import db from "../db";
|
||||
import { file } from "../db/schema/file";
|
||||
import { fileExists, getProjectDir } from "./utils";
|
||||
import { getFileExt } from "~/lib/utils";
|
||||
|
||||
type UnpackProjectOptions = {
|
||||
ext: string;
|
||||
};
|
||||
|
||||
export const unpackProject = async (
|
||||
opt: Partial<UnpackProjectOptions> = {}
|
||||
) => {
|
||||
const files = await db.query.file.findMany({
|
||||
where: and(
|
||||
eq(file.isDirectory, false),
|
||||
eq(file.isFile, false),
|
||||
isNull(file.deletedAt)
|
||||
),
|
||||
});
|
||||
|
||||
const projectDir = getProjectDir();
|
||||
if (!fileExists(projectDir)) {
|
||||
await fs.mkdir(projectDir, { recursive: true });
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
const ext = getFileExt(file.filename);
|
||||
|
||||
// skip file if not in included extension list
|
||||
if (opt.ext && opt.ext.length > 0 && !opt.ext.split(",").includes(ext)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const fpath = path.resolve(
|
||||
projectDir,
|
||||
file.path.replace(/(\.{2})(\/+)/g, "")
|
||||
);
|
||||
const dir = path.dirname(fpath);
|
||||
if (!fileExists(dir)) {
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
}
|
||||
|
||||
await fs.writeFile(fpath, file.content || "");
|
||||
}
|
||||
|
||||
return projectDir;
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
export const fileExists = (path: string) => {
|
||||
try {
|
||||
fs.accessSync(path, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const getProjectDir = () => {
|
||||
return path.resolve(process.cwd(), "storage/tmp/project1");
|
||||
};
|
||||
Reference in New Issue
Block a user