diff --git a/src/components/function.go b/src/components/function.go index 2b4f7142..cce0a3f3 100644 --- a/src/components/function.go +++ b/src/components/function.go @@ -7,7 +7,6 @@ import ( "log" "math" "os" - "os/user" "path/filepath" "regexp" "sort" @@ -17,29 +16,16 @@ import ( "github.com/lithammer/shortuuid" "github.com/rkoesters/xdg/userdirs" + "github.com/shirou/gopsutil/disk" ) -func getFolder() []folder { - var paths []string - - currentUser, err := user.Current() - if err != nil { - OutPutLog("Get user path error", err) - } - username := currentUser.Username - - folderPath := filepath.Join("/run/media", username) - entries, err := os.ReadDir(folderPath) +func GetFolder() []folder { + disks, err := GetExternalDisk() if err != nil { OutPutLog("Get external media error", err) } - for _, entry := range entries { - if entry.IsDir() { - paths = append(paths, filepath.Join(folderPath, entry.Name())) - } - } jsonData, err := os.ReadFile(SuperFileDataDir + pinnedFile) if err != nil { OutPutLog("Read superfile data error", err) @@ -65,14 +51,38 @@ func getFolder() []folder { folders = append(folders, folder{location: path, name: folderName}) } } - for _, path := range paths { - folderName := filepath.Base(path) - folders = append(folders, folder{location: path, name: folderName}) + + for _, disk := range disks { + folderName := filepath.Base(disk.Mountpoint) + folders = append(folders, folder{location: disk.Mountpoint, name: folderName}) } return folders } +func GetExternalDisk() (disks []disk.PartitionStat, err error) { + parts, err := disk.Partitions(true) + + if err != nil { + return []disk.PartitionStat{}, err + } + for _, disk := range parts { + if IsExternalDiskPath(disk.Mountpoint) { + disks = append(disks, disk) + } + } + + return disks, err +} + +func IsExternalDiskPath(path string) bool { + dir := filepath.Dir(path) + return strings.HasPrefix(dir, "/mnt") || + strings.HasPrefix(dir, "/media") || + strings.HasPrefix(dir, "/run/media") || + strings.HasPrefix(dir, "/Volumes") +} + func repeatString(s string, count int) string { return strings.Repeat(s, count) } @@ -461,7 +471,3 @@ func countFiles(dirPath string) (int, error) { return count, err } - -func IsExternalPath(path string) bool { - return strings.HasPrefix(path, "/run/media") -} diff --git a/src/components/model.go b/src/components/model.go index 7b6ba4b0..6d522d77 100644 --- a/src/components/model.go +++ b/src/components/model.go @@ -102,7 +102,7 @@ func InitialModel(dir string) model { }, sideBarModel: sideBarModel{ pinnedModel: pinnedModel{ - folder: getFolder(), + folder: GetFolder(), }, }, fileModel: fileModel{ @@ -170,7 +170,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case Config.Confirm[0], Config.Confirm[1]: m = CreateItem(m) } - // if in the renaming mode + // if in the renaming mode } else if m.warnModal.open { switch msg.String() { case Config.Cancel[0], Config.Cancel[1]: @@ -188,7 +188,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { }() } } - // if in the renaming mode + // if in the renaming mode } else if m.fileModel.renaming { switch msg.String() { case Config.Cancel[0], Config.Cancel[1]: @@ -288,7 +288,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case Config.FilePanelSelectModeItemDelete[0], Config.FilePanelSelectModeItemDelete[1]: go func() { m = DeleteMultipleItem(m) - if !IsExternalPath(m.fileModel.filePanels[m.filePanelFocusIndex].location) { + if !IsExternalDiskPath(m.fileModel.filePanels[m.filePanelFocusIndex].location) { m.fileModel.filePanels[m.filePanelFocusIndex].selected = m.fileModel.filePanels[m.filePanelFocusIndex].selected[:0] } }() @@ -344,7 +344,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } else { cmd = tea.Batch(cmd, listenForchannelMessage(channel)) } - m.sideBarModel.pinnedModel.folder = getFolder() + m.sideBarModel.pinnedModel.folder = GetFolder() return m, cmd } diff --git a/src/components/normalModeController.go b/src/components/normalModeController.go index c525942b..1d051994 100644 --- a/src/components/normalModeController.go +++ b/src/components/normalModeController.go @@ -125,7 +125,7 @@ func DeleteSingleItem(m model) model { return m } - if IsExternalPath(panel.location) || runtime.GOOS == "darwin" { + if IsExternalDiskPath(panel.location) || runtime.GOOS == "darwin" { channel <- channelMessage{ messageId: id, returnWarnModal: true, diff --git a/src/components/selectModeController.go b/src/components/selectModeController.go index 79003aeb..38b5e373 100644 --- a/src/components/selectModeController.go +++ b/src/components/selectModeController.go @@ -142,7 +142,7 @@ func DeleteMultipleItem(m model) model { panel := m.fileModel.filePanels[m.filePanelFocusIndex] if len(panel.selected) != 0 { id := shortuuid.New() - if IsExternalPath(panel.location) || runtime.GOOS == "darwin" { + if IsExternalDiskPath(panel.location) || runtime.GOOS == "darwin" { channel <- channelMessage{ messageId: id, returnWarnModal: true, diff --git a/src/go.mod b/src/go.mod index 95aa8dc2..a2560135 100644 --- a/src/go.mod +++ b/src/go.mod @@ -20,6 +20,7 @@ require ( github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.18 // indirect @@ -32,6 +33,7 @@ require ( github.com/rivo/uniseg v0.4.6 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect + github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/stretchr/testify v1.8.4 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/sync v0.5.0 // indirect diff --git a/src/go.sum b/src/go.sum index db9c16a6..6bb278a8 100644 --- a/src/go.sum +++ b/src/go.sum @@ -20,6 +20,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -62,6 +64,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=