diff --git a/db.sqlite b/db.sqlite index 99dc515..a7fd13a 100644 Binary files a/db.sqlite and b/db.sqlite differ diff --git a/db.sqlite-shm b/db.sqlite-shm index 6be91d4..fe9ac28 100644 Binary files a/db.sqlite-shm and b/db.sqlite-shm differ diff --git a/db.sqlite-wal b/db.sqlite-wal index fb0deeb..e69de29 100644 Binary files a/db.sqlite-wal and b/db.sqlite-wal differ diff --git a/server/tasks/pull.ts b/server/tasks/pull.ts index 0b06e49..a5afdc9 100644 --- a/server/tasks/pull.ts +++ b/server/tasks/pull.ts @@ -1,7 +1,8 @@ import useDatabase from "~/composables/useDatabase"; -import type { FileType } from '~/types/api'; +import { extname, basename } from 'node:path'; +import type { File, FileType, Tag } from '~/types/api'; +import type { CanvasColor, CanvasContent } from "~/types/canvas"; import { explorerContentTable } from "~/db/schema"; -import { eq, ne } from "drizzle-orm"; const typeMapping: Record = { ".md": "markdown", @@ -26,14 +27,88 @@ export default defineTask({ } }) as any; + const files: typeof explorerContentTable.$inferInsert = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => { + if(e.type === 'tree') + { + const title = basename(e.path); + const order = /(\d+)\. ?(.+)/gsmi.exec(title); + const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/'); + return { + path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), + order: order && order[1] ? order[1] : 0, + title: order && order[2] ? order[2] : title, + type: 'folder', + content: null, + owner: '1', + navigable: true, + private: e.path.startsWith('98.Privé'), + } + } + + const extension = extname(e.path); + const title = basename(e.path, extension); + const order = /(\d+)\. ?(.+)/gsmi.exec(title); + const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/'); + const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`)); + + return { + path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), + order: order && order[1] ? order[1] : 0, + title: order && order[2] ? order[2] : title, + type: (typeMapping[extension] ?? 'file'), + content: reshapeContent(content as string, typeMapping[extension] ?? 'File'), + owner: '1', + navigable: true, + private: e.path.startsWith('98.Privé') + } + })); + const db = useDatabase(); - const files = db.select().from(explorerContentTable).where(ne(explorerContentTable.type, 'folder')).all(); + db.delete(explorerContentTable).run(); + db.insert(explorerContentTable).values(files).run(); + + useStorage('cache').clear(); return { result: true }; } catch(e) { + console.error(e); + return { result: false, error: e }; } }, -}) \ No newline at end of file +}) + +function reshapeContent(content: string, type: FileType): string | null +{ + switch(type) + { + case "markdown": + case "file": + return content; + case "canvas": + const data = JSON.parse(content) as CanvasContent; + data.edges.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined); + data.nodes.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined); + return JSON.stringify(data); + default: + case 'folder': + return null; + } +} +function getColor(color: string): CanvasColor +{ + const colors: Record = { + '1': 'red', + '2': 'orange', + '3': 'yellow', + '4': 'green', + '5': 'cyan', + '6': 'purple', + }; + if(colors.hasOwnProperty(color)) + return { class: colors[color] }; + else + return { hex: color }; +} \ No newline at end of file diff --git a/server/tasks/push.ts b/server/tasks/push.ts index 7539c6e..9ade22e 100644 --- a/server/tasks/push.ts +++ b/server/tasks/push.ts @@ -1,8 +1,7 @@ import useDatabase from "~/composables/useDatabase"; -import { extname, basename } from 'node:path'; -import type { File, FileType, Tag } from '~/types/api'; -import type { CanvasColor, CanvasContent } from "~/types/canvas"; +import type { FileType } from '~/types/api'; import { explorerContentTable } from "~/db/schema"; +import { eq, ne } from "drizzle-orm"; const typeMapping: Record = { ".md": "markdown", @@ -27,47 +26,8 @@ export default defineTask({ } }) as any; - const files: typeof explorerContentTable.$inferInsert = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => { - if(e.type === 'tree') - { - const title = basename(e.path); - const order = /(\d+)\. ?(.+)/gsmi.exec(title); - const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/'); - return { - path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), - order: order && order[1] ? order[1] : 0, - title: order && order[2] ? order[2] : title, - type: 'folder', - content: null, - owner: '1', - navigable: true, - private: e.path.startsWith('98.Privé'), - } - } - - const extension = extname(e.path); - const title = basename(e.path, extension); - const order = /(\d+)\. ?(.+)/gsmi.exec(title); - const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/'); - const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`)); - - return { - path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), - order: order && order[1] ? order[1] : 0, - title: order && order[2] ? order[2] : title, - type: (typeMapping[extension] ?? 'file'), - content: reshapeContent(content as string, typeMapping[extension] ?? 'File'), - owner: '1', - navigable: true, - private: e.path.startsWith('98.Privé') - } - })); - const db = useDatabase(); - db.delete(explorerContentTable).run(); - db.insert(explorerContentTable).values(files).run(); - - useStorage('cache').clear(); + const files = db.select().from(explorerContentTable).where(ne(explorerContentTable.type, 'folder')).all(); return { result: true }; } @@ -76,37 +36,4 @@ export default defineTask({ return { result: false, error: e }; } }, -}) - -function reshapeContent(content: string, type: FileType): string | null -{ - switch(type) - { - case "markdown": - case "file": - return content; - case "canvas": - const data = JSON.parse(content) as CanvasContent; - data.edges.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined); - data.nodes.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined); - return JSON.stringify(data); - default: - case 'folder': - return null; - } -} -function getColor(color: string): CanvasColor -{ - const colors: Record = { - '1': 'red', - '2': 'orange', - '3': 'yellow', - '4': 'green', - '5': 'cyan', - '6': 'purple', - }; - if(colors.hasOwnProperty(color)) - return { class: colors[color] }; - else - return { hex: color }; -} \ No newline at end of file +}) \ No newline at end of file