Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Chiefs
ChiefSend2
Commits
094a9f40
Commit
094a9f40
authored
Mar 23, 2021
by
Lukas Böhm
🎱
Browse files
fix ID und database und openShare
parent
873ce517
Changes
2
Hide whitespace changes
Inline
Side-by-side
api/api.go
View file @
094a9f40
...
...
@@ -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
.
Error
f
(
"Error (%s): %s - %i"
,
e
.
Error
.
Error
(),
e
.
Message
,
e
.
Code
)
//
fmt.
Print
f("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
.
Sav
e
(
&
newShare
)
err
=
db
.
Creat
e
(
&
newShare
)
.
Error
if
err
!=
nil
{
return
&
HTTPError
{
err
,
"Can't
sav
e data"
,
500
}
return
&
HTTPError
{
err
,
"Can't
creat
e 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
)
{
...
...
api/database.go
View file @
094a9f40
...
...
@@ -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"
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment