debugging

This commit is contained in:
milarin 2023-01-15 13:59:48 +01:00
parent 507e114908
commit a69e593d70
2 changed files with 30 additions and 6 deletions

View File

@ -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
}

23
utils.go Normal file
View File

@ -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)
}
}