Commit cd0aa25f authored by Sonja Happ's avatar Sonja Happ
Browse files

fix for file update method, remove redundant code, error formatting fixes

parent 4d9699fb
......@@ -109,6 +109,7 @@ github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
......@@ -213,8 +214,6 @@ go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4=
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
......
......@@ -97,14 +97,14 @@ func addFile(c *gin.Context) {
}
// Extract file from POST request form
file_header, err := c.FormFile("file")
fileHeader, err := c.FormFile("file")
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("Get form error: %s", err.Error()))
return
}
var newFile File
err = newFile.Register(file_header, so.ID)
err = newFile.Register(fileHeader, so.ID)
if !helper.DBError(c, err) {
c.JSON(http.StatusOK, gin.H{"file": newFile.File})
}
......
......@@ -74,7 +74,7 @@ func (f *File) download(c *gin.Context) error {
} else {
url, err := f.getS3Url()
if err != nil {
return fmt.Errorf("Failed to presign S3 request: %s", err)
return fmt.Errorf("failed to presign S3 request: %s", err)
}
c.Redirect(http.StatusFound, url)
}
......@@ -93,10 +93,10 @@ func (f *File) Register(fileHeader *multipart.FileHeader, scenarioID uint) error
// set file data
fileContent, err := fileHeader.Open()
defer fileContent.Close()
if err != nil {
return err
}
defer fileContent.Close()
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if bucket == "" {
......@@ -105,7 +105,7 @@ func (f *File) Register(fileHeader *multipart.FileHeader, scenarioID uint) error
} else {
err := f.putS3(fileContent)
if err != nil {
return fmt.Errorf("Failed to upload to S3 bucket: %s", err)
return fmt.Errorf("failed to upload to S3 bucket: %s", err)
}
}
......@@ -151,10 +151,10 @@ func (f *File) update(fileHeader *multipart.FileHeader) error {
// set file data
fileContent, err := fileHeader.Open()
defer fileContent.Close()
if err != nil {
return err
}
defer fileContent.Close()
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if bucket == "" {
......@@ -163,7 +163,7 @@ func (f *File) update(fileHeader *multipart.FileHeader) error {
} else {
err := f.putS3(fileContent)
if err != nil {
return fmt.Errorf("Failed to upload to S3 bucket: %s", err)
return fmt.Errorf("failed to upload to S3 bucket: %s", err)
}
}
......@@ -193,12 +193,19 @@ func (f *File) update(fileHeader *multipart.FileHeader) error {
}
// Add File object with parameters to DB
err = f.save()
if err != nil {
return err
}
db := database.GetDB()
err = db.Model(f).Updates(map[string]interface{}{
"Size": f.Size,
"FileData": f.FileData,
"Date": f.Date,
"Name": f.Name,
"Type": f.Type,
"ImageHeight": f.ImageHeight,
"ImageWidth": f.ImageWidth,
"Key": f.Key,
}).Error
return nil
return err
}
func (f *File) Delete() error {
......@@ -214,7 +221,10 @@ func (f *File) Delete() error {
// delete file from s3 bucket
if f.Key != "" {
f.deleteS3()
err = f.deleteS3()
if err != nil {
return err
}
}
err = db.Model(&so).Association("Files").Delete(f).Error
......
......@@ -38,16 +38,22 @@ import (
// Global session
var s3Session *session.Session = nil
func getS3Session() (*session.Session, error) {
func getS3Session() (*session.Session, string, error) {
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if err != nil || bucket == "" {
return nil, "", fmt.Errorf("no S3 bucket configured: %s", err)
}
if s3Session == nil {
var err error
s3Session, err = createS3Session()
if err != nil {
return nil, err
return nil, "", err
}
}
return s3Session, nil
return s3Session, bucket, nil
}
func createS3Session() (*session.Session, error) {
......@@ -65,7 +71,7 @@ func createS3Session() (*session.Session, error) {
},
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create session: %s", err)
}
return sess, nil
......@@ -73,15 +79,10 @@ func createS3Session() (*session.Session, error) {
func (f *File) putS3(fileContent io.Reader) error {
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if err != nil || bucket == "" {
return fmt.Errorf("No S3 bucket configured")
}
// The session the S3 Uploader will use
sess, err := getS3Session()
sess, bucket, err := getS3Session()
if err != nil {
return fmt.Errorf("Failed to create session: %s", err)
return err
}
// Create an uploader with the session and default options
......@@ -97,22 +98,18 @@ func (f *File) putS3(fileContent io.Reader) error {
Body: fileContent,
})
if err != nil {
return fmt.Errorf("Failed to upload file, %v", err)
return fmt.Errorf("failed to upload file, %v", err)
}
return nil
}
func (f *File) getS3Url() (string, error) {
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if err != nil || bucket == "" {
return "", fmt.Errorf("No S3 bucket configured")
}
// The session the S3 Uploader will use
sess, err := getS3Session()
sess, bucket, err := getS3Session()
if err != nil {
return "", fmt.Errorf("Failed to create session: %s", err)
return "", err
}
// Create S3 service client
......@@ -138,15 +135,11 @@ func (f *File) getS3Url() (string, error) {
}
func (f *File) deleteS3() error {
bucket, err := configuration.GolbalConfig.String("s3.bucket")
if err != nil || bucket == "" {
return fmt.Errorf("No S3 bucket configured")
}
// The session the S3 Uploader will use
sess, err := getS3Session()
sess, bucket, err := getS3Session()
if err != nil {
return fmt.Errorf("Failed to create session: %s", err)
return err
}
// Create S3 service client
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment