-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc994a6
commit b964c71
Showing
19 changed files
with
338 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { mkdir, rm } from 'node:fs/promises' | ||
import { exec } from "../../../scripts/exec.mjs" | ||
|
||
export async function cloneRepo(repoUrl, targetDir) { | ||
await mkdir(targetDir, { recursive: true }) | ||
try { | ||
await exec(`git clone --depth 1 ${repoUrl} .`, { cwd: targetDir }) | ||
} catch (err) { | ||
await rm(targetDir, { recursive: true, force: true }) | ||
throw err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { unzipDirectory } from './zip.mjs' | ||
import { cloneRepo } from './git.mjs' | ||
import { getAvailablePath } from './path.mjs' | ||
import { mkdir, rename, rm } from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { tmpdir } from 'node:os' | ||
import { loadJsonFile } from "../../../scripts/json_loader.mjs" | ||
|
||
export default { | ||
info: { | ||
'': { | ||
name: 'fount', | ||
avatar: '', | ||
description: 'default description', | ||
description_markdown: 'default description', | ||
version: '1.0.0', | ||
author: 'steve02081504', | ||
homepage: '', | ||
tags: [] | ||
} | ||
}, | ||
async ImportAsData(username, data) { | ||
const tempDir = path.join(tmpdir(), 'fount_import_' + Date.now()) | ||
await mkdir(tempDir, { recursive: true }) | ||
try { | ||
await unzipDirectory(data, tempDir) | ||
} catch (err) { | ||
console.error('Unzip failed:', err) | ||
await rm(tempDir, { recursive: true, force: true }) | ||
throw new Error(`Unzip failed: ${err.message || err}`) | ||
} | ||
try { | ||
let metaPath = path.join(tempDir, 'fount.json') | ||
let meta = await loadJsonFile(metaPath) | ||
let targetPath = await getAvailablePath(username, meta.type, meta.dirname) | ||
await rename(tempDir, targetPath) | ||
} catch (err) { | ||
await rm(tempDir, { recursive: true, force: true }) | ||
throw new Error(`loadMeta failed: ${err.message || err}`) | ||
} | ||
}, | ||
async ImportByText(username, text) { | ||
const lines = text.trim().split('\n').map(line => line.trim()).filter(line => line) | ||
for (const line of lines) | ||
if (line.startsWith('http')) { | ||
const tempDir = path.join(tmpdir(), 'fount_import_git_' + Date.now()) | ||
try { | ||
await cloneRepo(line, tempDir) | ||
let metaPath = path.join(tempDir, 'fount.json') | ||
let meta = await loadJsonFile(metaPath) | ||
let targetPath = await getAvailablePath(username, meta.type, meta.dirname) | ||
await rename(tempDir, targetPath) | ||
} catch (err) { | ||
console.error(`Git clone failed for ${line}:`, err) | ||
await rm(tempDir, { recursive: true, force: true }) | ||
throw new Error(`Git clone failed for ${line}: ${err.message || err}`) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import path from 'node:path' | ||
import fs from 'node:fs' | ||
import { getUserDictionary } from "../../../server/auth.mjs" | ||
|
||
export function resolvePath(username, type, name) { | ||
let userPath = getUserDictionary(username) | ||
let partPath = path.join(userPath, type, name) | ||
return partPath | ||
} | ||
|
||
export function getAvailablePath(username, type, name) { | ||
let targetPath = resolvePath(username, type, name) | ||
if (fs.existsSync(targetPath)) | ||
fs.rmSync(targetPath, { recursive: true, force: true }) | ||
return targetPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import jszip from 'npm:jszip' | ||
import { writeFile, mkdir, readdir, stat, readFile } from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { Buffer } from "node:buffer" | ||
|
||
async function zipDirectory(dirPath, zip) { | ||
|
||
const items = await readdir(dirPath) | ||
|
||
for (const item of items) { | ||
const itemPath = path.join(dirPath, item) | ||
const itemStat = await stat(itemPath) | ||
|
||
if (itemStat.isDirectory()) | ||
await zipDirectory(itemPath, zip.folder(item)) // 递归压缩目录 | ||
else { | ||
const content = await readFile(itemPath) | ||
zip.file(item, content) | ||
} | ||
} | ||
} | ||
|
||
export async function zipDir(dirPath) { | ||
const zip = new jszip() | ||
await zipDirectory(dirPath, zip) | ||
return zip.generateAsync({ type: 'nodebuffer' }) | ||
} | ||
|
||
export async function unzipDirectory(buffer, targetPath) { | ||
try { | ||
const zip = new jszip() | ||
await zip.loadAsync(buffer) | ||
|
||
for (const zipEntry of Object.values(zip.files)) | ||
if (zipEntry.dir) { | ||
// 如果是目录,则创建目录 | ||
const dirPath = path.join(targetPath, zipEntry.name) | ||
await mkdir(dirPath, { recursive: true }) | ||
console.log(`Created directory: ${dirPath}`) | ||
} else { | ||
// 如果是文件,则写入文件 | ||
const filePath = path.join(targetPath, zipEntry.name) | ||
const fileBuffer = await zipEntry.async('nodebuffer') | ||
await mkdir(path.dirname(filePath), { recursive: true }) | ||
await writeFile(filePath, fileBuffer) | ||
console.log(`Wrote file: ${filePath}`) | ||
} | ||
} catch (err) { | ||
console.error('unzip error:',err) | ||
throw err | ||
} | ||
} | ||
|
||
export async function readZipfile(buffer, zipPath) { | ||
const zip = new jszip() | ||
await zip.loadAsync(buffer) | ||
const file = zip.files[zipPath] | ||
if (!file || file.dir) | ||
throw new Error(`File not found in ZIP: ${zipPath}`) | ||
|
||
return await file.async('nodebuffer') | ||
} | ||
|
||
export async function readZipfileAsJSON(buffer, zipPath) { | ||
try { | ||
let filebuffer = await readZipfile(buffer, zipPath) | ||
return JSON.parse(filebuffer.toString()) | ||
} catch (err) { | ||
throw new Error(`Failed to parse JSON file in ZIP ${zipPath}, ${err.message||err}`) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 35 additions & 17 deletions
52
src/public/shells/install/src/server/Installer_handler.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
import { LoadCharTemplate } from "./charTemplate_manager.mjs" | ||
import { LoadImportHanlder } from "./importHanlder_manager.mjs" | ||
import { getPartList } from '../../../../../server/parts_loader.mjs' | ||
|
||
export async function importChar(username, data) { | ||
let charTemplates = getPartList(username, 'charTemplates') | ||
for (let charTemplate of charTemplates) try { | ||
let template = await LoadCharTemplate(username, charTemplate) | ||
await template.ImportChar(username, data) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
export async function importPart(username, data) { | ||
let ImportHanlders = getPartList(username, 'ImportHanlders') | ||
const errors = [] | ||
|
||
for (let importHanlder of ImportHanlders) | ||
try { | ||
let hanlder = await LoadImportHanlder(username, importHanlder) | ||
await hanlder.ImportAsData(username, data) | ||
return | ||
} catch (err) { | ||
errors.push({ hanlder: importHanlder, error: err.message || String(err) }) | ||
console.log(`hanlder ${importHanlder} failed:`, err) | ||
} | ||
|
||
// 如果所有模板都失败,抛出包含所有错误的异常 | ||
if (errors.length > 0) | ||
throw Object.assign(new Error("All hanlders failed"), { errors }) | ||
} | ||
|
||
export async function importCharByText(username, text) { | ||
let charTemplates = getPartList(username, 'charTemplates') | ||
for (let charTemplate of charTemplates) try { | ||
let template = await LoadCharTemplate(username, charTemplate) | ||
await template.ImportCharByText(username, text) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
export async function importPartByText(username, text) { | ||
let ImportHanlders = getPartList(username, 'ImportHanlders') | ||
const errors = [] | ||
|
||
for (let importHanlder of ImportHanlders) | ||
try { | ||
let hanlder = await LoadImportHanlder(username, importHanlder) | ||
await hanlder.ImportByText(username, text) | ||
return | ||
} catch (err) { | ||
errors.push({ hanlder: importHanlder, error: err.message || String(err) }) | ||
console.log(`hanlder ${importHanlder} failed:`, err) | ||
} | ||
|
||
|
||
if (errors.length > 0) | ||
throw Object.assign(new Error("All hanlders failed"), { errors }) | ||
} |
22 changes: 0 additions & 22 deletions
22
src/public/shells/install/src/server/charTemplate_manager.mjs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.