Skip to content

Commit

Permalink
fix external media error
Browse files Browse the repository at this point in the history
  • Loading branch information
yorukot committed Apr 10, 2024
1 parent 16b2ce6 commit 338ff9f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
54 changes: 30 additions & 24 deletions src/components/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"math"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -461,7 +471,3 @@ func countFiles(dirPath string) (int, error) {

return count, err
}

func IsExternalPath(path string) bool {
return strings.HasPrefix(path, "/run/media")
}
10 changes: 5 additions & 5 deletions src/components/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func InitialModel(dir string) model {
},
sideBarModel: sideBarModel{
pinnedModel: pinnedModel{
folder: getFolder(),
folder: GetFolder(),
},
},
fileModel: fileModel{
Expand Down Expand Up @@ -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]:
Expand All @@ -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]:
Expand Down Expand Up @@ -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]
}
}()
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/normalModeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/components/selectModeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 338ff9f

Please sign in to comment.