🎨 改进 Windows 端第三方同步盘检测 Fix https://github.com/siyuan-note/siyuan/issues/7777

This commit is contained in:
Liang Ding 2023-03-26 10:30:54 +08:00
parent c89154545c
commit cfefe1f967
No known key found for this signature in database
GPG key ID: 136F30F901A2231D

View file

@ -22,7 +22,6 @@ import (
"io" "io"
"math/rand" "math/rand"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
@ -33,6 +32,8 @@ import (
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/denisbrodbeck/machineid" "github.com/denisbrodbeck/machineid"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/siyuan-note/httpclient" "github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging" "github.com/siyuan-note/logging"
) )
@ -114,7 +115,7 @@ var (
) )
var ( var (
thirdPartySyncCheckTicker = time.NewTicker(time.Minute * 10) thirdPartySyncCheckTicker = time.NewTicker(time.Second * 5)
) )
func ReportFileSysFatalError(err error) { func ReportFileSysFatalError(err error) {
@ -291,56 +292,34 @@ func existAvailabilityStatus(workspaceAbsPath string) bool {
return false return false
} }
dataAbsPath := filepath.Join(workspaceAbsPath, "data")
// 改进 Windows 端第三方同步盘检测 https://github.com/siyuan-note/siyuan/issues/7777 // 改进 Windows 端第三方同步盘检测 https://github.com/siyuan-note/siyuan/issues/7777
ps := ` defer logging.Recover()
function Get-MetaData { ole.CoInitialize(0)
[CmdletBinding()][OutputType([object])] defer ole.CoUninitialize()
param([ValidateNotNullOrEmpty()][string]$File)
chcp 65001 dataAbsPath := filepath.Join(workspaceAbsPath, "data")
$folderPath = Split-Path $File dir, file := filepath.Split(dataAbsPath)
$filePath = Split-Path $File -Leaf
try{ unknown, err := oleutil.CreateObject("Shell.Application")
$shell = New-Object -ComObject Shell.Application if nil != err {
$folderObj = $shell.NameSpace($folderPath) logging.LogWarnf("create shell application failed: %s", err)
$fileObj = $folderObj.ParseName($filePath)
$value = $folderObj.GetDetailsOf($fileObj, 303)
echo $value
}finally{
if($shell){
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$shell) | out-null
}
}
}
Get-MetaData -File "` + dataAbsPath + "\""
cmd := exec.Command("powershell", "-nologo", "-noprofile", "-command", ps)
gulu.CmdAttr(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return false return false
} }
shell, err := unknown.QueryInterface(ole.IID_IDispatch)
lines := strings.Split(string(out), "\n") if nil != err {
buf := bytes.Buffer{} logging.LogWarnf("query shell interface failed: %s", err)
for _, line := range lines { return false
line = strings.TrimSpace(line)
if strings.Contains(strings.ToLower(line), "active code page") {
continue
} }
defer shell.Release()
if "" != line { folderObj := oleutil.MustCallMethod(shell, "NameSpace", dir).ToIDispatch()
buf.WriteString(line) fileObj := oleutil.MustCallMethod(folderObj, "ParseName", file).ToIDispatch()
value := oleutil.MustCallMethod(folderObj, "GetDetailsOf", fileObj, 303)
if nil == value {
return false
} }
} status := value.Value().(string)
out = buf.Bytes()
status := strings.ToLower(strings.TrimSpace(string(out)))
if strings.Contains(status, "sync") || strings.Contains(status, "同步") || if strings.Contains(status, "sync") || strings.Contains(status, "同步") ||
strings.Contains(status, "available") || strings.Contains(status, "可用") { strings.Contains(status, "available") || strings.Contains(status, "可用") {
logging.LogErrorf("data [%s] third party sync status [%s]", dataAbsPath, status) logging.LogErrorf("data [%s] third party sync status [%s]", dataAbsPath, status)