Skip to content

Commit

Permalink
[pocketbase#78] enable fully qualified URIs for S3 endpoints and impr…
Browse files Browse the repository at this point in the history
…oved error reporting when uploading or deleting files
  • Loading branch information
ganigeorgiev committed Jul 11, 2022
1 parent 52c288d commit 46399dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type S3Config struct {
// Validate makes S3Config validatable by implementing [validation.Validatable] interface.
func (c S3Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Endpoint, is.Host, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Endpoint, is.URL, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Bucket, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Region, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.AccessKey, validation.When(c.Enabled, validation.Required)),
Expand Down
14 changes: 13 additions & 1 deletion core/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,19 @@ func TestS3ConfigValidate(t *testing.T) {
},
true,
},
// valid data
// valid data (url endpoint)
{
core.S3Config{
Enabled: true,
Endpoint: "https://localhost:8090",
Bucket: "test",
Region: "test",
AccessKey: "test",
Secret: "test",
},
false,
},
// valid data (hostname endpoint)
{
core.S3Config{
Enabled: true,
Expand Down
19 changes: 15 additions & 4 deletions forms/record_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package forms
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -316,17 +317,22 @@ func (form *RecordUpsert) processFilesToUpload() error {
}
defer fs.Close()

var uploadErrors []error
for i := len(form.filesToUpload) - 1; i >= 0; i-- {
file := form.filesToUpload[i]
path := form.record.BaseFilesPath() + "/" + file.Name()

if err := fs.Upload(file.Bytes(), path); err == nil {
// remove the uploaded file from the list
form.filesToUpload = append(form.filesToUpload[:i], form.filesToUpload[i+1:]...)
} else {
// store the upload error
uploadErrors = append(uploadErrors, fmt.Errorf("File %d: %v", i, err))
}
}

if len(form.filesToUpload) > 0 {
return errors.New("Failed to upload all files.")
if len(uploadErrors) > 0 {
return fmt.Errorf("Failed to upload all files: %v", uploadErrors)
}

return nil
Expand All @@ -347,20 +353,25 @@ func (form *RecordUpsert) processFilesToDelete() error {
}
defer fs.Close()

var deleteErrors []error
for i := len(form.filesToDelete) - 1; i >= 0; i-- {
filename := form.filesToDelete[i]
path := form.record.BaseFilesPath() + "/" + filename

if err := fs.Delete(path); err == nil {
// remove the deleted file from the list
form.filesToDelete = append(form.filesToDelete[:i], form.filesToDelete[i+1:]...)
} else {
// store the delete error
deleteErrors = append(deleteErrors, fmt.Errorf("File %d: %v", i, err))
}

// try to delete the related file thumbs (if any)
fs.DeletePrefix(form.record.BaseFilesPath() + "/thumbs_" + filename + "/")
}

if len(form.filesToDelete) > 0 {
return errors.New("Failed to delete all files.")
if len(deleteErrors) > 0 {
return fmt.Errorf("Failed to delete all files: %v", deleteErrors)
}

return nil
Expand Down

0 comments on commit 46399dd

Please sign in to comment.