first draft of file storage

This commit is contained in:
2026-01-28 15:33:19 +02:00
parent d647854e98
commit d6496a427b
7 changed files with 394 additions and 158 deletions
+71 -14
View File
@@ -18,21 +18,28 @@ type FileExistsParams struct {
Dest string
}
type FileExistsResult struct {
Descr *descr.File
ContentType string
ContentLength string
ContentDigest string
}
func (oper *Operator) FileExists(param *FileExistsParams) (int, *FileExistsResult, error) {
var err error
code := http.StatusNotFound
code := http.StatusOK
res := &FileExistsResult{}
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
exist, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
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
}
@@ -44,9 +51,7 @@ type PutFileParams struct {
Filepath string
Source io.ReadCloser
}
type PutFileResult struct {
Descr *descr.File
}
type PutFileResult struct{}
const defaultContentType = "application/octet-stream"
@@ -64,6 +69,8 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
contentType = defaultContentType
}
// TODO: convert file path to a unified and secure state
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
oper.logg.Debugf("Put file %s %s", collection, filename)
@@ -74,14 +81,14 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
return code, res, err
}
exists, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
descrExists, fileDescr, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
now := auxtool.TimeNow()
if exists {
if descrExists {
fileDescr.Size = size
fileDescr.Checksum = checksum
fileDescr.UpdatedAt = now
@@ -109,13 +116,11 @@ func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error)
}
}
err = oper.store.LinkFile(tmpname, collection, filename)
err = oper.store.HardlinkFile(tmpname, collection, filename)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
res.Descr = fileDescr
code := http.StatusOK
return code, res, err
}
@@ -127,12 +132,41 @@ type GetFileParams struct {
type GetFileResult struct {
ContentType string
ContentLength string
ContentDigest string
Source io.ReadCloser
}
func (oper *Operator) GetFile(param *GetFileParams) (int, *GetFileResult, error) {
var err error
res := &GetFileResult{}
// TODO: convert file path to a unified and secure state
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
oper.logg.Debugf("Gut file %s %s", collection, filename)
descrExists, fileDescr, err := oper.mdb.GetFileByCollection(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
}
@@ -147,5 +181,28 @@ func (oper *Operator) DeleteFile(param *DeleteFileParams) (int, *DeleteFileResul
var err error
res := &DeleteFileResult{}
code := http.StatusOK
filename := path.Base(param.Filepath)
collection := path.Dir(param.Filepath)
exist, _, err := oper.mdb.GetFileByCollection(collection, filename)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if exist {
err = oper.mdb.DeleteFileByCollection(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
}