1 Star 2 Fork 0

daniel2y/biliFyneExplorer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
file_xdg.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
daniel2y 提交于 2023-12-01 19:34 . 12
//go:build (linux || openbsd || freebsd || netbsd) && !android && !wasm && !js
// +build linux openbsd freebsd netbsd
// +build !android
// +build !wasm
// +build !js
package main
import (
"fmt"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
"golang.org/x/sys/execabs"
)
func getFavoriteLocation(homeURI fyne.URI, name, fallbackName string) (fyne.URI, error) {
cmdName := "xdg-user-dir"
if _, err := execabs.LookPath(cmdName); err != nil {
return storage.Child(homeURI, fallbackName) // no lookup possible
}
cmd := execabs.Command(cmdName, name)
loc, err := cmd.Output()
if err != nil {
return storage.Child(homeURI, fallbackName)
}
// Remove \n at the end
loc = loc[:len(loc)-1]
locURI := storage.NewFileURI(string(loc))
if locURI.String() == homeURI.String() {
fallback, _ := storage.Child(homeURI, fallbackName)
return fallback, fmt.Errorf("this computer does not define a %s folder", name)
}
return locURI, nil
}
func getFavoriteLocations() (map[string]fyne.ListableURI, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
homeURI := storage.NewFileURI(homeDir)
favoriteNames := getFavoriteOrder()
arguments := map[string]string{
"Documents": "DOCUMENTS",
"Downloads": "DOWNLOAD",
"Music": "MUSIC",
"Pictures": "PICTURES",
"Videos": "VIDEOS",
}
home, _ := storage.ListerForURI(homeURI)
favoriteLocations := map[string]fyne.ListableURI{
"Home": home,
}
for _, favName := range favoriteNames {
var uri fyne.URI
uri, err = getFavoriteLocation(homeURI, arguments[favName], favName)
listURI, err1 := storage.ListerForURI(uri)
if err1 != nil {
err = err1
continue
}
favoriteLocations[favName] = listURI
}
return favoriteLocations, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/y2h/bili-fyne-explorer.git
git@gitee.com:y2h/bili-fyne-explorer.git
y2h
bili-fyne-explorer
biliFyneExplorer
master

搜索帮助