Skip to content

Commit

Permalink
make guest file expiration fixed so that persisting of file doesn't d…
Browse files Browse the repository at this point in the history
…ecrease with time (example guest with expiration limit of 30 for URL and file can still upload a file after 29 days which will be persisted for 30 days)
  • Loading branch information
Balk-Z committed Dec 6, 2024
1 parent 2c3d594 commit cc0bcad
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 28 deletions.
7 changes: 1 addition & 6 deletions handlers/guest_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ func guestLinkFromRequest(r *http.Request) (picoshare.GuestLink, error) {
return picoshare.GuestLink{}, err
}

fileExpiration, err := parse.Expiration(payload.FileExpiration)
if err != nil {
return picoshare.GuestLink{}, err
}

maxFileBytes, err := parseMaxFileBytes(payload.MaxFileBytes)
if err != nil {
return picoshare.GuestLink{}, err
Expand All @@ -106,7 +101,7 @@ func guestLinkFromRequest(r *http.Request) (picoshare.GuestLink, error) {
return picoshare.GuestLink{
Label: label,
UrlExpires: urlExpiration,
FileExpires: fileExpiration,
FileExpires: payload.FileExpiration,
MaxFileBytes: maxFileBytes,
MaxFileUploads: maxFileUploads,
}, nil
Expand Down
8 changes: 4 additions & 4 deletions handlers/guest_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func TestGuestLinksPostAcceptsValidRequest(t *testing.T) {
payload: `{
"label": null,
"urlExpirationTime":"2030-01-02T03:04:25Z",
"fileExpirationTime":"2030-01-02T03:04:25Z",
"fileExpirationTime":"Never",
"maxFileBytes": null,
"maxFileUploads": null
}`,
expected: picoshare.GuestLink{
Label: picoshare.GuestLinkLabel(""),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: "Never",
MaxFileBytes: picoshare.GuestUploadUnlimitedFileSize,
MaxFileUploads: picoshare.GuestUploadUnlimitedFileUploads,
},
Expand All @@ -45,14 +45,14 @@ func TestGuestLinksPostAcceptsValidRequest(t *testing.T) {
payload: `{
"label": "For my good pal, Maurice",
"urlExpirationTime":"2030-01-02T03:04:25Z",
"fileExpirationTime":"2030-01-02T03:04:25Z",
"fileExpirationTime":"Never",
"maxFileBytes": 1048576,
"maxFileUploads": 1
}`,
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: "Never",
MaxFileBytes: makeGuestUploadMaxFileBytes(1048576),
MaxFileUploads: makeGuestUploadCountLimit(1),
},
Expand Down
6 changes: 4 additions & 2 deletions 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 Expand Up @@ -112,7 +114,7 @@ <h1 class="title">Create Guest Link</h1>
<select id="expiration-select-file">
{{ range .ExpirationOptions }}
<option
value="{{ formatExpiration .Expiration }}"
value="{{ .FriendlyName }}"
{{ if .IsDefault }}selected{{ end }}
>
{{ .FriendlyName }}
Expand Down
2 changes: 1 addition & 1 deletion handlers/templates/pages/guest-link-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h1 class="title">Guest Links</h1>
{{ formatExpiration .UrlExpires }}
</td>
<td class="is-vcentered expiration">
{{ formatFileExpiration .FileExpires }}
{{ .FileExpires }}
</td>
<td class="is-vcentered">
{{ formatSizeLimit .MaxFileBytes }}
Expand Down
17 changes: 16 additions & 1 deletion handlers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,22 @@ func (s Server) guestEntryPost() http.HandlerFunc {
r.Body = http.MaxBytesReader(w, r.Body, int64(*gl.MaxFileBytes))
}

id, err := s.insertFileFromRequest(r, gl.FileExpires, guestLinkID)
m := map[string]time.Time{
"1 day": time.Now().AddDate(0, 0, 1),
"7 days": time.Now().AddDate(0, 0, 7),
"30 days": time.Now().AddDate(0, 0, 30),
"1 year": time.Now().AddDate(1, 0, 0),
"Never": time.Time(picoshare.NeverExpire),
}

fileExpiry, err := parse.Expiration(m[gl.FileExpires].Format(time.RFC3339))
if err != nil {
log.Printf("error calculating file expiration for guest upload %v: %v", guestLinkID, err)
http.Error(w, "Failed to calculate file expiration", http.StatusInternalServerError)
return
}

id, err := s.insertFileFromRequest(r, fileExpiry, guestLinkID)
if err != nil {
var de *dbError
if errors.As(err, &de) {
Expand Down
15 changes: 9 additions & 6 deletions handlers/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ func TestGuestUpload(t *testing.T) {
{
description: "valid upload to guest link",
guestLinkInStore: picoshare.GuestLink{
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: "Never",
},
guestLinkID: "abcdefgh23456789",
status: http.StatusOK,
Expand Down Expand Up @@ -330,9 +331,10 @@ func TestGuestUpload(t *testing.T) {
{
description: "reject upload that includes a note",
guestLinkInStore: picoshare.GuestLink{
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileExpires: "Never",
},
guestLinkID: "abcdefgh23456789",
note: "I'm a disallowed note",
Expand Down Expand Up @@ -374,6 +376,7 @@ func TestGuestUpload(t *testing.T) {
Created: mustParseTime("2000-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
MaxFileBytes: makeGuestUploadMaxFileBytes(1),
FileExpires: "Never",
},
guestLinkID: "abcdefgh23456789",
status: http.StatusBadRequest,
Expand Down
2 changes: 1 addition & 1 deletion picoshare/guest_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type (
Label GuestLinkLabel
Created time.Time
UrlExpires ExpirationTime
FileExpires ExpirationTime
FileExpires string
MaxFileBytes GuestUploadMaxFileBytes
MaxFileUploads GuestUploadCountLimit
FilesUploaded int
Expand Down
9 changes: 2 additions & 7 deletions store/sqlite/guest_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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", guestLink.FileExpires)); err != nil {
return err
}

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

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

return picoshare.GuestLink{
ID: id,
Label: label,
Expand All @@ -167,6 +162,6 @@ func guestLinkFromRow(row rowScanner) (picoshare.GuestLink, error) {
FilesUploaded: filesUploaded,
Created: ct,
UrlExpires: picoshare.ExpirationTime(uet),
FileExpires: picoshare.ExpirationTime(fet),
FileExpires: fileExpirationTimeRaw,
}, nil
}

0 comments on commit cc0bcad

Please sign in to comment.