feat: fix panel persist over server rendering, etc

This commit is contained in:
2024-02-22 23:05:02 +07:00
parent 2c9faa332a
commit efe51a9b5e
27 changed files with 962 additions and 150 deletions
+11
View File
@@ -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;
+50
View File
@@ -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;
};
+15
View File
@@ -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");
};