organizer/handle_file.go
2023-01-15 14:10:27 +01:00

137 lines
3.4 KiB
Go

package main
import (
"errors"
"io"
"os"
"path/filepath"
"strings"
"time"
"git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
"github.com/fatih/color"
)
func HandleFile(path string) (b *strings.Builder) {
b = &strings.Builder{}
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)
if !ok {
b.WriteString(color.YellowString("\tnot parsable with parser '%s'\n", parser.Identity))
continue
}
b.WriteString(color.GreenString("\tparsable with parser '%s'\n", parser.Identity))
anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
if err != nil {
b.WriteString(color.RedString("\tanime not found: '%s'\n", parsedFile.OriginalAnimeTitle))
continue
}
parsedFile.Anime = anime
HandleParsedFile(b, parsedFile)
return
}
return
}
func HandleParsedFile(b *strings.Builder, parsedFile *model.ParsedFile) {
newFilePrio := logic.NewFilePriority(parsedFile)
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
if animeEpNotExistLocally {
b.WriteString(color.YellowString("\tfile exists locally\n"))
b.WriteString(color.YellowString("\t local file: %s\n", FilePrio2Str(oldFilePrio)))
b.WriteString(color.YellowString("\t new file: %s\n", FilePrio2Str(newFilePrio)))
if newFilePrio.Priority > oldFilePrio.Priority {
b.WriteString(color.GreenString("\t overwrite local file\n"))
} else {
b.WriteString(color.YellowString("\t ignore new file\n"))
}
}
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\n", err.Error()))
}
}
}
func OrganizeAnimeEpisode(b *strings.Builder, parsedFile *model.ParsedFile) error {
start := time.Now()
oldFile := filepath.Join(DownloadPath, parsedFile.File)
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File))
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
}
b.WriteString("1")
if err := os.Chown(filepath.Dir(newFile), Uid, Gid); err != nil {
return err
}
b.WriteString("2")
if _, err := os.Stat(newFile); !errors.Is(err, os.ErrNotExist) {
return err
}
b.WriteString("3")
inputFile, err := os.Open(oldFile)
if err != nil {
return err
}
defer inputFile.Close()
b.WriteString("4")
outputFile, err := os.Create(newFile)
if err != nil {
return err
}
defer outputFile.Close()
b.WriteString("5")
if err := os.Chown(newFile, Uid, Gid); err != nil {
return err
}
b.WriteString("6")
written, err := io.Copy(outputFile, inputFile)
if err != nil {
return err
}
b.WriteString("7 " + FormatBytes(written))
if err = os.Remove(oldFile); err != nil {
return err
}
b.WriteString("8")
if err = os.Remove(lockFile); err != nil {
return err
}
b.WriteString(color.BlueString("\t done (copying %s took %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)))
return nil
}