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

use env variables

parent fd6ede75
...@@ -93,7 +93,12 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -93,7 +93,12 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("Download file") fmt.Println("Download file")
vars := mux.Vars(r) vars := mux.Vars(r)
if vars["id"] == "" || vars["att"] == "" { shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
}
attID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 } return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
} }
...@@ -103,7 +108,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -103,7 +108,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
} }
var att Attachment var att Attachment
err = db.Where("id = ?", vars["att"]).First(&att).Error err = db.Where("id = ?", attID.String()).First(&att).Error
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return &HTTPError{err, "Record not found", 404} return &HTTPError{err, "Record not found", 404}
} }
...@@ -130,7 +135,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -130,7 +135,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
} }
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", att.Filename)) 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"])) http.ServeFile(w, r, filepath.Join(config.mediaDir, "data", shareID.String(), attID.String()))
return nil return nil
} }
...@@ -148,14 +153,14 @@ func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -148,14 +153,14 @@ func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil { if err != nil {
return &HTTPError{err, "Request does not contain a valid body", 400} return &HTTPError{err, "Request does not contain a valid body", 400}
} }
// parse in json
var newShare Share var newShare Share
err = json.Unmarshal(reqBody, &newShare) err = json.Unmarshal(reqBody, &newShare)
if err != nil { if err != nil {
return &HTTPError{err, "Can't parse body", 400} return &HTTPError{err, "Can't parse body", 400}
} }
newShare.Attachments = nil // dont want attachments yet
// create temporary db entries // create temporary db entrie
newShare.IsTemporary = true newShare.IsTemporary = true
err = db.Create(&newShare).Error err = db.Create(&newShare).Error
if err != nil { if err != nil {
...@@ -170,7 +175,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -170,7 +175,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("CloseShare") fmt.Println("CloseShare")
vars := mux.Vars(r) vars := mux.Vars(r)
if vars["id"] == "" { shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 } return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
} }
...@@ -181,7 +187,7 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -181,7 +187,7 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
// get stuff // get stuff
var share Share var share Share
err = db.Where("id = ?", vars["id"]).First(&share).Error err = db.Where("id = ?", shareID.String()).First(&share).Error
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return &HTTPError{err, "Record not found", 404} return &HTTPError{err, "Record not found", 404}
} }
...@@ -190,8 +196,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -190,8 +196,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
} }
// move files to permanent location // move files to permanent location
oldPath := filepath.Join(config.mediaDir, "temp", vars["id"]) oldPath := filepath.Join(config.mediaDir, "temp", shareID.String())
newPath := filepath.Join(config.mediaDir, "data", vars["id"]) newPath := filepath.Join(config.mediaDir, "data", shareID.String())
err = os.Rename(oldPath, newPath) err = os.Rename(oldPath, newPath)
if err != nil { if err != nil {
return &HTTPError{err, "Can't move directory", 500} return &HTTPError{err, "Can't move directory", 500}
...@@ -212,7 +218,8 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -212,7 +218,8 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("UploadTest") fmt.Println("UploadTest")
vars := mux.Vars(r) vars := mux.Vars(r)
if vars["id"] == "" { shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 } return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
} }
...@@ -221,6 +228,19 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -221,6 +228,19 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't get database", 500} return &HTTPError{err, "Can't get database", 500}
} }
// get share
var share Share
err = db.Where("id = ?", shareID.String()).First(&share).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return &HTTPError{err, "Record not found", 404}
}
if err != nil {
return &HTTPError{err, "Can't fetch data", 500}
}
if share.IsTemporary != true {
return &HTTPError{ errors.New("Cant upload to finalized shares"), "Can't upload to finalized Shares.", 403 }
}
// Parse file from body // Parse file from body
err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM
if err != nil { if err != nil {
...@@ -236,7 +256,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -236,7 +256,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
{ {
// add db entry TODO fehlerbehandlung für die ganze transaction // add db entry TODO fehlerbehandlung für die ganze transaction
db.Begin() db.Begin()
sid, err := uuid.Parse(vars["id"]) sid, err := uuid.Parse(shareID.String())
if err != nil { if err != nil {
return &HTTPError{ err, "foreign key shareID not valid", 500 } return &HTTPError{ err, "foreign key shareID not valid", 500 }
} }
...@@ -250,7 +270,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -250,7 +270,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil { if err != nil {
return &HTTPError{ err, "cant read file", 500 } return &HTTPError{ err, "cant read file", 500 }
} }
err = ioutil.WriteFile(filepath.Join("media", "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm) err = ioutil.WriteFile(filepath.Join(config.mediaDir, "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm)
if err != nil { if err != nil {
return &HTTPError{ err, "cant save file", 500 } return &HTTPError{ err, "cant save file", 500 }
db.Rollback() db.Rollback()
...@@ -280,7 +300,7 @@ func ConfigureRoutes() { ...@@ -280,7 +300,7 @@ func ConfigureRoutes() {
router.Handle("/share/{id}/attachment/{att}", endpointREST(DownloadFile)).Methods("GET") router.Handle("/share/{id}/attachment/{att}", endpointREST(DownloadFile)).Methods("GET")
log.Fatal(http.ListenAndServe(":6969", handler)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.port), handler))
} }
func SendJSON(w http.ResponseWriter, res interface{}) *HTTPError { func SendJSON(w http.ResponseWriter, res interface{}) *HTTPError {
......
...@@ -4,16 +4,16 @@ import ( ...@@ -4,16 +4,16 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os"
) )
type Config struct { var config = struct {
port int port int
mediaDir string mediaDir string
chunkSize int64 chunkSize int64
} }{
var config = Config {
port: 6969, port: 6969,
mediaDir: "./media", mediaDir: os.Getenv("MEDIA_DIR"),
chunkSize: (10 << 20), // 10 MB chunkSize: (10 << 20), // 10 MB
} }
......
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