mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Attachments handling
This commit is contained in:
parent
a8b0eeb973
commit
f5be4a8b99
@ -193,14 +193,10 @@ func (h *Handler) GetInfo(c echo.Context) error {
|
||||
//
|
||||
// Get a specific attachment.
|
||||
//
|
||||
// Path: /:sha/attachment/:name
|
||||
// Path: /attachment/:name
|
||||
func (h *Handler) GetAttachment(c echo.Context) error {
|
||||
sha := c.Param("sha")
|
||||
name := c.Param("name")
|
||||
|
||||
if err := SanitizePath(sha); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := SanitizePath(name); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -317,8 +313,8 @@ func main() {
|
||||
e.GET("/info", h.GetInfo)
|
||||
e.GET("/thumbnails.png", h.GetThumbnails)
|
||||
e.GET("/thumbnails.vtt", h.GetThumbnailsVtt)
|
||||
e.GET("/:sha/attachment/:name", h.GetAttachment)
|
||||
e.GET("/:sha/subtitle/:name", h.GetSubtitle)
|
||||
e.GET("/attachment/:name", h.GetAttachment)
|
||||
e.GET("/subtitle/:name", h.GetSubtitle)
|
||||
|
||||
e.Logger.Fatal(e.Start(":7666"))
|
||||
}
|
||||
|
@ -18,25 +18,18 @@ func NewExtractor() *Extractor {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Extractor) Extract(sha string) (<-chan struct{}, bool) {
|
||||
e.lock.RLock()
|
||||
existing, ok := e.extracted[sha]
|
||||
e.lock.RUnlock()
|
||||
|
||||
if ok {
|
||||
return existing, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (e *Extractor) RunExtractor(path string, sha string, subs *[]Subtitle) <-chan struct{} {
|
||||
existing, ok := e.Extract(sha)
|
||||
if ok {
|
||||
return existing
|
||||
func (e *Extractor) Extract(path string, subs *[]Subtitle) (<-chan struct{}, error) {
|
||||
sha, err := getHash(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make(chan struct{})
|
||||
e.lock.Lock()
|
||||
existing, ok := e.extracted[sha]
|
||||
if ok {
|
||||
return existing, nil
|
||||
}
|
||||
ret := make(chan struct{})
|
||||
e.extracted[sha] = ret
|
||||
e.lock.Unlock()
|
||||
|
||||
@ -73,5 +66,5 @@ func (e *Extractor) RunExtractor(path string, sha string, subs *[]Subtitle) <-ch
|
||||
close(ret)
|
||||
}()
|
||||
|
||||
return ret
|
||||
return ret, nil
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package src
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
@ -70,15 +68,10 @@ func (t *ThumbnailsCreator) ExtractThumbnail(path string, name string) (string,
|
||||
|
||||
func extractThumbnail(path string, name string) (string, error) {
|
||||
defer printExecTime("extracting thumbnails for %s", path)()
|
||||
info, err := os.Stat(path)
|
||||
sha, err := getHash(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h := sha1.New()
|
||||
h.Write([]byte(path))
|
||||
h.Write([]byte(info.ModTime().String()))
|
||||
sha := hex.EncodeToString(h.Sum(nil))
|
||||
|
||||
out := fmt.Sprintf("%s/%s", Settings.Metadata, sha)
|
||||
os.MkdirAll(out, 0o755)
|
||||
sprite_path := fmt.Sprintf("%s/sprite.png", out)
|
||||
|
@ -1,8 +1,11 @@
|
||||
package src
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -15,3 +18,15 @@ func printExecTime(message string, args ...any) func() {
|
||||
log.Printf("%s finished in %s", msg, time.Since(start))
|
||||
}
|
||||
}
|
||||
|
||||
func getHash(path string) (string, error) {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h := sha1.New()
|
||||
h.Write([]byte(path))
|
||||
h.Write([]byte(info.ModTime().String()))
|
||||
sha := hex.EncodeToString(h.Sum(nil))
|
||||
return sha, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user