Skip to content

Commit

Permalink
fix: video swaping
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarno committed Jan 7, 2025
1 parent cb9be63 commit 47c115d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
3 changes: 1 addition & 2 deletions internal/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

const defaultPort = 8080
const maxPortNumber = 65536
const maxDefaultCPUs = 8

type RawConfig struct {
Dir string
Expand Down Expand Up @@ -130,7 +129,7 @@ func (c Config) Print() {
func DefaultRawConfig() RawConfig {
return RawConfig{
Dir: os.Getenv("PWD"),
Parallel: min(runtime.NumCPU(), maxDefaultCPUs),
Parallel: runtime.NumCPU(),
Port: defaultPort,
Video: true,
Images: true,
Expand Down
2 changes: 2 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io/fs"
"net"
"net/http"
//nolint:gosec
_ "net/http/pprof"
"os"
"time"

Expand Down
42 changes: 26 additions & 16 deletions pkg/preview/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@ import (
"errors"
"fmt"
"log/slog"
"runtime"
"os"
"strings"

"github.com/davidbyttow/govips/v2/vips"
)

const (
maxWidthHeight = 256
vipsMaxCacheMem = 16 * 1024 * 1024
vipsMaxCacheSize = 16 * 1024 * 1024
vipsMaxCacheFiles = 4
jpegShrinkFactor = 8
maxWidthHeight = 256
jpegShrinkFactor = 8

imageDefault = iota
imageCollage
)

var (
ErrVipsLoadImage = errors.New("failed load image")
ErrVipsResizeImage = errors.New("failed resize image")
ErrImageDownScale = errors.New("failed to downscale the image")
ErrImageExport = errors.New("failed export the image")
ErrVipsLoadImage = errors.New("failed load image")
ErrVipsResizeImage = errors.New("failed resize image")
ErrImageDownScale = errors.New("failed to downscale the image")
ErrImageColorSpaceTransform = errors.New("failed to transform the image's color space")
ErrImageExport = errors.New("failed export the image")
)

//nolint:gochecknoinits
func init() {
os.Setenv("MALLOC_ARENA_MAX", "2")
vips.LoggingSettings(func(domain string, level vips.LogLevel, msg string) {
slog.Info(msg, slog.String("domain", domain), slog.Int("level", int(level)), slog.String("source", "VIPS"))
domainSlog := slog.String("source", domain)

switch level {
case vips.LogLevelCritical:
case vips.LogLevelError:
slog.Error(msg, domainSlog)
case vips.LogLevelDebug:
slog.Debug(msg, domainSlog)
case vips.LogLevelWarning:
slog.Warn(msg, domainSlog)
case vips.LogLevelInfo:
case vips.LogLevelMessage:
default:
slog.Info(msg, domainSlog)
}
}, vips.LogLevelError)
vips.Startup(&vips.Config{
ConcurrencyLevel: runtime.NumCPU(),
MaxCacheMem: vipsMaxCacheMem,
MaxCacheSize: vipsMaxCacheSize,
MaxCacheFiles: vipsMaxCacheFiles,
MaxCacheFiles: 1,
})
}

Expand Down Expand Up @@ -99,7 +109,7 @@ func downScale(image *vips.ImageRef, imageType int) error {
}
}

if err := image.Resize(scale, vips.KernelNearest); err != nil {
if err := image.Resize(scale, vips.KernelAuto); err != nil {
return fmt.Errorf("%w: %w", ErrVipsResizeImage, err)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/preview/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ func TestPreviewImage(t *testing.T) {
{
Name: ".jpg",
SourcePath: "../../test/img/image.jpg",
DataLength: 11312,
DataLength: 10332,
Width: 750,
Height: 1000,
Hash: "a7dfa90ec80959dd4239a48b394c5e0e98aaabc57710f8691bf0244f6d15ce23",
Hash: "182e0008de9bdd21a8af91ce6e6cbc57197e604886de71791133147f217c6fa0",
},
{
Name: ".gif",
SourcePath: "../../test/sample_minions.gif",
DataLength: 8970,
DataLength: 7794,
Width: 400,
Height: 200,
Hash: "118fbbdad8bfe93ac4982dfac7f26e418b5354eece720a78359e3ca1bc209757",
Hash: "846c829de9f1e4490e7025dcba99c903a7cb93289cd669b28790ffbc838e3847",
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/preview/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPreview(t *testing.T) {
}{
{
source: mockSource{image: true, path: "../../test/img/image.jpg"},
dataHash: "a7dfa90ec80959dd4239a48b394c5e0e98aaabc57710f8691bf0244f6d15ce23",
dataHash: "182e0008de9bdd21a8af91ce6e6cbc57197e604886de71791133147f217c6fa0",
},
{
source: mockSource{video: true, path: "../../test/sample.mp4"},
Expand Down
3 changes: 2 additions & 1 deletion web/js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Popover } from 'bootstrap';
require("fslightbox")
import { videoInit } from "./video";
import { videoInit, initVideoTransitions } from "./video";
import { zoom } from "./zoom"
import { observeDOM } from "./dom";
import { onSearch, highlightSearchResults } from "./search"
Expand Down Expand Up @@ -34,6 +34,7 @@ const initLightBox = () => {
fsLightbox.props.loadOnlyCurrentSource = true;
fsLightbox.props.onOpen = () => {
const videosItems = Array.from(document.querySelectorAll("video"))
videosItems.forEach(initVideoTransitions)
const streams = videosItems.filter(e => e.getAttribute("src").includes("hls"))
streams.forEach(videoInit)
}
Expand Down
6 changes: 3 additions & 3 deletions web/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export const videoInit = (video) => {
video.setAttribute("poster", originSrc.replace("origin", "preview"))


if (video.canPlayType('application/vnd.apple.mpegurl')) {
return
} else if (Hls.isSupported()) {
if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource(video.getAttribute("src"));
hls.attachMedia(video);
hls.on(Hls.Events.BUFFER_CREATED, () => video.removeAttribute("poster"));
}
}

export const initVideoTransitions = (video) => {
video.parentElement.onpointerdown = (e) => {
e.stopImmediatePropagation();
}
Expand Down

0 comments on commit 47c115d

Please sign in to comment.