From b0b7b6f17a882d87c9d07b6a526818bab90fc900 Mon Sep 17 00:00:00 2001 From: Jason Zhang <44888353+AnonymousMister@users.noreply.github.com> Date: Tue, 29 Aug 2023 21:01:36 +0800 Subject: [PATCH] Improve iCloud path checking (#9066) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: icloud目录判断范围太广 icloud 目录匹配包含导致全局无法正常使用 * fix: 提交修复icloud判断问题 1.删除无用的else块 2.完善simpleCheckIcloudPath方法 添加 桌面 和文档的路径判断 --- app/electron/init.html | 44 +++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/app/electron/init.html b/app/electron/init.html index ba010adf47b..b689b6a8fd6 100644 --- a/app/electron/init.html +++ b/app/electron/init.html @@ -421,17 +421,45 @@

SiYuan

// macOS 端对工作空间放置在 iCloud 路径下做检查 https://github.com/siyuan-note/siyuan/issues/7747 const path = require('path') - const iCloudRoot = path.join(decodeURIComponent(getSearch('home')), 'Library', 'Mobile Documents') - const allFiles = walk(iCloudRoot) + const homePath = decodeURIComponent(getSearch('home')) const absPathLower = absPath.toLowerCase() - for (const file of allFiles) { - if (-1 < absPathLower.indexOf(file.toLowerCase())) { - return true + const iCloudRoot = path.join(homePath, 'Library', 'Mobile Documents') + if(!simpleCheckIcloudPath(absPath, homePath)){ + //简单判断无法通过则复杂验证 + const allFiles = walk(iCloudRoot) + for (const file of allFiles) { + if (-1 < absPathLower.indexOf(file.toLowerCase())) { + return true + } } - } + } return false } + //简单判断Icloud同步目录 + //不允许 为桌面 文档 和 icloud 文件夹 和软链接 + const simpleCheckIcloudPath =(absPath, homePath)=>{ + const fs = require('fs') + let stat = fs.lstatSync(absPath) + if(stat.isSymbolicLink()){ + return false + } + const path = require('path') + const absPathLower = absPath.toLowerCase() + const iCloudRoot = path.join(homePath, 'Library', 'Mobile Documents') + if(absPathLower.startsWith(iCloudRoot.toLowerCase())){ + return false + } + const documentsRoot = path.join(homePath, 'Documents') + if (absPathLower.startsWith(documentsRoot.toLowerCase())) { + return false + } + const desktopRoot = path.join(homePath, 'Desktop') + if (absPathLower.startsWith(desktopRoot.toLowerCase())) { + return false + } + return true + } const walk = (dir, files = []) => { let dirFiles; const fs = require('fs') @@ -440,7 +468,6 @@

SiYuan

console.log("dir [" + dir + "] not exists") return files } - dirFiles = fs.readdirSync(dir) } catch (e) { console.error("read dir [" + dir + "] failed: ", e) @@ -459,9 +486,8 @@

SiYuan

if (files.includes(dir + path.sep + f)) { continue } - walk(dir + path.sep + f, files) - } else { files.push(dir + path.sep + f) + walk(dir + path.sep + f, files) } } return files