mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 20:24:27 -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.
|
// Get a specific attachment.
|
||||||
//
|
//
|
||||||
// Path: /:sha/attachment/:name
|
// Path: /attachment/:name
|
||||||
func (h *Handler) GetAttachment(c echo.Context) error {
|
func (h *Handler) GetAttachment(c echo.Context) error {
|
||||||
sha := c.Param("sha")
|
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
if err := SanitizePath(sha); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := SanitizePath(name); err != nil {
|
if err := SanitizePath(name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -317,8 +313,8 @@ func main() {
|
|||||||
e.GET("/info", h.GetInfo)
|
e.GET("/info", h.GetInfo)
|
||||||
e.GET("/thumbnails.png", h.GetThumbnails)
|
e.GET("/thumbnails.png", h.GetThumbnails)
|
||||||
e.GET("/thumbnails.vtt", h.GetThumbnailsVtt)
|
e.GET("/thumbnails.vtt", h.GetThumbnailsVtt)
|
||||||
e.GET("/:sha/attachment/:name", h.GetAttachment)
|
e.GET("/attachment/:name", h.GetAttachment)
|
||||||
e.GET("/:sha/subtitle/:name", h.GetSubtitle)
|
e.GET("/subtitle/:name", h.GetSubtitle)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":7666"))
|
e.Logger.Fatal(e.Start(":7666"))
|
||||||
}
|
}
|
||||||
|
@ -18,25 +18,18 @@ func NewExtractor() *Extractor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) Extract(sha string) (<-chan struct{}, bool) {
|
func (e *Extractor) Extract(path string, subs *[]Subtitle) (<-chan struct{}, error) {
|
||||||
e.lock.RLock()
|
sha, err := getHash(path)
|
||||||
existing, ok := e.extracted[sha]
|
if err != nil {
|
||||||
e.lock.RUnlock()
|
return nil, err
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := make(chan struct{})
|
|
||||||
e.lock.Lock()
|
e.lock.Lock()
|
||||||
|
existing, ok := e.extracted[sha]
|
||||||
|
if ok {
|
||||||
|
return existing, nil
|
||||||
|
}
|
||||||
|
ret := make(chan struct{})
|
||||||
e.extracted[sha] = ret
|
e.extracted[sha] = ret
|
||||||
e.lock.Unlock()
|
e.lock.Unlock()
|
||||||
|
|
||||||
@ -73,5 +66,5 @@ func (e *Extractor) RunExtractor(path string, sha string, subs *[]Subtitle) <-ch
|
|||||||
close(ret)
|
close(ret)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return ret
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package src
|
package src
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
@ -70,15 +68,10 @@ func (t *ThumbnailsCreator) ExtractThumbnail(path string, name string) (string,
|
|||||||
|
|
||||||
func extractThumbnail(path string, name string) (string, error) {
|
func extractThumbnail(path string, name string) (string, error) {
|
||||||
defer printExecTime("extracting thumbnails for %s", path)()
|
defer printExecTime("extracting thumbnails for %s", path)()
|
||||||
info, err := os.Stat(path)
|
sha, err := getHash(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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)
|
out := fmt.Sprintf("%s/%s", Settings.Metadata, sha)
|
||||||
os.MkdirAll(out, 0o755)
|
os.MkdirAll(out, 0o755)
|
||||||
sprite_path := fmt.Sprintf("%s/sprite.png", out)
|
sprite_path := fmt.Sprintf("%s/sprite.png", out)
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package src
|
package src
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,3 +18,15 @@ func printExecTime(message string, args ...any) func() {
|
|||||||
log.Printf("%s finished in %s", msg, time.Since(start))
|
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