mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
fix(import): harden directory import path handling
This commit is contained in:
1 parent
56902fea1c
commit
5db4b3bf9d
2 files changed
+15
-6
No files matched your search
@@ -164,7 +164,8 @@ export function registerImportRoutes(server: FastifyInstance, dbManager: Databas
|
|||||||
if (segments.length > 1) {
|
if (segments.length > 1) {
|
||||||
relPath = segments.slice(1).join('/')
|
relPath = segments.slice(1).join('/')
|
||||||
}
|
}
|
||||||
const targetPath = path.join(tmpDir, relPath)
|
const targetPath = path.resolve(tmpDir, relPath)
|
||||||
|
if (!targetPath.startsWith(tmpDir + path.sep)) continue
|
||||||
fs.mkdirSync(path.dirname(targetPath), { recursive: true })
|
fs.mkdirSync(path.dirname(targetPath), { recursive: true })
|
||||||
fs.writeFileSync(targetPath, fileBuffers[i].data)
|
fs.writeFileSync(targetPath, fileBuffers[i].data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,14 +125,22 @@ async function handleDrop(e: DragEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleDirectoryDrop(dirEntry: FileSystemDirectoryEntry, fileList: FileList) {
|
async function handleDirectoryDrop(dirEntry: FileSystemDirectoryEntry, fileList: FileList) {
|
||||||
// Electron: try to get directory path
|
// Electron: derive directory path from the native file path
|
||||||
if (fileList.length > 0) {
|
if (fileList.length > 0) {
|
||||||
try {
|
try {
|
||||||
// @ts-expect-error Electron webUtils
|
// @ts-expect-error Electron webUtils
|
||||||
const dirPath = window.electron?.webUtils?.getPathForFile?.(fileList[0])
|
const filePath: string | undefined = window.electron?.webUtils?.getPathForFile?.(fileList[0])
|
||||||
if (dirPath) {
|
if (filePath) {
|
||||||
emit('directory-drop', { files: [], dirPath })
|
// fileList[0] may be the directory itself or a file inside it;
|
||||||
return
|
// locate the dropped directory name as a path segment to get the correct root
|
||||||
|
const sep = filePath.includes('\\') ? '\\' : '/'
|
||||||
|
const segments = filePath.split(sep)
|
||||||
|
const dirIdx = segments.lastIndexOf(dirEntry.name)
|
||||||
|
if (dirIdx !== -1) {
|
||||||
|
const dirPath = segments.slice(0, dirIdx + 1).join(sep)
|
||||||
|
emit('directory-drop', { files: [], dirPath })
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Not Electron
|
// Not Electron
|
||||||
|
|||||||
Reference in new issue
Block a user