Files
mstore/app/operator/fileop_delfile.go
T

66 lines
1.5 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package operator
import (
"context"
"net/http"
"path"
)
// DeleteFile
type DeleteFileParams struct {
Filepath string
}
type DeleteFileResult struct{}
func (oper *Operator) DeleteFile(ctx context.Context, operatorID string, params *DeleteFileParams) (int, *DeleteFileResult, error) {
var err error
res := &DeleteFileResult{}
code := http.StatusOK
xfilepath, err := cleanFilepath(params.Filepath)
if err != nil {
code := http.StatusInternalServerError
return code, res, err
}
filename := path.Base(xfilepath)
collection := path.Dir(xfilepath)
resName := params.Filepath
oper.iLock.WaitAndLock(resName)
defer oper.iLock.Done(resName)
descrExists, _, err := oper.mdb.GetFileByCollectionName(ctx, collection, filename)
if err != nil {
code = http.StatusInternalServerError
return code, res, err
}
if !descrExists {
code := http.StatusNotFound
return code, res, err
}
if descrExists {
err = oper.mdb.DeleteFileByCollectionName(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
}