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

fix ID und database und openShare

parent 873ce517
......@@ -25,7 +25,7 @@ type endpointREST func(http.ResponseWriter, *http.Request) *HTTPError
func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil { // e is *HTTPError, not os.Error.
fmt.Errorf("Error (%s): %s - %i", e.Error.Error(), e.Message, e.Code)
//fmt.Printf("Error (%s): %s - %i", e.Error.Error(), e.Message, e.Code)
if e.Code == 401 {
w.Header().Set("WWW-Authenticate", `Basic realm="Please enter the password"`)
}
......@@ -63,7 +63,6 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
var share Share
err = db.Preload("Attachments").Where("ID = ?", vars["id"]).First(&share).Error
// return 404
if errors.Is(err, gorm.ErrRecordNotFound) {
return &HTTPError{err, "Record not found", 404}
}
......@@ -74,6 +73,7 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Share is not finalized", 424}
}
// auth
sid, pass, _ := r.BasicAuth()
if sid != share.ID.String() {
return &HTTPError{err, "wrong username", 401}
......@@ -113,6 +113,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't fetch data", 500}
}
// auth
sid, pass, _ := r.BasicAuth()
if sid != share.ID.String() {
return &HTTPError{err, "wrong username", 401}
......@@ -141,7 +142,6 @@ 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)
......@@ -150,11 +150,10 @@ func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
}
// create temporary db entries
err = db.Create(&newShare).Error
newShare.IsTemporary = true
db.Save(&newShare)
err = db.Create(&newShare).Error
if err != nil {
return &HTTPError{err, "Can't save data", 500}
return &HTTPError{err, "Can't create data", 500}
}
// create temporary dir
......@@ -281,7 +280,10 @@ func ConfigureRoutes() {
func SendJSON(w http.ResponseWriter, res interface{}) *HTTPError {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
return &HTTPError{err, "Can't encode data", 500}
if err != nil {
return &HTTPError{err, "Can't encode data", 500}
}
return nil
}
func SendMail(receiver string) {
......
......@@ -11,27 +11,55 @@ import (
// Share has many Attachments, ShareID is the foreign key
type Share struct {
ID uuid.UUID `json:"id" gorm:"unique; not null"`
Name string `json:"name"`
Expires time.Time `json:"expires,omitempty"`
DownloadLimit int `json:"downloadLimit,omitempty"`
IsPublic bool `json:"is_public" gorm:"not null; default:false; index"`
Password string `json:"-"`
Email string `json:"email"`
IsTemporary bool `json:"-" gorm:"not null"`
Attachments []Attachment `json:"files" gorm:"constraint:OnDelete:CASCADE"`
ID uuid.UUID `json:"id" gorm:"type:uuid; primary_key"`
Name string `json:"name"`
Expires time.Time `json:"expires,omitempty"`
DownloadLimit int `json:"downloadLimit,omitempty"`
IsPublic bool `json:"is_public" gorm:"not null; default:false; index"`
Password string `json:"-"`
Email string `json:"email"`
IsTemporary bool `json:"-" gorm:"not null"`
Attachments []Attachment`json:"files" gorm:"constraint:OnDelete:CASCADE"`
}
type Attachment struct {
ID uuid.UUID `json:"id" gorm:"unique; not null"`
Filename string `json:"filename" gorm:"not null"`
Filesize int64 `json:"filesize" gorm:"not null; default:0"`
IsEncrypted bool `json:"-" gorm:"not null; default:false"`
ID uuid.UUID `json:"id" gorm:"type:uuid; primary_key"`
Filename string `json:"filename" gorm:"not null"`
Filesize int64 `json:"filesize" gorm:"not null; default:0"`
IsEncrypted bool `json:"-" gorm:"not null; default:false"`
ShareID uuid.UUID `json:"-" gorm:"not null"`
}
func (att *Attachment) BeforeCreate(scope *gorm.DB) error {
uid, err := uuid.NewRandom()
if err != nil {
return err
}
att.ID = uid
//scope.Set("ID", uid)
return nil
}
ShareID uuid.UUID `json:"-" gorm:"not null"`
func (sh *Share) BeforeCreate(scope *gorm.DB) error {
uid, err := uuid.NewRandom()
if err != nil {
return err
}
sh.ID = uid
//scope.Set("ID", uid)
return nil
}
//func generateID() uuid.UUID {
// uid, err := uuid.NewRandom()
// if err != nil {
// fmt.Println(err)
// }
// return uid
//}
func GetDatabase()(*gorm.DB, error) {
dsn := os.Getenv("DATABASE_URI")
......
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