Commit a68d83e7 authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

fix errors

parent 6fe86c61
......@@ -56,6 +56,9 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("Get Share")
vars := mux.Vars(r)
if vars["id"] == "" {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
}
db, err := GetDatabase()
......@@ -63,6 +66,8 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't get database", 500}
}
var share Share
err = db.Preload("Attachments").Where("ID = ?", vars["id"]).First(&share).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
......@@ -92,6 +97,10 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("Download file")
vars := mux.Vars(r)
if vars["id"] == "" || vars["att"] == "" {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
}
db, err := GetDatabase()
if err != nil {
return &HTTPError{err, "Can't get database", 500}
......@@ -124,7 +133,6 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "wrong password", 401}
}
// return 200
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", att.Filename))
http.ServeFile(w, r, filepath.Join(config.mediaDir, "data", vars["id"], vars["att"]))
return nil
......@@ -172,6 +180,10 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("CloseShare")
vars := mux.Vars(r)
if vars["id"] == "" {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
}
db, err := GetDatabase()
if err != nil {
return &HTTPError{err, "Can't get database", 500}
......@@ -210,6 +222,10 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("UploadTest")
vars := mux.Vars(r)
if vars["id"] == "" {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
}
db, err := GetDatabase()
if err != nil {
return &HTTPError{err, "Can't get database", 500}
......@@ -232,7 +248,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
db.Begin()
sid, err := uuid.Parse(vars["id"])
if err != nil {
fmt.Println(err)
return &HTTPError{ err, "foreign key shareID not valid", 500 }
}
att.ShareID = sid
att.Filename = handler.Filename
......@@ -242,11 +258,11 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
// save file
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
return &HTTPError{ err, "cant read file", 500 }
}
err = ioutil.WriteFile(filepath.Join("media", "temp", vars["id"], att.ID.String()), fileBytes, os.ModePerm)
err = ioutil.WriteFile(filepath.Join("media", "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm)
if err != nil {
fmt.Println(err)
return &HTTPError{ err, "cant save file", 500 }
db.Rollback()
}
db.Commit()
......@@ -284,10 +300,3 @@ func SendJSON(w http.ResponseWriter, res interface{}) *HTTPError {
}
return nil
}
func GetParam(vars map[string]string, key string) (string, *HTTPError) {
if _, ok := vars[key]; !ok {
return "", &HTTPError{errors.New("invalid URL param"), "Invalid URL", 400}
}
return vars[key], nil
}
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