From a4f5b7887b052d1adaebc2b63e6206aafbd3bf01 Mon Sep 17 00:00:00 2001 From: milarin Date: Sun, 15 Jan 2023 14:08:45 +0100 Subject: [PATCH] debug --- handle_file.go | 76 ++++++++++++++++++++++++-------------------------- utils.go | 22 +++++++++++++++ 2 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 utils.go 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) + } +}