This commit is contained in:
milarin 2023-01-15 14:08:45 +01:00
parent fdf31e22e4
commit a4f5b7887b
2 changed files with 59 additions and 39 deletions

View File

@ -1,7 +1,9 @@
package main
import (
"fmt"
"errors"
"io"
"os"
"path/filepath"
"strings"
"time"
@ -67,56 +69,52 @@ 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
// }
written, 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
}
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)
b.WriteString(color.BlueString("\t done (copying %s took %s)\n", FormatBytes(written), time.Since(start).Truncate(100*time.Millisecond)))
return nil
}

22
utils.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
"git.milar.in/milarin/gmath"
)
func FormatBytes[T gmath.Integer](bytes T) string {
value := float64(bytes)
if value/1000000000000 >= 1000000000000 {
return fmt.Sprint("%.02fT", value/1000000000000)
} else if value/1000000000 >= 1000000000 {
return fmt.Sprint("%.02fG", value/1000000000)
} else if value/1000000 >= 1000000 {
return fmt.Sprint("%.02fM", value/1000000)
} else if value/1000 >= 1000 {
return fmt.Sprint("%.02fK", value/1000)
} else {
return fmt.Sprint("%.02fB", value)
}
}