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 {
fmt.Println("Download file")
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 }
}
......@@ -103,7 +108,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
}
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) {
return &HTTPError{err, "Record not found", 404}
}
......@@ -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))
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
}
......@@ -148,14 +153,14 @@ func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil {
return &HTTPError{err, "Request does not contain a valid body", 400}
}
// parse in json
var newShare Share
err = json.Unmarshal(reqBody, &newShare)
if err != nil {
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
err = db.Create(&newShare).Error
if err != nil {
......@@ -170,7 +175,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("CloseShare")
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 }
}
......@@ -181,7 +187,7 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
// get stuff
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) {
return &HTTPError{err, "Record not found", 404}
}
......@@ -190,8 +196,8 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
}
// move files to permanent location
oldPath := filepath.Join(config.mediaDir, "temp", vars["id"])
newPath := filepath.Join(config.mediaDir, "data", vars["id"])
oldPath := filepath.Join(config.mediaDir, "temp", shareID.String())
newPath := filepath.Join(config.mediaDir, "data", shareID.String())
err = os.Rename(oldPath, newPath)
if err != nil {
return &HTTPError{err, "Can't move directory", 500}
......@@ -212,7 +218,8 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("UploadTest")
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 }
}
......@@ -221,6 +228,19 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
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
err = r.ParseMultipartForm(config.chunkSize) // Maximum 10 MB in RAM
if err != nil {
......@@ -236,7 +256,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
{
// add db entry TODO fehlerbehandlung für die ganze transaction
db.Begin()
sid, err := uuid.Parse(vars["id"])
sid, err := uuid.Parse(shareID.String())
if err != nil {
return &HTTPError{ err, "foreign key shareID not valid", 500 }
}
......@@ -250,7 +270,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil {
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 {
return &HTTPError{ err, "cant save file", 500 }
db.Rollback()
......@@ -280,7 +300,7 @@ func ConfigureRoutes() {
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 {
......
......@@ -4,16 +4,16 @@ import (
"encoding/json"
"fmt"
"log"
"os"
)
type Config struct {
var config = struct {
port int
mediaDir string
chunkSize int64
}
var config = Config {
}{
port: 6969,
mediaDir: "./media",
mediaDir: os.Getenv("MEDIA_DIR"),
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