Skip to content

Commit

Permalink
feature: default img and video icons
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarno committed Nov 13, 2024
1 parent 3a1ec33 commit 9be57d8
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 23 deletions.
20 changes: 17 additions & 3 deletions cmd/tinytune/tinytune.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -142,7 +143,7 @@ func main() {
},
Action: func(ctx *cli.Context) error {
if ctx.Args().Len() != 0 {
config.dir = ctx.Args().First()
config.dir = ctx.Args().Get(ctx.Args().Len() - 1)
}
start(config)

Expand All @@ -157,12 +158,25 @@ func start(config Config) {
slog.SetDefault(logging.Get())

ctx := gracefulShutdownCtx()

slog.Info(
"TinyTune",
slog.String("version", Version),
slog.Bool("image-processing", config.imageProcessing),
slog.Bool("video-processing", config.videoProcessing),
slog.Bool("acceleration", config.acceleration),
slog.Int64("max-new-images", config.maxNewImageItems),
slog.Int64("max-new-videos", config.maxNewVideoItems),
)

indexFilePath := filepath.Join(config.dir, IndexFileName)

files, err := internal.NewCrawlerOS(config.dir).Scan(indexFilePath)
internal.PanicError(err)

indexFile, err := os.OpenFile(indexFilePath, os.O_RDWR|os.O_CREATE, fs.ModeAppend)
indexFileRights := 0755

indexFile, err := os.OpenFile(indexFilePath, os.O_RDWR|os.O_CREATE, fs.FileMode(indexFileRights))
internal.PanicError(err)
defer indexFile.Close()

Expand Down Expand Up @@ -249,7 +263,7 @@ func idGenerator(p index.FileMeta) (string, error) {
idSource := []byte(fmt.Sprintf("%s%s", p.RelativePath(), p.ModTime()))
fileID := sha256.Sum256(idSource)

return string(fileID[:10]), nil
return hex.EncodeToString(fileID[:5]), nil
}

func gracefulShutdownCtx() context.Context {
Expand Down
3 changes: 2 additions & 1 deletion internal/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"runtime"

"github.com/alxarno/tinytune/pkg/index"
"github.com/alxarno/tinytune/pkg/preview"
"github.com/davidbyttow/govips/v2/vips"
)
Expand Down Expand Up @@ -37,7 +38,7 @@ func init() {
}

func ImagePreview(path string) (preview.Data, error) {
preview := preview.Data{Resolution: "0x0"}
preview := preview.Data{Resolution: "0x0", ContentType: index.ContentTypeImage}

image, err := vips.NewImageFromFile(path)
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions internal/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ func NewPreviewer(opts ...PreviewerOption) (*Previewer, error) {

func (p Previewer) Pull(path string) (preview.Data, error) {
contentType := p.ContentType(path)
defaultPreview := preview.Data{ContentType: index.ContentTypeOther}
defaultPreview := preview.Data{ContentType: contentType, Resolution: "0x0"}

if contentType == index.ContentTypeImage && p.image {
preview, err := ImagePreview(path)
if err != nil {
return defaultPreview, err
}

preview.ContentType = contentType

return preview, nil
}

Expand All @@ -92,16 +90,25 @@ func (p Previewer) Pull(path string) (preview.Data, error) {
return defaultPreview, err
}

preview.ContentType = contentType

return preview, nil
}

if contentType == index.ContentTypeVideo {
//default resolution for video player
defaultPreview.Resolution = "1280x720"
}

return defaultPreview, nil
}

func (p Previewer) ContentType(path string) int {
ext := filepath.Ext(path)
minExtensionLength := 2

if len(ext) < minExtensionLength {
return index.ContentTypeOther
}

if slices.Contains(p.imageFormats, ext[1:]) {
return index.ContentTypeImage
}
Expand Down
12 changes: 7 additions & 5 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (s *Server) indexHandler() http.Handler {

if err := s.templates["index.html"].ExecuteTemplate(w, "index", data); err != nil {
w.WriteHeader(http.StatusInternalServerError)

return
}
})
}
Expand Down Expand Up @@ -224,16 +226,15 @@ func (s *Server) searchHandler() http.Handler {
func (s *Server) previewHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := s.source.PullPreview(r.PathValue("fileID"))
if err != nil {
if err != nil || len(data) == 0 {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404")

return
}
// Cache for week
w.WriteHeader(http.StatusOK)
w.Header().Add("Cache-Control", "max-age=604800")
w.Header().Add("Content-Type", http.DetectContentType(data))
w.WriteHeader(http.StatusOK)
logWriteErr(w.Write(data))
})
}
Expand Down Expand Up @@ -275,12 +276,13 @@ func WithDebug(debug bool) ServerOption {

func NewServer(ctx context.Context, opts ...ServerOption) *Server {
server := &Server{}
server.loadTemplates()

for _, opt := range opts {
opt(server)
}

server.loadTemplates()

mux := http.NewServeMux()
serverTimeoutSeconds := 30
httpServer := &http.Server{
Expand All @@ -299,7 +301,7 @@ func NewServer(ctx context.Context, opts ...ServerOption) *Server {
mux.Handle("GET /origin/{fileID}/", chain.Then(server.originHandler()))
mux.Handle("GET /static/", chain.Then(staticHandler))

go PanicError(httpServer.ListenAndServe())
go func() { PanicError(httpServer.ListenAndServe()) }()

return server
}
1 change: 1 addition & 0 deletions pkg/httputil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func LoggingHandler(handler http.Handler) http.Handler {
"GET 200": ansiBrightGreen,
"GET 206": ansiBrightBlue,
"GET 302": ansiBrightYellow,
"GET 303": ansiBrightYellow,
}
logger := logging.Get(logging.WithOption(func(opt *tint.Options) {
opt.ReplaceAttr = func(_ []string, attribute slog.Attr) slog.Attr {
Expand Down
6 changes: 5 additions & 1 deletion pkg/httputil/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ func (rw *responseWriter) Write(b []byte) (int, error) {
n, err := rw.ResponseWriter.Write(b)
rw.size += n

return n, fmt.Errorf("%w:%w", ErrWrite, err)
if err != nil {
return n, fmt.Errorf("%w:%w", ErrWrite, err)
}

return n, nil
}
7 changes: 7 additions & 0 deletions web/templates/icon-file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ define "icon-file"}}<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" width="80" height="100" viewBox="0 0 24 24" version="1.1">
<g transform="translate(0 -1028.4)">
<path d="m5 1030.4c-1.1046 0-2 0.9-2 2v8 4 6c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-6-4-4l-6-6h-10z" fill="#D1D1D1"/>
<path d="m5 1029.4c-1.1046 0-2 0.9-2 2v8 4 6c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-6-4-4l-6-6h-10z" fill="#ffffff"/>
<path d="m21 1035.4-6-6v4c0 1.1 0.895 2 2 2h4z" fill="#D1D1D1"/>
</g>
</svg>{{end}}
1 change: 1 addition & 0 deletions web/templates/icon-img.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ define "icon-image"}}<svg width="80" height="100" version="1.1" viewBox="0 0 24 24" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><g transform="translate(0,-1028.4)"><path d="m5 1030.4c-1.1046 0-2 0.9-2 2v18c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-14l-6-6z" fill="#008da5"/><path d="m5 1029.4c-1.1046 0-2 0.9-2 2v18c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-14l-6-6z" fill="#0dbbd3"/><path d="m21 1035.4-6-6v4c0 1.1 0.895 2 2 2z" fill="#d1d1d1"/></g><g transform="matrix(.42717 0 0 .43004 6.874 6.8397)" fill="#fff"><path d="m6.205 3h11.59c1.114 0 1.519 0.116 1.926 0.334s0.727 0.538 0.945 0.945 0.334 0.811 0.334 1.926v7.51l-4.391-4.053a1.5 1.5 0 0 0-2.265 0.27l-3.13 4.695-2.303-1.48a1.5 1.5 0 0 0-1.96 0.298l-3.946 4.705a12.98 12.98 0 0 1-5e-3 -0.355v-11.59c0-1.115 0.116-1.519 0.334-1.926s0.538-0.727 0.945-0.945 0.811-0.334 1.926-0.334zm9.477 8.53 5.318 4.907v1.357c0 1.114-0.116 1.519-0.334 1.926a2.272 2.272 0 0 1-0.945 0.945c-0.407 0.218-0.811 0.334-1.926 0.334h-11.59c-1.115 0-1.519-0.116-1.926-0.334a2.305 2.305 0 0 1-0.485-0.345l4.406-5.253 2.346 1.508a1.5 1.5 0 0 0 2.059-0.43l3.077-4.616zm-7.694-5.53c-1.11 0-1.988 0.832-1.988 1.988 0 1.157 0.879 2.012 1.988 2.012 1.133 0 2.012-0.855 2.012-2.012 0-1.156-0.879-1.988-2.012-1.988z" clip-rule="evenodd" fill="#fff" fill-rule="evenodd"/></g></svg>{{ end }}
1 change: 1 addition & 0 deletions web/templates/icon-video.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ define "icon-video"}}<svg width="80" height="100" version="1.1" viewBox="0 0 24 24" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><g transform="translate(0,-1028.4)"><path d="m5 1030.4c-1.1046 0-2 0.9-2 2v18c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-14l-6-6z" fill="#a50055"/><path d="m5 1029.4c-1.1046 0-2 0.9-2 2v18c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-14l-6-6z" fill="#d30d83"/><path d="m21 1035.4-6-6v4c0 1.1 0.895 2 2 2z" fill="#d1d1d1"/></g><g transform="matrix(.42717 0 0 .43004 6.874 6.8397)" display="none" fill="#fff"><path d="m6.205 3h11.59c1.114 0 1.519 0.116 1.926 0.334s0.727 0.538 0.945 0.945 0.334 0.811 0.334 1.926v7.51l-4.391-4.053a1.5 1.5 0 0 0-2.265 0.27l-3.13 4.695-2.303-1.48a1.5 1.5 0 0 0-1.96 0.298l-3.946 4.705a12.98 12.98 0 0 1-5e-3 -0.355v-11.59c0-1.115 0.116-1.519 0.334-1.926s0.538-0.727 0.945-0.945 0.811-0.334 1.926-0.334zm9.477 8.53 5.318 4.907v1.357c0 1.114-0.116 1.519-0.334 1.926a2.272 2.272 0 0 1-0.945 0.945c-0.407 0.218-0.811 0.334-1.926 0.334h-11.59c-1.115 0-1.519-0.116-1.926-0.334a2.305 2.305 0 0 1-0.485-0.345l4.406-5.253 2.346 1.508a1.5 1.5 0 0 0 2.059-0.43l3.077-4.616zm-7.694-5.53c-1.11 0-1.988 0.832-1.988 1.988 0 1.157 0.879 2.012 1.988 2.012 1.133 0 2.012-0.855 2.012-2.012 0-1.156-0.879-1.988-2.012-1.988z" clip-rule="evenodd" fill="#fff" fill-rule="evenodd"/></g><g transform="matrix(.014648 0 0 .014648 8.25 8.25)" fill="#fff"><path class="st0" d="m464 56v40h-40v-40h-336v40h-40v-40h-48v400h48v-40h40v40h336v-40h40v40h48v-400zm-376 298.67h-40v-64h40zm0-133.34h-40v-64h40zm220.09 44.719-101.73 60.734c-0.75 0.438-1.656 0.469-2.406 0.031-0.734-0.422-1.203-1.219-1.203-2.094v-121.44c0-0.859 0.469-1.656 1.203-2.094 0.75-0.406 1.656-0.391 2.406 0.031l101.73 60.75c0.719 0.406 1.156 1.203 1.156 2.031 0 0.845-0.437 1.626-1.156 2.048zm155.91 88.625h-40v-64h40zm0-133.34h-40v-64h40z" fill="#fff"/></g></svg>{{ end }}
8 changes: 1 addition & 7 deletions web/templates/item-file.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{{define "file-item"}}<figure class="figure dir-list-item">
<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" width="80" height="100" viewBox="0 0 24 24" version="1.1">
<g transform="translate(0 -1028.4)">
<path d="m5 1030.4c-1.1046 0-2 0.9-2 2v8 4 6c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-6-4-4l-6-6h-10z" fill="#D1D1D1"/>
<path d="m5 1029.4c-1.1046 0-2 0.9-2 2v8 4 6c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-6-4-4l-6-6h-10z" fill="#ffffff"/>
<path d="m21 1035.4-6-6v4c0 1.1 0.895 2 2 2h4z" fill="#D1D1D1"/>
</g>
</svg>
{{ template "icon-file" .}}
<figcaption class="figure-caption">{{ .Name }}</figcaption>
</figure>{{end}}
6 changes: 5 additions & 1 deletion web/templates/item-image.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{{define "image-item"}}<a href="/origin/{{ .ID }}" data-loading="lazy" class="image-lightbox" hx-boost="false" data-fslightbox="gallery" data-pswp-width="{{ width .Resolution }}" data-pswp-height="{{ height .Resolution }}"><figure class="figure dir-list-item">
{{define "image-item"}}<a href="/origin/{{ .ID }}" data-loading="lazy" class="image-lightbox" hx-boost="false" data-fslightbox="gallery"><figure class="figure dir-list-item">
{{ if eq .Resolution "0x0" }}
{{ template "icon-image" .}}
{{ else }}
<div class="wrap">
<div class="spacer"></div>
<img src="/preview/{{ .ID }}" id="img-{{.ID}}" class="figure-img img-fluid rounded" alt="{{ .Name }}" hx-preserve>
</div>
{{ end }}
<figcaption class="figure-caption">{{ .Name }}</figcaption>
</figure></a>{{end}}
4 changes: 4 additions & 0 deletions web/templates/item-video.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{define "video-item"}}<a href="/origin/{{ .ID }}" class="image-lightbox" hx-boost="false" data-extension="{{ ext .Name }}" data-width="{{ width .Resolution}}" data-height="{{ height .Resolution}}"><figure class="figure dir-list-item">
{{ if eq .Preview.Length 0 }}
{{ template "icon-video" .}}
{{ else }}
<div class="wrap" style="--origin-width: {{ width .Resolution }};--origin-height: {{ height .Resolution }};">
<div class="spacer"></div>
<img
Expand All @@ -11,5 +14,6 @@
>
<span class="duration rounded" id="duration-{{ .ID }}">{{ dur .Duration }}</span>
</div>
{{ end }}
<figcaption class="figure-caption">{{ .Name }}</figcaption>
</figure></a>{{end}}

0 comments on commit 9be57d8

Please sign in to comment.