Skip to content

Commit

Permalink
refactor: meta's resolution attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarno committed Nov 17, 2024
1 parent c513111 commit 2857659
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 46 deletions.
10 changes: 10 additions & 0 deletions e2e/index.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@
<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>
<figcaption class="figure-caption">test.index.tinytune</figcaption>
</figure></li>
<li class="col"><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>
<figcaption class="figure-caption">sample.txt</figcaption>
</figure></li>
Expand Down
Binary file modified e2e/preview-video.fixture
Binary file not shown.
14 changes: 2 additions & 12 deletions internal/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
func loadTemplates(src fs.FS, streaming map[string]struct{}) map[string]*template.Template {
templates := make(map[string]*template.Template)
funcs := template.FuncMap{
"ext": extension,
"width": width,
"height": height,
"ext": extension,
"eqMinusOne": func(x int, y int) bool {
return x == y-1
},
Expand All @@ -29,20 +27,12 @@ func loadTemplates(src fs.FS, streaming map[string]struct{}) map[string]*templat
func extension(name string) string {
extension := path.Ext(name)
if extension != "" {
return extension[1:]
return strings.ToLower(extension[1:])
}

return ""
}

func width(res string) string {
return strings.Split(res, "x")[0]
}

func height(res string) string {
return strings.Split(res, "x")[1]
}

func getStreaming(files map[string]struct{}) func(path string) bool {
return func(path string) bool {
_, ok := files[path]
Expand Down
4 changes: 3 additions & 1 deletion pkg/index/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func (ib *indexBuilder) loadFile(
}

metaItem.Duration = preview.Duration()
metaItem.Resolution = preview.Resolution()
width, height := preview.Resolution()
metaItem.Resolution.Width = width
metaItem.Resolution.Height = height
metaItem.Preview = PreviewLocation{
Length: uint32(len(preview.Data())),
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/index/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ func TestIndexBuilder(t *testing.T) {
require.NoError(err)
index, err := NewIndex(context.Background(), indexFile)
require.NoError(err)
require.Len(index.meta, 14)
require.Len(index.meta, 15)
sample, ok := index.meta["005f6b0265"]
require.True(ok)
require.True(sample.IsVideo())
require.Equal("sample_960x400_ocean_with_audio.flv", sample.Name)
require.Equal("960x400", sample.Resolution)
require.EqualValues(11590, sample.Preview.Length)
require.Equal(960, sample.Resolution.Width)
require.Equal(400, sample.Resolution.Height)
require.EqualValues(37618, sample.Preview.Length)
require.EqualValues(72046, sample.Preview.Offset)
require.EqualValues(7222114, sample.OriginSize)
require.LessOrEqual(sample.Preview.Offset+sample.Preview.Length, uint32(len(index.data)))
Expand Down
4 changes: 2 additions & 2 deletions pkg/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func (m mockPreviewData) Data() []byte {
return m.data
}

func (m mockPreviewData) Resolution() string {
return "0x0"
func (m mockPreviewData) Resolution() (int, int) {
return 0, 0
}

func (m mockPreviewData) Duration() time.Duration {
Expand Down
7 changes: 6 additions & 1 deletion pkg/index/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Meta struct {
IsDir bool `json:"isDir"`
Preview PreviewLocation `json:"preview"`
Duration time.Duration `json:"duration"`
Resolution string `json:"resolution"`
Resolution Resolution `json:"resolution"`
Type int `json:"type"`
}

Expand All @@ -41,6 +41,11 @@ type PreviewLocation struct {
Offset uint32 `json:"offset"`
}

type Resolution struct {
Width int `json:"width"`
Height int `json:"height"`
}

func (m *Meta) Size() int64 {
return m.OriginSize
}
Expand Down
17 changes: 10 additions & 7 deletions pkg/preview/data.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package preview

import "time"
import (
"time"
)

type Data interface {
Data() []byte
Duration() time.Duration
Resolution() string
Resolution() (int, int)
}

type data struct {
duration time.Duration
resolution string
data []byte
duration time.Duration
width int
height int
data []byte
}

func (d data) Duration() time.Duration {
return d.duration
}

func (d data) Resolution() string {
return d.resolution
func (d data) Resolution() (int, int) {
return d.width, d.height
}

func (d data) Data() []byte {
Expand Down
4 changes: 2 additions & 2 deletions pkg/preview/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func init() {
}

func imagePreview(path string) (data, error) {
preview := data{resolution: "0x0"}
preview := data{}

image, err := vips.NewImageFromFile(path)
if err != nil {
return preview, fmt.Errorf("%w: %w", ErrVipsNewImage, err)
}
defer image.Close()

preview.resolution = fmt.Sprintf("%dx%d", image.Width(), image.Height())
preview.width, preview.height = image.Width(), image.Height()

preview.data, err = downScale(image, imageDefault)

Expand Down
5 changes: 4 additions & 1 deletion pkg/preview/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func TestPreviewImage(t *testing.T) {
preview, err := imagePreview("../../test/image.jpg")
require.NoError(t, err)
assert.Len(preview.Data(), 8620)
assert.Equal("1527x898", preview.Resolution())
width, height := preview.Resolution()
assert.Equal(1527, width)
assert.Equal(898, height)

hash := sha256.Sum256(preview.Data())
assert.Equal("64de9c944a91c93e750d097577c8fc5992100a7bb186d376534e78705aefbbbd", hex.EncodeToString(hash[:]))
}
5 changes: 3 additions & 2 deletions pkg/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ifMaxPass(maxNewItems *int64) bool {

//nolint:cyclop,ireturn,nolintlint //it's very simple method...
func (p Previewer) Pull(src Source) (Data, error) {
defaultPreview := data{resolution: "0x0"}
defaultPreview := data{}

biggestThenMaxFileSize := p.maxFileSize != -1 && src.Size() > p.maxFileSize
toImage := src.IsImage() && p.image && ifMaxPass(&p.maxImages)
Expand Down Expand Up @@ -108,7 +108,8 @@ func (p Previewer) Pull(src Source) (Data, error) {

if src.IsVideo() {
// default resolution for video player
defaultPreview.resolution = "1280x720"
defaultPreview.width = 1280
defaultPreview.height = 720
}

return defaultPreview, nil
Expand Down
20 changes: 9 additions & 11 deletions pkg/preview/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,43 +183,41 @@ func getVideoStream(streams []probeStream) *probeStream {
return nil
}

func probeOutputFrames(a string) (string, time.Duration, error) {
func probeOutputFrames(a string) (int, int, time.Duration, error) {
data := probeData{}
resolution := "0x0"

if err := json.Unmarshal([]byte(a), &data); err != nil {
return resolution, 0, fmt.Errorf("%w: %w", ErrMetaInfoUnmarshal, err)
return 0, 0, 0, fmt.Errorf("%w: %w", ErrMetaInfoUnmarshal, err)
}

seconds, err := strconv.ParseFloat(data.Format.Duration, 64)
if err != nil {
return resolution, 0, fmt.Errorf("%w: %w", ErrMetaInfoDurationParse, err)
return 0, 0, 0, fmt.Errorf("%w: %w", ErrMetaInfoDurationParse, err)
}

videoStream := getVideoStream(data.Streams)
if videoStream == nil {
return resolution, 0, ErrVideoStreamNotFound
return 0, 0, 0, ErrVideoStreamNotFound
}

resolution = fmt.Sprintf("%dx%d", videoStream.Width, videoStream.Height)

return resolution, time.Duration(seconds) * time.Second, nil
return videoStream.Width, videoStream.Height, time.Duration(seconds) * time.Second, nil
}

func videoPreview(path string, params VideoParams) (data, error) {
preview := data{resolution: "0x0"}
preview := data{}

metaJSON, err := videoProbe(path, params.timeout)
if err != nil {
return preview, err
}

resolution, duration, err := probeOutputFrames(metaJSON)
width, height, duration, err := probeOutputFrames(metaJSON)
if err != nil {
return preview, err
}

preview.resolution = resolution
preview.width = width
preview.height = height
preview.duration = duration

if preview.data, err = produceVideoPreview(path, duration, params); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/preview/video_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func TestPreviewVideo(t *testing.T) {
require.NoError(t, err)
assert.Len(preview.Data(), 105634, fmt.Sprintf("got %d", len(preview.Data())))

Check failure on line 20 in pkg/preview/video_test.go

View workflow job for this annotation

GitHub Actions / build-and-test (1.22.x)

formatter: remove unnecessary fmt.Sprintf (testifylint)
assert.EqualValues(time.Second*5, preview.Duration())
assert.Equal("1280x720", preview.Resolution())
width, height := preview.Resolution()
assert.Equal(1280, width)
assert.Equal(720, height)

hash := sha256.Sum256(preview.Data())
assert.Equal("ab9e213afa42466148583b1ddab5bc00f8218ecac230cc7f8b6797e9e5179ba2", hex.EncodeToString(hash[:]))
}
Expand All @@ -31,6 +34,7 @@ func TestPreviewVideoFLV(t *testing.T) {
preview, err := videoPreview("../../test/video/sample_960x400_ocean_with_audio.flv", VideoParams{timeout: time.Minute})
require.NoError(t, err)
assert.Len(preview.Data(), 37618, fmt.Sprintf("got %d", len(preview.Data())))

Check failure on line 36 in pkg/preview/video_test.go

View workflow job for this annotation

GitHub Actions / build-and-test (1.22.x)

formatter: remove unnecessary fmt.Sprintf (testifylint)

hash := sha256.Sum256(preview.Data())
assert.Equal("30d4dcd9d15d17939a74bfbc7c4a19126a61369504d308bc11847f1bb45e4627", hex.EncodeToString(hash[:]))
}
Binary file modified test/test.index.tinytune
Binary file not shown.
2 changes: 1 addition & 1 deletion web/templates/item-image.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "image-item"}}<a href="/origin/{{ .ID }}" data-loading="lazy" class="image-lightbox" hx-boost="false" data-fslightbox="gallery" type="image"><figure class="figure dir-list-item">
{{ if eq .Resolution "0x0" }}
{{ if eq .Resolution.Width 0 }}
{{ template "icon-image" .}}
{{ else }}
<div class="wrap">
Expand Down
4 changes: 2 additions & 2 deletions web/templates/item-video.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{define "video-item"}}<a href="{{ if streaming .Path }}#/rts/{{.ID}}{{ else }}/origin/{{ .ID }}{{end}}" type="video" class="image-lightbox" hx-boost="false" data-extension="{{ if streaming .Path }}application/x-mpegURL{{else}}video/{{ ext .Name }}{{end}}" data-width="{{ width .Resolution}}" data-height="{{ height .Resolution}}"><figure class="figure dir-list-item">
{{define "video-item"}}<a href="{{ if streaming .Path }}#/rts/{{.ID}}{{ else }}/origin/{{ .ID }}{{end}}" type="video" class="image-lightbox" hx-boost="false" data-extension="{{ if streaming .Path }}application/x-mpegURL{{else}}video/{{ ext .Name }}{{end}}" data-width="{{ .Resolution.Width }}" data-height="{{ .Resolution.Height }}"><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="wrap" style="--origin-width: {{ .Resolution.Width }};--origin-height: {{ .Resolution.Height }};">
<div class="spacer"></div>
<img
id="video-{{ .ID }}"
Expand Down

0 comments on commit 2857659

Please sign in to comment.