organizer/handle_file.go

168 lines
4.4 KiB
Go
Raw Permalink Normal View History

2023-01-13 19:23:36 +01:00
package main
import (
2023-01-14 12:23:00 +01:00
"errors"
2023-01-15 13:59:48 +01:00
"fmt"
2023-01-14 12:23:00 +01:00
"io"
"os"
2023-01-13 19:23:36 +01:00
"path/filepath"
2023-01-13 20:04:57 +01:00
"time"
2023-01-13 19:23:36 +01:00
"git.milar.in/milarin/adverr"
2023-01-13 19:23:36 +01:00
"git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
2023-01-13 20:04:57 +01:00
"github.com/fatih/color"
2023-01-13 19:23:36 +01:00
)
2023-01-15 19:43:36 +01:00
func HandleFile(fh *FileHandle) {
defer fh.Close()
path := fh.File
w := fh.Writer
2023-01-15 13:59:48 +01:00
fmt.Fprint(w, color.MagentaString("%s file found: %s\n", time.Now().Format("2006-01-02 15:04:05"), path))
2023-01-13 20:04:57 +01:00
2023-01-15 19:43:36 +01:00
fmt.Fprint(w, "\ttry parsers: ")
for i, parser := range parsers.Parsers {
2023-01-13 19:23:36 +01:00
parsedFile, ok := parser.FileParser(&parser, path)
if !ok {
2023-01-15 19:43:36 +01:00
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"))
}
2023-01-13 19:23:36 +01:00
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
2023-01-13 19:23:36 +01:00
}
fmt.Fprintln(w, color.GreenString(parser.Identity))
2023-01-13 19:23:36 +01:00
parsedFile.Anime = anime
2023-01-15 13:59:48 +01:00
HandleParsedFile(w, parsedFile)
break
2023-01-13 19:23:36 +01:00
}
}
2023-01-15 13:59:48 +01:00
func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) {
2023-01-13 19:23:36 +01:00
newFilePrio := logic.NewFilePriority(parsedFile)
oldFilePrio, animeEpNotExistLocally := logic.GetAnimeEpProps(parsedFile.AnimeEpisode())
2023-01-15 13:59:48 +01:00
// debug output
2023-01-14 12:11:10 +01:00
if animeEpNotExistLocally {
2023-01-15 19:43:36 +01:00
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))
2023-01-14 12:11:10 +01:00
if newFilePrio.Priority > oldFilePrio.Priority {
2023-01-15 13:59:48 +01:00
fmt.Fprint(w, color.GreenString("\t overwrite local file\n"))
2023-01-15 19:43:36 +01:00
} else if !DeleteLowPriorityFiles {
2023-01-15 13:59:48 +01:00
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()))
2023-01-14 12:11:10 +01:00
} else {
2023-01-15 13:59:48 +01:00
fmt.Fprint(w, color.GreenString(" done\n"))
2023-01-14 12:11:10 +01:00
}
2023-01-15 13:59:48 +01:00
return
2023-01-14 12:11:10 +01:00
}
2023-01-13 19:23:36 +01:00
if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority {
DeleteOldAnimeEpisode(parsedFile.AnimeEpisode())
2023-01-15 13:59:48 +01:00
if err := OrganizeAnimeEpisode(w, parsedFile); err != nil {
fmt.Fprint(w, color.RedString("\terror: %s\n", err.Error()))
2023-01-14 12:11:10 +01:00
}
2023-01-13 19:23:36 +01:00
}
}
2023-01-15 13:59:48 +01:00
func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error {
PrepareTelegramAnimeEpMessage(parsedFile.AnimeEpisode())
2023-01-13 20:04:57 +01:00
start := time.Now()
2023-01-15 22:19:49 +01:00
oldFile := parsedFile.File
2023-01-16 00:00:40 +01:00
newFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "part")
encodedFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), filepath.Ext(parsedFile.File))
2023-01-14 12:23:00 +01:00
lockFile := logic.GetAnimeEpFilepath(parsedFile.AnimeEpisode(), "lock")
2023-01-13 19:23:36 +01:00
2023-01-15 19:43:36 +01:00
fmt.Fprintf(w, "\tmove file\n\t from: '%s'\n\t to: '%s'\n", oldFile, newFile)
2023-01-13 19:23:36 +01:00
2023-01-14 12:23:00 +01:00
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
}
2023-01-15 13:59:48 +01:00
if _, err := os.Stat(newFile); err != nil && !errors.Is(err, os.ErrNotExist) {
2023-01-14 12:23:00 +01:00
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
}
2023-01-15 13:59:48 +01:00
written, err := io.Copy(outputFile, inputFile)
2023-01-14 12:23:00 +01:00
if err != nil {
return err
}
2023-01-16 00:00:40 +01:00
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 {
2023-01-14 12:23:00 +01:00
return err
}
2023-01-16 00:00:40 +01:00
if err := os.Remove(oldFile); err != nil {
2023-01-14 12:23:00 +01:00
return err
}
2023-01-13 19:23:36 +01:00
2023-01-16 00:00:40 +01:00
if err := os.Remove(lockFile); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
2023-01-13 19:23:36 +01:00
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)
}
}
}