275 lines
6.3 KiB
Go
275 lines
6.3 KiB
Go
package operator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"mstore/app/descr"
|
|
"mstore/pkg/auxtool"
|
|
"mstore/pkg/auxuuid"
|
|
)
|
|
|
|
// FileExists
|
|
type FileExistsParams struct {
|
|
Filepath string
|
|
Source string
|
|
Dest string
|
|
}
|
|
type FileExistsResult struct {
|
|
ContentType string
|
|
ContentLength string
|
|
ContentDigest string
|
|
}
|
|
|
|
func cleanFilepath(filename string) (string, error) {
|
|
filename = "/" + filename
|
|
return filepath.Clean(filename), nil
|
|
}
|
|
|
|
func (oper *Operator) FileExists(ctx context.Context, param *FileExistsParams) (int, *FileExistsResult, error) {
|
|
var err error
|
|
code := http.StatusOK
|
|
res := &FileExistsResult{}
|
|
|
|
xfilepath, err := cleanFilepath(param.Filepath)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
|
|
filename := path.Base(xfilepath)
|
|
collection := path.Dir(xfilepath)
|
|
|
|
exist, fileDescr, err := oper.mdb.GetFileByCollection(ctx, collection, filename)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
if !exist {
|
|
code = http.StatusNotFound
|
|
return code, res, err
|
|
}
|
|
res = &FileExistsResult{
|
|
ContentLength: strconv.FormatInt(fileDescr.Size, 10),
|
|
ContentType: fileDescr.Type,
|
|
ContentDigest: fileDescr.Checksum,
|
|
}
|
|
return code, res, err
|
|
}
|
|
|
|
// PutFile
|
|
type PutFileParams struct {
|
|
ContentType string
|
|
ContentLength string
|
|
Filepath string
|
|
Source io.ReadCloser
|
|
}
|
|
type PutFileResult struct{}
|
|
|
|
const defaultContentType = "application/octet-stream"
|
|
|
|
func (oper *Operator) PutFile(ctx context.Context, param *PutFileParams) (int, *PutFileResult, error) {
|
|
var err error
|
|
res := &PutFileResult{}
|
|
|
|
if param.ContentLength == "" {
|
|
code := http.StatusLengthRequired
|
|
err = fmt.Errorf("Content-Length is empty")
|
|
return code, res, err
|
|
}
|
|
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
|
|
}
|
|
|
|
// TODO: convert file path to a unified and secure state
|
|
xfilepath, err := cleanFilepath(param.Filepath)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
filename := path.Base(xfilepath)
|
|
collection := path.Dir(xfilepath)
|
|
|
|
tmpname, size, checksum, err := oper.store.WriteTempFile(param.Source)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
|
|
descrExists, fileDescr, err := oper.mdb.GetFileByCollection(ctx, collection, filename)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
|
|
now := auxtool.TimeNow()
|
|
if descrExists {
|
|
fileDescr.Size = size
|
|
fileDescr.Checksum = checksum
|
|
fileDescr.UpdatedAt = now
|
|
fileDescr.Type = contentType
|
|
err = oper.mdb.UpdateFileByID(ctx, 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(ctx, fileDescr)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
}
|
|
|
|
err = oper.store.HardlinkFile(tmpname, collection, filename)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
code := http.StatusOK
|
|
return code, res, err
|
|
}
|
|
|
|
// GetFile
|
|
type GetFileParams struct {
|
|
Filepath string
|
|
}
|
|
type GetFileResult struct {
|
|
ContentType string
|
|
ContentLength string
|
|
ContentDigest string
|
|
Source io.ReadCloser
|
|
}
|
|
|
|
func (oper *Operator) GetFile(ctx context.Context, param *GetFileParams) (int, *GetFileResult, error) {
|
|
var err error
|
|
res := &GetFileResult{}
|
|
|
|
// TODO: convert file path to a unified and secure state
|
|
|
|
xfilepath, err := cleanFilepath(param.Filepath)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
filename := path.Base(xfilepath)
|
|
collection := path.Dir(xfilepath)
|
|
|
|
descrExists, fileDescr, err := oper.mdb.GetFileByCollection(ctx, collection, filename)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
if !descrExists {
|
|
code := http.StatusNotFound
|
|
return code, res, err
|
|
}
|
|
reader, err := oper.store.GetFileReader(collection, filename)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
res = &GetFileResult{
|
|
ContentLength: strconv.FormatInt(fileDescr.Size, 10),
|
|
ContentType: fileDescr.Type,
|
|
ContentDigest: fileDescr.Checksum,
|
|
Source: reader,
|
|
}
|
|
code := http.StatusOK
|
|
return code, res, err
|
|
}
|
|
|
|
// DeleteFile
|
|
type DeleteFileParams struct {
|
|
Filepath string
|
|
}
|
|
type DeleteFileResult struct{}
|
|
|
|
func (oper *Operator) DeleteFile(ctx context.Context, param *DeleteFileParams) (int, *DeleteFileResult, error) {
|
|
var err error
|
|
res := &DeleteFileResult{}
|
|
code := http.StatusOK
|
|
|
|
xfilepath, err := cleanFilepath(param.Filepath)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
filename := path.Base(xfilepath)
|
|
collection := path.Dir(xfilepath)
|
|
|
|
exist, _, err := oper.mdb.GetFileByCollection(ctx, collection, filename)
|
|
if err != nil {
|
|
code = http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
if exist {
|
|
err = oper.mdb.DeleteFileByCollection(ctx, collection, filename)
|
|
if err != nil {
|
|
code = http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
}
|
|
err = oper.store.DeleteFile(collection, filename)
|
|
if err != nil {
|
|
code = http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
return code, res, err
|
|
}
|
|
|
|
// ListFiles
|
|
type ListFilesParams struct {
|
|
Filepath string
|
|
}
|
|
type ListFilesResult struct {
|
|
FileDescrs []descr.File
|
|
}
|
|
|
|
func (oper *Operator) ListFiles(ctx context.Context, param *ListFilesParams) (int, *ListFilesResult, error) {
|
|
var err error
|
|
res := &ListFilesResult{
|
|
FileDescrs: make([]descr.File, 0),
|
|
}
|
|
|
|
// TODO: convert file path to a unified and secure state
|
|
|
|
_, err = cleanFilepath(param.Filepath)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
|
|
fileDescrs, err := oper.mdb.ListAllFiles(ctx)
|
|
if err != nil {
|
|
code := http.StatusInternalServerError
|
|
return code, res, err
|
|
}
|
|
for _, item := range fileDescrs {
|
|
res.FileDescrs = append(res.FileDescrs, item)
|
|
}
|
|
code := http.StatusOK
|
|
return code, res, err
|
|
}
|