diff --git a/electron/main/item/index.ts b/electron/main/item/index.ts index a99ceb4..756e679 100644 --- a/electron/main/item/index.ts +++ b/electron/main/item/index.ts @@ -1,6 +1,6 @@ import { BrowserWindow, shell, app } from "electron"; -import { join } from "node:path"; -import { parsePath, getURLParams } from "../../commons/utils"; +import { extname, join } from "node:path"; +import { parsePath, getURLParams, getFileIcon } from "../../commons/utils"; import { Item } from "../../../types/item"; import { batchAdd, @@ -15,8 +15,10 @@ import { writeFile, statSync, readFileSync, accessSync } from "node:fs"; import mime from "mime"; import { deleteExtname, + getFileName, getItemName, isAbsolutePath, + newItem, } from "../../../commons/utils/common"; import { iconExts } from "../../commons/utils"; import { addAssociateFolderWatcher } from "../classification"; @@ -29,6 +31,7 @@ import { showSaveDialogSync, } from "../commons/index"; import { fork } from "../../commons/utilityProcessUtils"; +import { ShortcutInfo } from "../../../types/common"; // 窗口 let itemAddEditWindow: BrowserWindow | null = null; @@ -260,24 +263,17 @@ function move(idList: Array, toClassificationId: number) { * @param classificationId * @param pathList */ -function drop(classificationId: number, pathList: Array) { - fork( - "getDropItemInfo", - { - classificationId, - pathList, - }, - (resultList: Array) => { - // 添加项目 - let itemList = batchAdd(classificationId, resultList); - // 发送消息到页面 - sendToWebContent("mainWindow", "onAddItem", { - itemList, - clear: false, - classificationId: null, - }); - } - ); +async function drop(classificationId: number, pathList: Array) { + // 获取项目信息 + let resultList = await getDropItemInfo(classificationId, pathList); + // 添加项目 + let itemList = batchAdd(classificationId, resultList); + // 发送消息到页面 + sendToWebContent("mainWindow", "onAddItem", { + itemList, + clear: false, + classificationId: null, + }); } /** @@ -708,6 +704,90 @@ function deleteQuickSearchHistory(itemId: number) { } } +/** + * 通过路径获取项目信息 + * @param classificationId + * @param pathList + */ +async function getDropItemInfo( + classificationId: number, + pathList: Array +) { + // 项目列表 + let itemList: Array = []; + // 解析文件信息并添加项目 + for (const path of pathList) { + try { + // item + let item = newItem({ classificationId }); + // 目标 + item.data.target = path; + // 名称 + item.name = getFileName(item.data.target); + // 判断是否是快捷方式,如果是的话,需要获取真实路径 + if (mime.getType(path) === "application/x-ms-shortcut") { + // 快捷方式 + // 获取真实文件路径和参数 + let shortcutInfo: ShortcutInfo | null = + global.addon.getShortcutFileInfo(path); + if (shortcutInfo) { + // 路径 + if (shortcutInfo.target) { + item.data.target = shortcutInfo.target; + } + // 参数 + if (shortcutInfo.arguments) { + item.data.params = shortcutInfo.arguments; + } + } + } + // 获取图标 + item.data.icon = await getFileIcon(item.data.target); + // 获取后缀,判断是否是url + let ext = extname(item.data.target); + if (ext && ext.toLowerCase() === ".url") { + // url + let url = parseUrlFileContent(readFileSync(item.data.target, "utf-8")); + if (url && url.trim() !== "") { + item.data.target = url; + item.type = 2; + } else { + continue; + } + } else { + // 文件类型 + let stats = statSync(item.data.target); + item.type = stats.isFile() ? 0 : 1; + } + // 去掉后缀 + if (item.type === 0 || item.type === 2) { + item.name = deleteExtname(item.name); + } + // push + itemList.push(item); + } catch (e) {} + } + return itemList; +} + +/** + * 解析.url文件内容以获取URL + * @param content + * @returns + */ +function parseUrlFileContent(content: string) { + if (content) { + const lines = content.split("\n"); + for (const line of lines) { + if (line.startsWith("URL=")) { + const url = line.substring(4).trim(); + return url; + } + } + } + return null; +} + export { createAddEditWindow, createNetworkIconWindow, diff --git a/electron/main/worker.ts b/electron/main/worker.ts index 425a6ca..fb1ce27 100644 --- a/electron/main/worker.ts +++ b/electron/main/worker.ts @@ -5,7 +5,7 @@ import { newItem, } from "../../commons/utils/common"; import { CommonItem, Item } from "../../types/item"; -import { parse, join, extname } from "node:path"; +import { parse, join } from "node:path"; import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import xml2js from "xml2js"; import { newCommonItem, newCommonItemData } from "../../commons/utils/common"; @@ -43,11 +43,6 @@ process.parentPort.once("message", async (event) => { res = await getStartMenuItemList(dataParam); } else if (params.name === "getAppxItemList") { res = await getAppxItemList(); - } else if (params.name === "getDropItemInfo") { - res = await getDropItemInfo( - dataParam.classificationId, - dataParam.pathList - ); } else if (params.name === "refreshItemIcon") { res = await refreshItemIcon(dataParam); } else if (params.name === "getDirectoryItemList") { @@ -558,90 +553,6 @@ function getPropertiesIcon(installLocation: string, result: any) { return null; } -/** - * 通过路径获取项目信息 - * @param classificationId - * @param pathList - */ -async function getDropItemInfo( - classificationId: number, - pathList: Array -) { - // 项目列表 - let itemList: Array = []; - // 解析文件信息并添加项目 - for (const path of pathList) { - try { - // item - let item = newItem({ classificationId }); - // 目标 - item.data.target = path; - // 名称 - item.name = getFileName(item.data.target); - // 判断是否是快捷方式,如果是的话,需要获取真实路径 - if (mime.getType(path) === "application/x-ms-shortcut") { - // 快捷方式 - // 获取真实文件路径和参数 - let shortcutInfo: ShortcutInfo | null = - global.addon.getShortcutFileInfo(path); - if (shortcutInfo) { - // 路径 - if (shortcutInfo.target) { - item.data.target = shortcutInfo.target; - } - // 参数 - if (shortcutInfo.arguments) { - item.data.params = shortcutInfo.arguments; - } - } - } - // 获取图标 - item.data.icon = await getFileIcon(item.data.target); - // 获取后缀,判断是否是url - let ext = extname(item.data.target); - if (ext && ext.toLowerCase() === ".url") { - // url - let url = parseUrlFileContent(readFileSync(item.data.target, "utf-8")); - if (url && url.trim() !== "") { - item.data.target = url; - item.type = 2; - } else { - continue; - } - } else { - // 文件类型 - let stats = statSync(item.data.target); - item.type = stats.isFile() ? 0 : 1; - } - // 去掉后缀 - if (item.type === 0 || item.type === 2) { - item.name = deleteExtname(item.name); - } - // push - itemList.push(item); - } catch (e) {} - } - return itemList; -} - -/** - * 解析.url文件内容以获取URL - * @param content - * @returns - */ -function parseUrlFileContent(content: string) { - if (content) { - const lines = content.split("\n"); - for (const line of lines) { - if (line.startsWith("URL=")) { - const url = line.substring(4).trim(); - return url; - } - } - } - return null; -} - /** * 刷新项目图标 * @param itemList