working commit
This commit is contained in:
+87
-8
@@ -3,7 +3,12 @@ package operator
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
//"path"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/pkg/auxtool"
|
||||
"mstore/pkg/auxuuid"
|
||||
)
|
||||
|
||||
// File exists
|
||||
@@ -12,16 +17,23 @@ type FileExistsParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
type FileExistsResult struct{}
|
||||
type FileExistsResult struct {
|
||||
Descr *descr.File
|
||||
}
|
||||
|
||||
func (oper *Operator) FileExists(param *FileExistsParams) (int, *FileExistsResult, error) {
|
||||
var err error
|
||||
|
||||
//filename := path.Base(param.Filepath)
|
||||
//dirname := path.Dir(filepath)
|
||||
|
||||
code := http.StatusNotFound
|
||||
res := &FileExistsResult{}
|
||||
code := http.StatusOK
|
||||
|
||||
filename := path.Base(param.Filepath)
|
||||
collection := path.Dir(param.Filepath)
|
||||
|
||||
exist, file, err := oper.mdb.GetFileByCollection(collection, filename)
|
||||
if exist {
|
||||
code = http.StatusOK
|
||||
res.Descr = file
|
||||
}
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
@@ -32,11 +44,78 @@ type PutFileParams struct {
|
||||
Filepath string
|
||||
Source io.ReadCloser
|
||||
}
|
||||
type PutFileResult struct{}
|
||||
type PutFileResult struct {
|
||||
Descr *descr.File
|
||||
}
|
||||
|
||||
const defaultContentType = "application/octet-stream"
|
||||
|
||||
func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error) {
|
||||
var err error
|
||||
res := &PutFileResult{}
|
||||
|
||||
size, err := strconv.ParseInt(param.ContentLength, 10, 64)
|
||||
if err != nil {
|
||||
code := http.StatusLengthRequired
|
||||
return code, res, err
|
||||
}
|
||||
contentType := param.ContentType
|
||||
if contentType == "" {
|
||||
contentType = defaultContentType
|
||||
}
|
||||
|
||||
filename := path.Base(param.Filepath)
|
||||
collection := path.Dir(param.Filepath)
|
||||
oper.logg.Debugf("Put file %s %s", collection, filename)
|
||||
|
||||
tmpname, size, checksum, err := oper.store.WriteTempFile(param.Source)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
exists, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
now := auxtool.TimeNow()
|
||||
if exists {
|
||||
fileDescr.Size = size
|
||||
fileDescr.Checksum = checksum
|
||||
fileDescr.UpdatedAt = now
|
||||
fileDescr.Type = contentType
|
||||
err = oper.mdb.UpdateFileByID(fileDescr.ID, fileDescr)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
} else {
|
||||
fileDescr = &descr.File{
|
||||
ID: auxuuid.NewUUID(),
|
||||
Name: filename,
|
||||
Collection: collection,
|
||||
Size: size,
|
||||
Type: contentType,
|
||||
Checksum: checksum,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
err = oper.mdb.InsertFile(fileDescr)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
}
|
||||
|
||||
err = oper.store.LinkFile(tmpname, collection, filename)
|
||||
if err != nil {
|
||||
code := http.StatusInternalServerError
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
res.Descr = fileDescr
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"mstore/app/logger"
|
||||
"mstore/app/maindb"
|
||||
"mstore/app/storage"
|
||||
)
|
||||
|
||||
type OperatorParams struct {
|
||||
MainDB *maindb.Database
|
||||
Store *storage.Storage
|
||||
}
|
||||
|
||||
type Operator struct {
|
||||
maindb *maindb.Database
|
||||
mdb *maindb.Database
|
||||
store *storage.Storage
|
||||
logg *logger.Logger
|
||||
}
|
||||
|
||||
func NewOperator(params *OperatorParams) (*Operator, error) {
|
||||
var err error
|
||||
oper := &Operator{
|
||||
maindb: params.MainDB,
|
||||
mdb: params.MainDB,
|
||||
store: params.Store,
|
||||
}
|
||||
oper.logg = logger.NewLogger("operator")
|
||||
return oper, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user