Skip to content

Commit

Permalink
use ExpirationTime instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Balk-Z committed Dec 7, 2024
1 parent a8cb111 commit 2bb8367
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions handlers/guest_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestGuestLinksPostAcceptsValidRequest(t *testing.T) {
expected: picoshare.GuestLink{
Label: picoshare.GuestLinkLabel(""),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: mustParseExpirationTime("2999-12-31T00:00:00Z"),
MaxFileBytes: picoshare.GuestUploadUnlimitedFileSize,
MaxFileUploads: picoshare.GuestUploadUnlimitedFileUploads,
},
Expand All @@ -52,7 +52,7 @@ func TestGuestLinksPostAcceptsValidRequest(t *testing.T) {
expected: picoshare.GuestLink{
Label: picoshare.GuestLinkLabel("For my good pal, Maurice"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: mustParseExpirationTime("2999-12-31T00:00:00Z"),
MaxFileBytes: makeGuestUploadMaxFileBytes(1048576),
MaxFileUploads: makeGuestUploadCountLimit(1),
},
Expand Down
4 changes: 3 additions & 1 deletion handlers/templates/pages/guest-link-create.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

const labelInput = document.getElementById("label");
const expirationSelect = document.getElementById("expiration-select");
const expirationSelectFile = document.getElementById("expiration-select-file");
const expirationSelectFile = document.getElementById(
"expiration-select-file"
);
const maxFileBytesInput = document.getElementById("max-file-size");
const fileUploadLimitInput = document.getElementById("file-upload-limit");
const createLinkForm = document.getElementById("create-guest-link-form");
Expand Down
12 changes: 12 additions & 0 deletions handlers/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func (s Server) guestLinkIndexGet() http.HandlerFunc {
}
return fmt.Sprintf("%s (%.0f days%s)", t.Format(time.DateOnly), math.Abs(delta.Hours())/24, suffix)
},
"formatFileExpiration": func(et picoshare.ExpirationTime) string {
if et == picoshare.NeverExpire {
return "Never"
}
t := time.Time(et)
letterS := "s"
delta := time.Until(t)
if delta.Hours() < 25 {
letterS = ""
}
return fmt.Sprintf("After %.0f day%s", math.Abs(delta.Hours())/24, letterS)
},
"isActive": func(gl picoshare.GuestLink) bool {
return gl.IsActive()
},
Expand Down
8 changes: 3 additions & 5 deletions store/sqlite/guest_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (s Store) GetGuestLinks() ([]picoshare.GuestLink, error) {

func (s *Store) InsertGuestLink(guestLink picoshare.GuestLink) error {
log.Printf("saving new guest link %s", guestLink.ID)
log.Printf("guestLink %v", guestLink)

if _, err := s.ctx.Exec(`
INSERT INTO guest_links
Expand All @@ -89,7 +90,7 @@ func (s *Store) InsertGuestLink(guestLink picoshare.GuestLink) error {
sql.Named("max_file_uploads", guestLink.MaxFileUploads),
sql.Named("creation_time", formatTime(time.Now())),
sql.Named("url_expiration_time", formatExpirationTime(guestLink.UrlExpires)),
sql.Named("file_expiration_time", formatExpirationTime(guestLink.FileExpires))); err != nil {
sql.Named("file_expiration_time", formatFileExpirationTime(guestLink.FileExpires))); err != nil {
return err
}

Expand Down Expand Up @@ -154,10 +155,7 @@ func guestLinkFromRow(row rowScanner) (picoshare.GuestLink, error) {
return picoshare.GuestLink{}, err
}

fet, err := parseDatetime(fileExpirationTimeRaw)
if err != nil {
return picoshare.GuestLink{}, err
}
fet := parseFileDatetime(fileExpirationTimeRaw)

return picoshare.GuestLink{
ID: id,
Expand Down
26 changes: 26 additions & 0 deletions store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package sqlite

import (
"database/sql"
"fmt"
"log"
"math"
"time"

_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -66,6 +68,11 @@ func NewWithChunkSize(path string, chunkSize int, optimizeForLitestream bool) St
}
}

func formatFileExpirationTime(et picoshare.ExpirationTime) string {
delta := time.Until(time.Time(et))
return fmt.Sprintf("%.0f", math.Abs(delta.Hours())/24)
}

func formatExpirationTime(et picoshare.ExpirationTime) string {
return formatTime(time.Time(et))
}
Expand All @@ -74,6 +81,25 @@ func formatTime(t time.Time) string {
return t.UTC().Format(timeFormat)
}

func parseFileDatetime(s string) time.Time {
log.Printf("asadsasdasdsad")
t := time.Now()
switch s {
case "1":
t = t.AddDate(0, 0, 1)
case "7":
t = t.AddDate(0, 0, 7)
case "30":
t = t.AddDate(0, 0, 30)
case "365":
t = t.AddDate(1, 0, 0)
default:
t = time.Time(picoshare.NeverExpire)
}

return t
}

func parseDatetime(s string) (time.Time, error) {
return time.Parse(timeFormat, s)
}

0 comments on commit 2bb8367

Please sign in to comment.