37 lines
1.5 KiB
TypeScript
37 lines
1.5 KiB
TypeScript
import useDatabase from '~/composables/useDatabase';
|
|
import { explorerContentTable } from '~/db/schema';
|
|
import { schema } from '~/schemas/file';
|
|
import { parsePath } from '~/shared/general.utils';
|
|
|
|
export default defineEventHandler(async (e) => {
|
|
const body = await readValidatedBody(e, schema.safeParse);
|
|
if(!body.success)
|
|
{
|
|
setResponseStatus(e, 403);
|
|
throw body.error;
|
|
}
|
|
|
|
const db = useDatabase();
|
|
|
|
const buffer = Buffer.from(convertToStorableLinks(body.data.content, db.select({ path: explorerContentTable.path }).from(explorerContentTable).all().map(e => e.path)), 'utf-8');
|
|
|
|
const content = db.insert(explorerContentTable).values({ ...body.data, content: buffer }).onConflictDoUpdate({ target: explorerContentTable.path, set: { ...body.data, content: buffer, timestamp: new Date() } });
|
|
|
|
if(content !== undefined)
|
|
{
|
|
return content;
|
|
}
|
|
|
|
setResponseStatus(e, 404);
|
|
return;
|
|
});
|
|
|
|
export function convertToStorableLinks(content: string, path: string[]): string
|
|
{
|
|
return content.replaceAll(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/g, (e: string, a1?: string, a2?: string , a3?: string) => {
|
|
const parsed = parsePath(a1 ?? '%%%%----%%%%----%%%%');
|
|
const replacer = path.find(e => e.endsWith(parsed));
|
|
const value = `[[${a1 ? (replacer ?? '') : ''}${a2 ?? ''}${(!a3 && a1 && replacer !== parsed ? '|' + a1 : a3) ?? ''}]]`;
|
|
return value;
|
|
});
|
|
} |