debugging

This commit is contained in:
milarin 2023-01-15 13:59:48 +01:00
parent 507e114908
commit fdf31e22e4

View File

@ -1,9 +1,7 @@
package main
import (
"errors"
"io"
"os"
"fmt"
"path/filepath"
"strings"
"time"
@ -16,7 +14,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 +55,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()))
}
}
}
@ -68,52 +67,56 @@ 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
}
// _, 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
// }
b.WriteString(color.BlueString("\t done (took %s)\n", time.Since(start)))
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)
return nil
}