[server] Allow creating empty resources

This commit is contained in:
Abhishek Shroff
2026-01-20 11:48:06 +05:30
parent 073a15369c
commit 169fd20aab
2 changed files with 94 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
package fs
import (
"errors"
"io"
"net/http"
"codeberg.org/shroff/phylum/server/internal/api/authenticator"
"codeberg.org/shroff/phylum/server/internal/api/v1/responses"
"codeberg.org/shroff/phylum/server/internal/core"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type createFileParams struct {
Path string `json:"path" form:"path" binding:"required"`
ID string `json:"id" form:"id" binding:"omitempty,uuid"`
VersionID string `json:"version_id" form:"version_id" binding:"omitempty,uuid"`
CreateParents bool `json:"create_parents" form:"create_parents"`
Conflict core.ResourceBindConflictResolution `json:"conflict" form:"conflict"`
SHA256 string `json:"sha256" form:"sha256"`
}
func handleCreateFileRequest(c *gin.Context) {
var params createFileParams
err := c.ShouldBindQuery(&params)
if err != nil {
panic(err)
}
var id uuid.UUID
var versionID uuid.UUID
if params.ID != "" {
id, err = uuid.Parse(params.ID)
if err != nil {
panic(err)
}
}
if params.VersionID != "" {
versionID, err = uuid.Parse(params.VersionID)
if err != nil {
panic(err)
}
}
// TODO: Calculate and verify sha sum
f := authenticator.GetFileSystem(c)
file, err := c.FormFile("contents")
if err != nil {
if !errors.Is(err, http.ErrMissingFile) {
panic(err)
}
r, err := f.CreateResourceByPath(params.Path, id, false, false, params.Conflict)
if err != nil && !errors.Is(err, core.ErrIDConflict) {
panic(err)
}
c.JSON(200, responses.ResourceFromFS(r))
return
}
err = func() error {
// TODO: #perf disk I/O in tx
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
out, err := f.CreateFileByPath(params.Path, id, versionID, params.Conflict)
if err != nil {
return err
}
if _, err := io.Copy(out, src); err != nil {
out.Close()
return err
} else {
return out.Close()
}
}()
if err != nil && !errors.Is(err, core.ErrIDConflict) {
panic(err)
}
// id may have changed if this is an overwrite
r, err := f.ResourceByPathWithRoot(params.Path)
if err != nil {
panic(err)
}
c.JSON(200, responses.ResourceFromFS(r))
}

View File

@@ -27,6 +27,7 @@ func SetupRoutes(r *gin.RouterGroup) {
group.POST("/delete_version", handleDeleteVersionRequest)
group.POST("/mkdir", handleMkdirRequest)
group.POST("/create_file", handleCreateFileRequest)
group.PUT("/upload", handleUploadRequest)
group.GET("/search", handleSearchRequest)