organizer/handle_file.go

168 lines
4.4 KiB
Go

package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
"github.com/fatih/color"
)
func HandleFile(fh *FileHandle) {
defer fh.Close()
path := fh.File
w := fh.Writer
fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path))
fmt.Fprint(w, "\ttry parsers: ")
for i, parser := range parsers.Parsers {
parsedFile, ok := parser.FileParser(&parser, path)
if !ok {
fmt.Fprint(w, color.YellowString(parser.Identity))
if i < len(parsers.Parsers)-1 {
fmt.Fprint(w, ", ")
} else {
fmt.Fprintln(w, color.RedString("\n\tfile ignored"))
}
continue
}
anime, err := logic.SearchAnimeByTitle(parsedFile.OriginalAnimeTitle)
if err != nil {
fmt.Fprintln(w, color.RedString(parser.Identity))
fmt.Fprintln(w, color.RedString("\tanime not found: '%s'", parsedFile.OriginalAnimeTitle))
break
}
fmt.Fprintln(w, color.GreenString(parser.Identity))
parsedFile.Anime = anime
HandleParsedFile(w, parsedFile)
break
}
}
func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) {
newFilePrio := logic.NewFilePriority(parsedFile)
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
// debug output
if animeEpNotExistLocally {
fmt.Fprintln(w, "\tfile exists locally")
fmt.Fprintf(w, "\t local file: %s\n", FilePrio2Str(oldFilePrio))
fmt.Fprintf(w, "\t new file: %s\n", FilePrio2Str(newFilePrio))
if newFilePrio.Priority > oldFilePrio.Priority {
fmt.Fprint(w, color.GreenString("\t overwrite local file\n"))
} else if !DeleteLowPriorityFiles {
fmt.Fprint(w, color.YellowString("\t ignore new file\n"))
}
}
// delete files with lower priority from DownloadPath
if DeleteLowPriorityFiles && animeEpNotExistLocally && oldFilePrio.Priority >= newFilePrio.Priority {
fmt.Fprint(w, color.YellowString("\tdelete file with lower priority '%s'", parsedFile.File))
err := os.Remove(parsedFile.File)
if err != nil {
fmt.Fprint(w, color.RedString(" failed: '%s'\n", err.Error()))
} else {
fmt.Fprint(w, color.GreenString(" done\n"))
}
return
}
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
DeleteOldAnimeEpisode(parsedFile.AnimeEpisode())
if err := OrganizeAnimeEpisode(w, parsedFile); err != nil {
fmt.Fprint(w, color.RedString("\terror: %s\n", err.Error()))
}
}
}
func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error {
PrepareTelegramAnimeEpMessage(parsedFile.AnimeEpisode())
start := time.Now()
oldFile := parsedFile.File
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "part")
encodedFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File))
lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock")
fmt.Fprintf(w, "\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.Chown(filepath.Dir(newFile), Uid, Gid); err != nil {
return err
}
if _, err := os.Stat(newFile); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
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()
if err := os.Chown(newFile, Uid, Gid); err != nil {
return err
}
written, err := io.Copy(outputFile, inputFile)
if err != nil {
return err
}
fmt.Fprintf(w, "\t done (copied %s in %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond))
if err := EncodeVideo(w, parsedFile.Parser.FileEncoding, newFile, encodedFile); err != nil {
return err
}
if err := os.Remove(oldFile); err != nil {
return err
}
if err := os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
func DeleteOldAnimeEpisode(animeEp model.AnimeEpisode) {
animeEpPath := logic.GetAnimeEpFilepath(animeEp, "*")
files, err := filepath.Glob(animeEpPath)
if err != nil {
panic(logic.ErrInvalidGlobSyntax.Wrap(err, animeEpPath))
}
for _, file := range files {
if filepath.Ext(file) == ".lock" {
continue
}
if err := os.Remove(file); err != nil {
adverr.Println(err)
}
}
}