Skip to content

Commit

Permalink
feat(story): Adding share with story mode only
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jul 10, 2022
1 parent 0199f74 commit 106f2db
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 39 deletions.
8 changes: 5 additions & 3 deletions cmd/fibr/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ <h2 class="small bg-grey no-margin full ellipsis">
{{ end }}
{{ else }}
</span>
<a class="padding" href="#search" download>
<img class="icon" src="{{ url "/svg/search?fill=" }}{{ if .Search }}limegreen{{ else }}aliceblue{{ end }}" alt="Search files">
</a>
{{ if not .Request.Share.Story }}
<a class="padding" href="#search" download>
<img class="icon" src="{{ url "/svg/search?fill=" }}{{ if .Search }}limegreen{{ else }}aliceblue{{ end }}" alt="Search files">
</a>
{{ end }}
{{ end }}
</header>

Expand Down
8 changes: 6 additions & 2 deletions cmd/fibr/templates/share-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ <h2 class="header">Share {{ .Name }}</h2>
<input type="hidden" name="method" value="POST" />

<p class="padding no-margin center">
<input id="edit" type="checkbox" name="edit" value="true" />
<label for="edit">Edit right</label>
<label for="rights">Rights</label>
<select id="rights" aria-label="Share's rights" name="rights">
<option value="edit">Edit</option>
<option value="read">Read only</option>
<option value="story">Story only</option>
</select>
</p>

<p class="padding no-margin">
Expand Down
3 changes: 3 additions & 0 deletions cmd/fibr/templates/share-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ <h2 class="flex flex-center header no-margin">
{{ if .Edit }}
<img class="icon" src="{{ url "/svg/edit?fill=aliceblue" }}" alt="Editable">
{{ end }}
{{ if .Story }}
<img class="icon" src="{{ url "/svg/image?fill=aliceblue" }}" alt="Story mode">
{{ end }}
{{ if .Password }}
<img class="icon" src="{{ url "/svg/lock?fill=aliceblue" }}" alt="Password protected">
{{ end }}
Expand Down
52 changes: 30 additions & 22 deletions cmd/fibr/templates/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

{{ $root := . }}

<link rel="preload" as="image" href="{{ url "/svg/list?fill=aliceblue" }}">
<link rel="preload" as="image" href="{{ url "/svg/th?fill=aliceblue" }}">
<link rel="preload" as="image" href="{{ url "/svg/image?fill=aliceblue" }}">
<link rel="preload" as="image" href="{{ url "/svg/link?fill=#272727" }}">

{{ template "search-modal" . }}
{{ if not .Request.Share.Story }}
<link rel="preload" as="image" href="{{ url "/svg/list?fill=aliceblue" }}">
<link rel="preload" as="image" href="{{ url "/svg/th?fill=aliceblue" }}">
<link rel="preload" as="image" href="{{ url "/svg/image?fill=aliceblue" }}">
{{ end }}

{{ if not .Request.Share.Story }}
{{ template "search-modal" . }}
{{ end }}

{{ template "items-style" . }}

{{ if .HasMap }}
Expand Down Expand Up @@ -122,25 +128,27 @@
</style>

<div class="content">
<div id="menu" class="flex flex-center">
<a id="list-display" class="button button-icon" href="?d=list">
<img class="icon" src="{{ url "/svg/list?fill=aliceblue" }}" alt="List">
</a>
<a id="grid-display" class="button button-icon" href="?d=grid">
<img class="icon" src="{{ url "/svg/th?fill=aliceblue" }}" alt="Grid">
</a>
<a id="story-display" class="button button-icon" href="?d=story">
<img class="icon" src="{{ url "/svg/image?fill=aliceblue" }}" alt="Image story">
</a>

<span class="flex-grow"></span>

{{ if .HasMap }}
<a class="padding" href="#map" download>
<img class="icon" src="{{ url "/svg/map?fill=aliceblue" }}" alt="View on map">
{{ if not .Request.Share.Story }}
<div id="menu" class="flex flex-center">
<a id="list-display" class="button button-icon" href="?d=list">
<img class="icon" src="{{ url "/svg/list?fill=aliceblue" }}" alt="List">
</a>
{{ end }}
</div>
<a id="grid-display" class="button button-icon" href="?d=grid">
<img class="icon" src="{{ url "/svg/th?fill=aliceblue" }}" alt="Grid">
</a>
<a id="story-display" class="button button-icon" href="?d=story">
<img class="icon" src="{{ url "/svg/image?fill=aliceblue" }}" alt="Image story">
</a>

<span class="flex-grow"></span>

{{ if .HasMap }}
<a class="padding" href="#map" download>
<img class="icon" src="{{ url "/svg/map?fill=aliceblue" }}" alt="View on map">
</a>
{{ end }}
</div>
{{ end }}

<ul id="files" class="no-margin no-padding">
{{ range .Files }}
Expand Down
18 changes: 16 additions & 2 deletions pkg/crud/share.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crud

import (
"errors"
"fmt"
"net/http"
"path"
Expand Down Expand Up @@ -48,6 +49,19 @@ func (a App) bestSharePath(pathname string) string {
return ""
}

func parseRights(value string) (edit, story bool, err error) {
switch value {
case "edit":
return true, false, nil
case "read":
return false, false, nil
case "story":
return false, true, nil
default:
return false, false, errors.New("invalid rights: edit, read or story allowed")
}
}

func (a App) createShare(w http.ResponseWriter, r *http.Request, request provider.Request) {
if !request.CanShare {
a.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
Expand All @@ -56,7 +70,7 @@ func (a App) createShare(w http.ResponseWriter, r *http.Request, request provide

var err error

edit, err := getFormBool(r.FormValue("edit"))
edit, story, err := parseRights(strings.TrimSpace(r.FormValue("rights")))
if err != nil {
a.error(w, r, request, model.WrapInvalid(err))
return
Expand Down Expand Up @@ -91,7 +105,7 @@ func (a App) createShare(w http.ResponseWriter, r *http.Request, request provide
return
}

id, err := a.shareApp.Create(ctx, request.Filepath(), edit, password, info.IsDir, duration)
id, err := a.shareApp.Create(ctx, request.Filepath(), edit, story, password, info.IsDir, duration)
if err != nil {
a.error(w, r, request, model.WrapInternal(err))
return
Expand Down
4 changes: 1 addition & 3 deletions pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ func (a App) story(r *http.Request, request provider.Request, item absto.Item, f
logger.WithField("item", item.Pathname).Error("unable to get exif: %s", err)
}

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

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

request.Display = provider.StoryDisplay

return renderer.NewPage("story", http.StatusOK, map[string]any{
"Paths": getPathParts(request),
"Files": items,
Expand Down
2 changes: 1 addition & 1 deletion pkg/crud/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (a App) Upload(w http.ResponseWriter, r *http.Request, request provider.Req

var shareID string
if shared {
id, err := a.shareApp.Create(ctx, path.Join(request.Path, filename), false, "", false, duration)
id, err := a.shareApp.Create(ctx, path.Join(request.Path, filename), false, false, "", false, duration)
if err != nil {
a.error(w, r, request, model.WrapInternal(err))
return
Expand Down
4 changes: 4 additions & 0 deletions pkg/fibr/fibr.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (a App) parseShare(request *provider.Request, authorizationHeader string) e
request.CanEdit = share.Edit
request.Path = strings.TrimPrefix(request.Path, fmt.Sprintf("/%s", share.ID))

if share.Story {
request.Display = provider.StoryDisplay
}

if share.File {
request.Path = ""
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/mocks/share.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/provider/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Auth interface {
//go:generate mockgen -destination ../mocks/share.go -mock_names ShareManager=Share -package mocks github.com/ViBiOh/fibr/pkg/provider ShareManager
type ShareManager interface {
Get(string) Share
Create(context.Context, string, bool, string, bool, time.Duration) (string, error)
Create(context.Context, string, bool, bool, string, bool, time.Duration) (string, error)
Delete(context.Context, string) error
List() []Share
}
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Share struct {
Password string `json:"password"`
Duration time.Duration `json:"duration"`
Edit bool `json:"edit"`
Story bool `json:"story"`
File bool `json:"file"`
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/share/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (a *App) List() (shares []provider.Share) {
}

// Create a share
func (a *App) Create(ctx context.Context, filepath string, edit bool, password string, isDir bool, duration time.Duration) (string, error) {
func (a *App) Create(ctx context.Context, filepath string, edit, story bool, password string, isDir bool, duration time.Duration) (string, error) {
var id string

_, err := a.Exclusive(ctx, a.amqpExclusiveRoutingKey, semaphoreDuration, func(ctx context.Context) error {
Expand All @@ -61,6 +61,7 @@ func (a *App) Create(ctx context.Context, filepath string, edit bool, password s
Path: filepath,
RootName: path.Base(filepath),
Edit: edit,
Story: story,
Password: password,
File: !isDir,
Creation: a.clock.Now(),
Expand Down

0 comments on commit 106f2db

Please sign in to comment.