From a69e593d7072706c9530a0114ed8a03f88422307 Mon Sep 17 00:00:00 2001 From: milarin Date: Sun, 15 Jan 2023 13:59:48 +0100 Subject: [PATCH] debugging --- handle_file.go | 13 +++++++------ utils.go | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 utils.go diff --git a/handle_file.go b/handle_file.go index 655755e..4eaa23e 100644 --- a/handle_file.go +++ b/handle_file.go @@ -16,7 +16,7 @@ import ( func HandleFile(path string) (b *strings.Builder) { b = &strings.Builder{} - b.WriteString(color.MagentaString("\nfile found: %s\n", path)) + b.WriteString(color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path)) for _, parser := range parsers.Parsers { parsedFile, ok := parser.FileParser(&parser, path) @@ -57,8 +57,9 @@ func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) { } if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority { + // TODO remove old file when overwriting existing episode (might have other file extension) if err := OrganizeAnimeEpisode(b, parsedFile); err != nil { - b.WriteString(color.RedString("\terror: %s", err.Error())) + b.WriteString(color.RedString("\terror: %s\n", err.Error())) } } } @@ -80,7 +81,7 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro return err } - if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) { + if _, err := os.Stat(newFile); err != nil && !errors.Is(err, os.ErrNotExist) { return err } @@ -100,7 +101,7 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro return err } - _, err = io.Copy(outputFile, inputFile) + written, err := io.Copy(outputFile, inputFile) if err != nil { return err } @@ -109,11 +110,11 @@ func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) erro return err } - if err = os.Remove(lockFile); err != nil { + if err = os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) { return err } - b.WriteString(color.BlueString("\t done (took %s)\n", time.Since(start))) + 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..28a6f87 --- /dev/null +++ b/utils.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + + "git.milar.in/milarin/gmath" +) + +func FormatBytes[T gmath.Integer](bytes T) string { + value := float64(bytes) + + if value >= 1000000000000 { + return fmt.Sprintf("%.02fT", value/1000000000000) + } else if value >= 1000000000 { + return fmt.Sprintf("%.02fG", value/1000000000) + } else if value >= 1000000 { + return fmt.Sprintf("%.02fM", value/1000000) + } else if value >= 1000 { + return fmt.Sprintf("%.02fK", value/1000) + } else { + return fmt.Sprintf("%.02fB", value) + } +}