diff --git a/handle_file.go b/handle_file.go index 38c77f9..efeb34a 100644 --- a/handle_file.go +++ b/handle_file.go @@ -1,7 +1,9 @@ package main import ( - "fmt" + "errors" + "io" + "os" "path/filepath" "strings" "time" @@ -67,56 +69,52 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro oldFile := filepath.Join(DownloadPath, parsedFile.File) newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File)) - //lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") + lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock") b.WriteString(color.BlueString("\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)) - // if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { - // return err - // } + if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { + return err + } - // if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil { - // return err - // } + if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil { + return err + } - // if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { - // return err - // } + if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { + return err + } - // inputFile, err := os.Open(oldFile) - // if err != nil { - // return err - // } - // defer inputFile.Close() + inputFile, err := os.Open(oldFile) + if err != nil { + return err + } + defer inputFile.Close() - // outputFile, err := os.Create(newFile) - // if err != nil { - // return err - // } - // defer outputFile.Close() + outputFile, err := os.Create(newFile) + if err != nil { + return err + } + defer outputFile.Close() - // if err := os.Chown(newFile, Uid, Gid); err != nil { - // return err - // } + if err := os.Chown(newFile, Uid, Gid); err != nil { + return err + } - // _, err = io.Copy(outputFile, inputFile) - // if err != nil { - // return err - // } + written, err := io.Copy(outputFile, inputFile) + if err != nil { + return err + } - // if err = os.Remove(oldFile); err != nil { - // return err - // } + if err = os.Remove(oldFile); err != nil { + return err + } - // if err = os.Remove(lockFile); err != nil { - // return err - // } + if err = os.Remove(lockFile); err != nil { + return err + } - time.Sleep(1 * time.Second) - - n, err := b.WriteString(color.BlueString("\t done (took %s)\n", time.Since(start).Truncate(100*time.Millisecond))) - - fmt.Println("ASD", n, err) + b.WriteString(color.BlueString("\t done (copying %s took %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond))) return nil } diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..1dffc26 --- /dev/null +++ b/utils.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + + "git.milar.in/milarin/gmath" +) + +func FormatBytes[T gmath.Integer](bytes T) string { + value := float64(bytes) + if value/1000000000000 >= 1000000000000 { + return fmt.Sprint("%.02fT", value/1000000000000) + } else if value/1000000000 >= 1000000000 { + return fmt.Sprint("%.02fG", value/1000000000) + } else if value/1000000 >= 1000000 { + return fmt.Sprint("%.02fM", value/1000000) + } else if value/1000 >= 1000 { + return fmt.Sprint("%.02fK", value/1000) + } else { + return fmt.Sprint("%.02fB", value) + } +}