working commit
This commit is contained in:
+7
-4
@@ -18,22 +18,25 @@ import (
|
||||
|
||||
const zeroContentLength = "0"
|
||||
|
||||
func (hand *Handler) FileExists(rctx *router.Context) {
|
||||
func (hand *Handler) FileInfo(rctx *router.Context) {
|
||||
|
||||
filepath, _ := rctx.GetSubpath("filepath")
|
||||
params := &operator.FileExistsParams{
|
||||
params := &operator.FileInfoParams{
|
||||
Filepath: filepath,
|
||||
}
|
||||
ctx := rctx.GetContext()
|
||||
code, res, err := hand.oper.FileExists(ctx, params)
|
||||
code, res, err := hand.oper.FileInfo(ctx, params)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("FileExists error: %v", err)
|
||||
hand.logg.Errorf("FileInfo error: %v", err)
|
||||
rctx.SetStatus(code)
|
||||
return
|
||||
}
|
||||
rctx.SetHeader("Content-Collection", res.ContentCollection)
|
||||
rctx.SetHeader("Content-Name", res.ContentName)
|
||||
rctx.SetHeader("Content-Type", res.ContentType)
|
||||
rctx.SetHeader("Content-Size", res.ContentSize)
|
||||
rctx.SetHeader("Content-Digest", res.ContentDigest)
|
||||
|
||||
rctx.SetHeader("Content-Length", zeroContentLength)
|
||||
rctx.SetStatus(code)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ func (db *Database) InsertAccount(ctx context.Context, account *descr.Account) e
|
||||
|
||||
func (db *Database) UpdateAccountByID(ctx context.Context, accountID string, account *descr.Account) error {
|
||||
var err error
|
||||
|
||||
request := `UPDATE accounts SET username = $1, passhash = $2, disabled = $3, updated_at = $4 WHERE id = $6`
|
||||
_, err = db.db.Exec(request, account.Username, account.Passhash, account.Disabled, account.UpdatedAt, accountID)
|
||||
if err != nil {
|
||||
@@ -41,7 +40,7 @@ func (db *Database) ReducedListAccounts(ctx context.Context) ([]descr.Account, e
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (db *Database) CompletedListAccounts(ctx context.Context) ([]descr.Account, error) {
|
||||
func (db *Database) ListAccounts(ctx context.Context) ([]descr.Account, error) {
|
||||
var err error
|
||||
request := `SELECT * FROM accounts`
|
||||
res := make([]descr.Account, 0)
|
||||
@@ -56,6 +55,7 @@ func (db *Database) GetAccountByID(ctx context.Context, accountID string) (bool,
|
||||
var err error
|
||||
var res *descr.Account
|
||||
var exists bool
|
||||
|
||||
request := `SELECT * FROM accounts WHERE id = $1 LiMIT 1`
|
||||
dbRes := make([]descr.Account, 0)
|
||||
err = db.db.Select(&dbRes, request, accountID)
|
||||
|
||||
+26
-14
@@ -100,15 +100,19 @@ func (oper *Operator) UpdateAccount(ctx context.Context, params *UpdateAccountPa
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with ID %s dont exists", params.AccountID)
|
||||
return res, err
|
||||
}
|
||||
case params.Username != "":
|
||||
accountExists, accountDescr, err = oper.mdb.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with this is or name dont exists")
|
||||
return res, err
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with name %s dont exists", params.Username)
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
now := auxtool.TimeNow()
|
||||
if params.NewUsername != "" {
|
||||
@@ -150,15 +154,19 @@ func (oper *Operator) DeleteAccount(ctx context.Context, params *DeleteAccountPa
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with ID %s dont exists", params.AccountID)
|
||||
return res, err
|
||||
}
|
||||
case params.Username != "":
|
||||
accountExists, accountDescr, err = oper.mdb.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with this is or name dont exists")
|
||||
return res, err
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with name %s dont exists", params.Username)
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
|
||||
err = oper.mdb.DeleteAllGrantsForAccountID(ctx, accountDescr.ID)
|
||||
@@ -216,7 +224,7 @@ type GetAccountParams struct {
|
||||
AccountID string
|
||||
}
|
||||
type GetAccountResult struct {
|
||||
Account *descr.AccountShortDescr `json:"accountDescr"`
|
||||
Account *descr.AccountShortDescr `json:"account"`
|
||||
}
|
||||
|
||||
func (oper *Operator) GetAccount(ctx context.Context, params *GetAccountParams) (*GetAccountResult, error) {
|
||||
@@ -227,19 +235,23 @@ func (oper *Operator) GetAccount(ctx context.Context, params *GetAccountParams)
|
||||
var accountExists bool
|
||||
switch {
|
||||
case params.AccountID != "":
|
||||
accountExists, accountDescr, err = oper.mdb.GetAccountByID(ctx, params.Username)
|
||||
accountExists, accountDescr, err = oper.mdb.GetAccountByID(ctx, params.AccountID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with ID %s dont exists", params.AccountID)
|
||||
return res, err
|
||||
}
|
||||
case params.Username != "":
|
||||
accountExists, accountDescr, err = oper.mdb.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with this is or name dont exists")
|
||||
return res, err
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with name %s dont exists", params.Username)
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
accountShortDescr := &descr.AccountShortDescr{
|
||||
Username: accountDescr.Username,
|
||||
|
||||
+16
-12
@@ -23,16 +23,18 @@ import (
|
||||
"mstore/pkg/auxuuid"
|
||||
)
|
||||
|
||||
// FileExists
|
||||
type FileExistsParams struct {
|
||||
// FileInfo
|
||||
type FileInfoParams struct {
|
||||
Filepath string
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
type FileExistsResult struct {
|
||||
ContentType string
|
||||
ContentSize string
|
||||
ContentDigest string
|
||||
type FileInfoResult struct {
|
||||
ContentCollection string
|
||||
ContentName string
|
||||
ContentType string
|
||||
ContentSize string
|
||||
ContentDigest string
|
||||
}
|
||||
|
||||
func cleanFilepath(filename string) (string, error) {
|
||||
@@ -40,10 +42,10 @@ func cleanFilepath(filename string) (string, error) {
|
||||
return filepath.Clean(filename), nil
|
||||
}
|
||||
|
||||
func (oper *Operator) FileExists(ctx context.Context, param *FileExistsParams) (int, *FileExistsResult, error) {
|
||||
func (oper *Operator) FileInfo(ctx context.Context, param *FileInfoParams) (int, *FileInfoResult, error) {
|
||||
var err error
|
||||
code := http.StatusOK
|
||||
res := &FileExistsResult{}
|
||||
res := &FileInfoResult{}
|
||||
|
||||
xfilepath, err := cleanFilepath(param.Filepath)
|
||||
if err != nil {
|
||||
@@ -63,10 +65,12 @@ func (oper *Operator) FileExists(ctx context.Context, param *FileExistsParams) (
|
||||
code = http.StatusNotFound
|
||||
return code, res, err
|
||||
}
|
||||
res = &FileExistsResult{
|
||||
ContentSize: strconv.FormatInt(fileDescr.Size, 10),
|
||||
ContentType: fileDescr.Type,
|
||||
ContentDigest: fileDescr.Checksum,
|
||||
res = &FileInfoResult{
|
||||
ContentCollection: fileDescr.Collection,
|
||||
ContentName: fileDescr.Name,
|
||||
ContentSize: strconv.FormatInt(fileDescr.Size, 10),
|
||||
ContentType: fileDescr.Type,
|
||||
ContentDigest: fileDescr.Checksum,
|
||||
}
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
@@ -72,10 +72,11 @@ func (svc *Service) Build() error {
|
||||
|
||||
svc.rout.Get(`/v3/api/service/hello`, svc.hand.SendHello)
|
||||
|
||||
svc.rout.Head(`/v3/api/file/{filepath}`, svc.hand.FileExists)
|
||||
svc.rout.Head(`/v3/api/file/{filepath}`, svc.hand.FileInfo)
|
||||
svc.rout.Put(`/v3/api/file/{filepath}`, svc.hand.PutFile)
|
||||
svc.rout.Get(`/v3/api/file/{filepath}`, svc.hand.GetFile)
|
||||
svc.rout.Delete(`/v3/api/file/{filepath}`, svc.hand.DeleteFile)
|
||||
|
||||
svc.rout.Get(`/v3/api/files/{filepath}`, svc.hand.ListFiles)
|
||||
svc.rout.Get(`/v3/api/files/`, svc.hand.ListFiles)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user