Skip to content

Commit

Permalink
feat: Starting to add cover edition
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jul 22, 2022
1 parent 3c7c35f commit 97e3c8b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/fibr/templates/seo.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

{{ if $needCover }}
{{ if .Cover.Img }}
<meta property="og:image" content="{{ publicURL (.Request.AbsoluteURL .Cover.Img.Name) }}?thumbnail{{ if .Cover.Scale }}&scale={{ .Cover.Scale }}{{ end }}">
<meta property="og:image" content="{{ publicURL (.Request.AbsoluteURL .Cover.Img.Name) }}?thumbnail">
<meta property="og:image:alt" content="Thumbnail of folder">
<meta property="og:image:type" content="image/webp">
<meta property="og:image:height" content="{{ .Cover.ImgHeight }}">
Expand Down
35 changes: 8 additions & 27 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ const (
uint32max = (1 << 32) - 1
)

func (a App) getCover(ctx context.Context, request provider.Request, files []absto.Item) map[string]any {
for _, file := range files {
if a.thumbnailApp.HasThumbnail(ctx, file, thumbnail.SmallSize) {
return map[string]any{
"Img": provider.StorageToRender(file, request),
"ImgHeight": thumbnail.SmallSize,
"ImgWidth": thumbnail.SmallSize,
}
}
}

return nil
}

func (a App) list(ctx context.Context, request provider.Request, message renderer.Message, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx, end := tracer.StartSpan(ctx, a.tracer, "list")
defer end()
Expand All @@ -53,12 +39,11 @@ func (a App) list(ctx context.Context, request provider.Request, message rendere
}
})

var hasMap bool
var directoryAggregate provider.Aggregate
wg.Go(func() {
if aggregate, err := a.exifApp.GetAggregateFor(ctx, item); err != nil && !absto.IsNotExist(err) {
var err error
if directoryAggregate, err = a.exifApp.GetAggregateFor(ctx, item); err != nil && !absto.IsNotExist(err) {
logger.WithField("fn", "crud.List").WithField("item", request.Path).Error("unable to get aggregate: %s", err)
} else if len(aggregate.Location) != 0 {
hasMap = true
}
})

Expand All @@ -81,15 +66,15 @@ func (a App) list(ctx context.Context, request provider.Request, message rendere

wg.Wait()

hasThumbnail, hasStory, cover := a.enrichThumbnail(ctx, request, items, thumbnails)
hasThumbnail, hasStory, cover := a.enrichThumbnail(ctx, request, directoryAggregate, items, thumbnails)

content := map[string]any{
"Paths": getPathParts(request),
"Files": items,
"Cover": cover,
"Request": request,
"Message": message,
"HasMap": hasMap,
"HasMap": len(directoryAggregate.Location),
"HasThumbnail": hasThumbnail,
"HasStory": hasStory,
"ChunkUpload": a.chunkUpload,
Expand All @@ -106,7 +91,7 @@ func (a App) list(ctx context.Context, request provider.Request, message rendere
return renderer.NewPage("files", http.StatusOK, content), nil
}

func (a App) enrichThumbnail(ctx context.Context, request provider.Request, items []provider.RenderItem, thumbnails map[string]absto.Item) (hasThumbnail bool, hasStory bool, cover map[string]any) {
func (a App) enrichThumbnail(ctx context.Context, request provider.Request, directoryAggregate provider.Aggregate, items []provider.RenderItem, thumbnails map[string]absto.Item) (hasThumbnail bool, hasStory bool, cover cover) {
renderWithThumbnail := request.Display == provider.GridDisplay

for index, item := range items {
Expand All @@ -116,12 +101,8 @@ func (a App) enrichThumbnail(ctx context.Context, request provider.Request, item

hasThumbnail = true

if cover == nil {
cover = map[string]any{
"Img": item,
"ImgHeight": thumbnail.SmallSize,
"ImgWidth": thumbnail.SmallSize,
}
if cover.IsZero() || (len(directoryAggregate.Cover) != 0 && cover.Img.Name != directoryAggregate.Cover) {
cover = newCover(item, thumbnail.SmallSize)
}

if !hasStory {
Expand Down
38 changes: 38 additions & 0 deletions pkg/crud/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package crud

import (
"context"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
)

type cover struct {
Img provider.RenderItem
ImgHeight uint64
ImgWidth uint64
}

func newCover(item provider.RenderItem, size uint64) cover {
return cover{
Img: item,
ImgHeight: size,
ImgWidth: size,
}
}

func (c cover) IsZero() bool {
return len(c.Img.Item.Name) == 0
}

func (a App) getCover(ctx context.Context, request provider.Request, files []absto.Item) (output cover) {
for _, file := range files {
if a.thumbnailApp.HasThumbnail(ctx, file, thumbnail.SmallSize) {
output = newCover(provider.StorageToRender(file, request), thumbnail.SmallSize)
return
}
}

return
}
23 changes: 12 additions & 11 deletions pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ func (a App) story(r *http.Request, request provider.Request, item absto.Item, f
defer end()

items := make([]provider.StoryItem, 0, len(files))
var cover map[string]any
var cover cover
var hasMap bool

for _, item := range files {
if cover == nil {
cover = map[string]any{
"Img": provider.StorageToRender(item, request),
"ImgHeight": thumbnail.SmallSize,
"ImgWidth": thumbnail.SmallSize,
}
directoryAggregate, err := a.exifApp.GetAggregateFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("fn", "crud.story").WithField("item", request.Path).Error("unable to get aggregate: %s", err)
}

for _, file := range files {
if cover.IsZero() || (len(directoryAggregate.Cover) != 0 && cover.Img.Name != directoryAggregate.Cover) {
cover = newCover(provider.StorageToRender(file, request), thumbnail.SmallSize)
}

exif, err := a.exifApp.GetExifFor(ctx, item)
exif, err := a.exifApp.GetExifFor(ctx, file)
if err != nil {
logger.WithField("item", item.Pathname).Error("unable to get exif: %s", err)
logger.WithField("item", file.Pathname).Error("unable to get exif: %s", err)
}

if !request.Share.Story && !hasMap && exif.Geocode.HasCoordinates() {
hasMap = true
}

items = append(items, provider.StorageToStory(item, request, exif))
items = append(items, provider.StorageToStory(file, request, exif))
}

return renderer.NewPage("story", http.StatusOK, map[string]any{
Expand Down
3 changes: 2 additions & 1 deletion pkg/provider/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
type Aggregate struct {
Start time.Time `json:"start,omitempty"`
End time.Time `json:"end,omitempty"`
Location string `json:"location"`
Location string `json:"location,omitempty"`
Cover string `json:"cover,omitempty"`
}

// ExifResponse from AMQP
Expand Down

0 comments on commit 97e3c8b

Please sign in to comment.