mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-12 15:18:38 -05:00
[server] Fix metadata for copied resources
This commit is contained in:
@@ -40,19 +40,20 @@ func (q *Queries) ChildResourceByName(ctx context.Context, arg ChildResourceByNa
|
||||
|
||||
const createResource = `-- name: CreateResource :one
|
||||
INSERT INTO resources(
|
||||
id, parent, name, dir, content_type, content_sha256
|
||||
id, parent, name, dir, content_size, content_type, content_sha256
|
||||
) VALUES (
|
||||
$1::uuid, $2::uuid, $3::text, $4::boolean,
|
||||
CASE $4 WHEN TRUE THEN '' ELSE 'text/plain' END,
|
||||
CASE $4 WhEN TRUE THEN '' ELSE 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' END
|
||||
$1::uuid, $2::uuid, $3::text, $4::boolean, $5::bigint, $6::text, $7::text
|
||||
) RETURNING id, name, parent, dir, created, modified, deleted, content_size, content_type, content_sha256
|
||||
`
|
||||
|
||||
type CreateResourceParams struct {
|
||||
ID uuid.UUID
|
||||
Parent *uuid.UUID
|
||||
Name string
|
||||
Dir bool
|
||||
ID uuid.UUID
|
||||
Parent *uuid.UUID
|
||||
Name string
|
||||
Dir bool
|
||||
ContentSize int64
|
||||
ContentType string
|
||||
ContentSha256 string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateResource(ctx context.Context, arg CreateResourceParams) (Resource, error) {
|
||||
@@ -61,6 +62,9 @@ func (q *Queries) CreateResource(ctx context.Context, arg CreateResourceParams)
|
||||
arg.Parent,
|
||||
arg.Name,
|
||||
arg.Dir,
|
||||
arg.ContentSize,
|
||||
arg.ContentType,
|
||||
arg.ContentSha256,
|
||||
)
|
||||
var i Resource
|
||||
err := row.Scan(
|
||||
|
||||
@@ -115,7 +115,16 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive bool, conflictReso
|
||||
err = r.f.db.WithTx(r.f.ctx, func(dbh *db.DbHandler) error {
|
||||
f := r.f.withDb(dbh)
|
||||
|
||||
root, created, deleted, err = f.createResource(id, destParent.ID(), destName, r.Dir(), conflictResolution)
|
||||
root, created, deleted, err = f.createResource(
|
||||
id,
|
||||
destParent.ID(),
|
||||
destName,
|
||||
r.Dir(),
|
||||
r.Info.contentSize,
|
||||
r.Info.contentType,
|
||||
r.Info.contentSHA256,
|
||||
conflictResolution,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -170,9 +179,6 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive bool, conflictReso
|
||||
}
|
||||
|
||||
info := ResourceInfoFromDBResource(root)
|
||||
info.contentSize = r.ContentSize()
|
||||
info.contentType = r.ContentType()
|
||||
info.contentSHA256 = r.ContentSHA256()
|
||||
if destParent.Path() == "/" {
|
||||
info.path = "/" + info.name
|
||||
} else {
|
||||
|
||||
@@ -11,6 +11,9 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
)
|
||||
|
||||
const emptyContentType = "text/plain"
|
||||
const emptyContentSHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
||||
|
||||
func (f filesystem) CreateResourceByPath(path string, dir, recursive bool, conflictResolution ResourceBindConflictResolution) (Resource, error) {
|
||||
path = strings.TrimRight(path, "/")
|
||||
index := strings.LastIndex(path, "/")
|
||||
@@ -52,7 +55,13 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool, conf
|
||||
var err error
|
||||
parent := r.ID()
|
||||
f := r.f.withDb(d)
|
||||
if result, created, _, err = f.createResource(id, parent, name, dir, conflictResolution); err != nil {
|
||||
contentType := ""
|
||||
contentSHA256 := ""
|
||||
if dir {
|
||||
contentType = emptyContentType
|
||||
contentSHA256 = emptyContentSHA256
|
||||
}
|
||||
if result, created, _, err = f.createResource(id, parent, name, dir, 0, contentType, contentSHA256, conflictResolution); err != nil {
|
||||
return err
|
||||
} else if created {
|
||||
return d.UpdateResourceModified(r.f.ctx, parent)
|
||||
@@ -92,10 +101,27 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool, conf
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f filesystem) createResource(id uuid.UUID, parent uuid.UUID, name string, dir bool, conflictResolution ResourceBindConflictResolution) (res db.Resource, created, deleted bool, err error) {
|
||||
func (f filesystem) createResource(
|
||||
id uuid.UUID,
|
||||
parent uuid.UUID,
|
||||
name string,
|
||||
dir bool,
|
||||
contentSize int64,
|
||||
contentType string,
|
||||
contentSHA256 string,
|
||||
conflictResolution ResourceBindConflictResolution,
|
||||
) (res db.Resource, created, deleted bool, err error) {
|
||||
err = f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
f := f.withDb(d)
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{ID: id, Parent: &parent, Name: name, Dir: dir})
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{
|
||||
ID: id,
|
||||
Parent: &parent,
|
||||
Name: name,
|
||||
ContentSize: contentSize,
|
||||
ContentType: contentType,
|
||||
ContentSha256: contentSHA256,
|
||||
Dir: dir,
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err == nil {
|
||||
@@ -122,7 +148,15 @@ func (f filesystem) createResource(id uuid.UUID, parent uuid.UUID, name string,
|
||||
}
|
||||
err = f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
f := f.withDb(d)
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{ID: id, Parent: &parent, Name: name, Dir: dir})
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{
|
||||
ID: id,
|
||||
Parent: &parent,
|
||||
Name: name,
|
||||
ContentSize: contentSize,
|
||||
ContentType: contentType,
|
||||
ContentSha256: contentSHA256,
|
||||
Dir: dir,
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -146,7 +180,16 @@ func (f filesystem) createResource(id uuid.UUID, parent uuid.UUID, name string,
|
||||
} else {
|
||||
err = f.deleteRecursive(id, parent, true, false)
|
||||
if err == nil {
|
||||
res, created, _, err = f.createResource(id, parent, name, dir, ResourceBindConflictResolutionError)
|
||||
res, created, _, err = f.createResource(
|
||||
id,
|
||||
parent,
|
||||
name,
|
||||
dir,
|
||||
contentSize,
|
||||
contentType,
|
||||
contentSHA256,
|
||||
ResourceBindConflictResolutionError,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,7 +197,16 @@ func (f filesystem) createResource(id uuid.UUID, parent uuid.UUID, name string,
|
||||
deleted = true
|
||||
err = f.deleteRecursive(id, parent, true, false)
|
||||
if err == nil {
|
||||
res, created, _, err = f.createResource(id, parent, name, dir, ResourceBindConflictResolutionError)
|
||||
res, created, _, err = f.createResource(
|
||||
id,
|
||||
parent,
|
||||
name,
|
||||
dir,
|
||||
contentSize,
|
||||
contentType,
|
||||
contentSHA256,
|
||||
ResourceBindConflictResolutionError,
|
||||
)
|
||||
}
|
||||
}
|
||||
} else if strings.Contains(err.Error(), "resources_pkey") {
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
-- name: CreateResource :one
|
||||
INSERT INTO resources(
|
||||
id, parent, name, dir, content_type, content_sha256
|
||||
id, parent, name, dir, content_size, content_type, content_sha256
|
||||
) VALUES (
|
||||
@id::uuid, sqlc.narg('parent')::uuid, @name::text, @dir::boolean,
|
||||
CASE @dir WHEN TRUE THEN '' ELSE 'text/plain' END,
|
||||
CASE @dir WhEN TRUE THEN '' ELSE 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' END
|
||||
@id::uuid, sqlc.narg('parent')::uuid, @name::text, @dir::boolean, @content_size::bigint, @content_type::text, @content_sha256::text
|
||||
) RETURNING *;
|
||||
|
||||
-- name: CreateResources :copyfrom
|
||||
|
||||
Reference in New Issue
Block a user