[#78] enable fully qualified URIs for S3 endpoints and improved error reporting when uploading or deleting files
This commit is contained in:
		
							parent
							
								
									52c288d9db
								
							
						
					
					
						commit
						46399dddac
					
				| 
						 | 
					@ -255,7 +255,7 @@ type S3Config struct {
 | 
				
			||||||
// Validate makes S3Config validatable by implementing [validation.Validatable] interface.
 | 
					// Validate makes S3Config validatable by implementing [validation.Validatable] interface.
 | 
				
			||||||
func (c S3Config) Validate() error {
 | 
					func (c S3Config) Validate() error {
 | 
				
			||||||
	return validation.ValidateStruct(&c,
 | 
						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.Bucket, validation.When(c.Enabled, validation.Required)),
 | 
				
			||||||
		validation.Field(&c.Region, 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)),
 | 
							validation.Field(&c.AccessKey, validation.When(c.Enabled, validation.Required)),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -315,7 +315,19 @@ func TestS3ConfigValidate(t *testing.T) {
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			true,
 | 
								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{
 | 
								core.S3Config{
 | 
				
			||||||
				Enabled:   true,
 | 
									Enabled:   true,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,6 +3,7 @@ package forms
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
| 
						 | 
					@ -316,17 +317,22 @@ func (form *RecordUpsert) processFilesToUpload() error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer fs.Close()
 | 
						defer fs.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var uploadErrors []error
 | 
				
			||||||
	for i := len(form.filesToUpload) - 1; i >= 0; i-- {
 | 
						for i := len(form.filesToUpload) - 1; i >= 0; i-- {
 | 
				
			||||||
		file := form.filesToUpload[i]
 | 
							file := form.filesToUpload[i]
 | 
				
			||||||
		path := form.record.BaseFilesPath() + "/" + file.Name()
 | 
							path := form.record.BaseFilesPath() + "/" + file.Name()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := fs.Upload(file.Bytes(), path); err == nil {
 | 
							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:]...)
 | 
								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 {
 | 
						if len(uploadErrors) > 0 {
 | 
				
			||||||
		return errors.New("Failed to upload all files.")
 | 
							return fmt.Errorf("Failed to upload all files: %v", uploadErrors)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
| 
						 | 
					@ -347,20 +353,25 @@ func (form *RecordUpsert) processFilesToDelete() error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer fs.Close()
 | 
						defer fs.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var deleteErrors []error
 | 
				
			||||||
	for i := len(form.filesToDelete) - 1; i >= 0; i-- {
 | 
						for i := len(form.filesToDelete) - 1; i >= 0; i-- {
 | 
				
			||||||
		filename := form.filesToDelete[i]
 | 
							filename := form.filesToDelete[i]
 | 
				
			||||||
		path := form.record.BaseFilesPath() + "/" + filename
 | 
							path := form.record.BaseFilesPath() + "/" + filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := fs.Delete(path); err == nil {
 | 
							if err := fs.Delete(path); err == nil {
 | 
				
			||||||
 | 
								// remove the deleted file from the list
 | 
				
			||||||
			form.filesToDelete = append(form.filesToDelete[:i], form.filesToDelete[i+1:]...)
 | 
								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)
 | 
							// try to delete the related file thumbs (if any)
 | 
				
			||||||
		fs.DeletePrefix(form.record.BaseFilesPath() + "/thumbs_" + filename + "/")
 | 
							fs.DeletePrefix(form.record.BaseFilesPath() + "/thumbs_" + filename + "/")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(form.filesToDelete) > 0 {
 | 
						if len(deleteErrors) > 0 {
 | 
				
			||||||
		return errors.New("Failed to delete all files.")
 | 
							return fmt.Errorf("Failed to delete all files: %v", deleteErrors)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue