Skip to content

Commit

Permalink
add: more wait and check to make sure .chromedownload is not moved
Browse files Browse the repository at this point in the history
  • Loading branch information
just-hms committed Sep 21, 2024
1 parent 2dbadf8 commit ba302e5
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions cmd/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,43 @@ func getLastFile(path string) (fs.FileInfo, error) {
return lastFile, nil
}

func waitForDownload(logger logx.Logger, downloadPath string) (fs.FileInfo, string, error) {
func waitForDownload(logger logx.Logger, downloadPath string) (fs.FileInfo, error) {
spinner := termx.NewSpin(logger)
defer spinner.Flush()

for {
lastFile, err := getLastFile(downloadPath)
if err != nil {
return nil, "", err
return nil, err
}

sourcePath := filepath.Join(downloadPath, lastFile.Name())
lastFile, err = os.Stat(sourcePath)
if err != nil {
return nil, err
}

waited := false
for osx.IsLocked(sourcePath) {
lastFile, err = os.Stat(sourcePath)
if err != nil {
return nil, "", err
}
waited = true
spinner.Spin("Downloading " + osx.Size(lastFile))
time.Sleep(100 * time.Millisecond)
}
if !waited {
return lastFile, sourcePath, nil

if waited {
continue
}

time.Sleep(400 * time.Millisecond)

checkLastFile, err := getLastFile(downloadPath)
if err != nil {
return nil, err
}
if checkLastFile.Name() != lastFile.Name() {
continue
}
return checkLastFile, nil
}
}

Expand All @@ -73,9 +83,9 @@ var mvCmd = &cobra.Command{
SilenceUsage: false,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
destPath := "."
destFolder := "."
if len(args) == 1 {
destPath = args[0]
destFolder = args[0]
}

downloadPath, err := osx.DownloadFolderPath()
Expand All @@ -85,7 +95,7 @@ var mvCmd = &cobra.Command{

logger := logx.Logger{}

lastFile, sourcePath, err := waitForDownload(logger, downloadPath)
lastFile, err := waitForDownload(logger, downloadPath)
if err != nil {
return fmt.Errorf("failed to get the last downloaded file: %v", err)
}
Expand All @@ -105,12 +115,14 @@ var mvCmd = &cobra.Command{
fmt.Println()
}

err = osx.Move(sourcePath, destPath)
sourcePath := filepath.Join(downloadPath, lastFile.Name())

err = osx.Move(sourcePath, destFolder)
if err != nil {
return fmt.Errorf("failed to %v", err)
}
if verboseFlag {
fmt.Println(filepath.Join(destPath, filepath.Base(sourcePath)))
fmt.Println(filepath.Join(destFolder, lastFile.Name()))
}
return nil
},
Expand Down

0 comments on commit ba302e5

Please sign in to comment.