diff --git a/server/internal/core/util.go b/server/internal/core/content_props.go similarity index 67% rename from server/internal/core/util.go rename to server/internal/core/content_props.go index 0a4ede11..d8bc7859 100644 --- a/server/internal/core/util.go +++ b/server/internal/core/content_props.go @@ -4,12 +4,11 @@ import ( "crypto/sha256" "hash" "io" - "strings" "github.com/gabriel-vasile/mimetype" ) -type contentPropsComputer struct { +type contentProps struct { dest io.WriteCloser len int sum hash.Hash @@ -17,8 +16,8 @@ type contentPropsComputer struct { successCallback func(int, hash.Hash, string) error } -func computeProps(dest io.WriteCloser, successCallback func(int, hash.Hash, string) error) *contentPropsComputer { - return &contentPropsComputer{ +func computeProps(dest io.WriteCloser, successCallback func(int, hash.Hash, string) error) io.WriteCloser { + return &contentProps{ dest: dest, sum: sha256.New(), contents: make([]byte, 0, 3072), @@ -26,7 +25,7 @@ func computeProps(dest io.WriteCloser, successCallback func(int, hash.Hash, stri } } -func (c *contentPropsComputer) Write(p []byte) (n int, err error) { +func (c *contentProps) Write(p []byte) (n int, err error) { n, err = c.dest.Write(p) c.len += n if c.sum != nil { @@ -47,15 +46,9 @@ func min(a, b int) int { } -func (c *contentPropsComputer) Close() error { +func (c *contentProps) Close() error { if err := c.dest.Close(); err != nil { return err } return c.successCallback(c.len, c.sum, mimetype.Detect(c.contents).String()) } - -func CheckResourceNameInvalid(s string) bool { - return s == "" || s == "." || s == ".." || strings.ContainsFunc(s, func(r rune) bool { - return r == 0 || r == '/' - }) -} diff --git a/server/internal/core/core.go b/server/internal/core/core.go index 92fb3c8e..ffcc5574 100644 --- a/server/internal/core/core.go +++ b/server/internal/core/core.go @@ -49,10 +49,10 @@ type FileSystem interface { Copy(r Resource, target string, id uuid.UUID, recursive bool, conflictResolution ResourceBindConflictResolution) (Resource, bool, error) Move(r Resource, target string, conflictResolution ResourceBindConflictResolution) (Resource, bool, error) - // permissions.go + // resource_permissions.go UpdatePermissions(r Resource, user User, permission Permission) (Resource, error) - //publink.go + // resource_publink.go CreatePublink(r Resource, id, password string, expires pgtype.Timestamp, accessLimit int) error ListPublinks(r Resource) ([]Publink, error) diff --git a/server/internal/core/publink.go b/server/internal/core/publink.go index 6182ceb6..43f3126d 100644 --- a/server/internal/core/publink.go +++ b/server/internal/core/publink.go @@ -1,13 +1,10 @@ package core import ( - "strings" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - "github.com/shroff/phylum/server/internal/crypt" ) type Publink struct { @@ -39,42 +36,3 @@ func scanPublink(row pgx.CollectableRow) (Publink, error) { } return p, nil } - -func (f filesystem) CreatePublink(r Resource, id, password string, expires pgtype.Timestamp, accessLimit int) error { - if !r.hasPermission(PermissionShare | PermissionRead) { - return ErrInsufficientPermissions - } - if r.deleted.Valid { - return ErrResourceDeleted - } - - passwordHash := "" - - if password != "" { - hash, err := crypt.Generate(password) - if err != nil { - return err - } - passwordHash = hash - } - - const q = `INSERT INTO publinks(id, root, password_hash, expires, access_limit) VALUES - ($1::text, $2::uuid, $3::text, $4::timestamp, $5::int)` - - _, err := f.db.Exec(q, id, r.ID(), passwordHash, expires, accessLimit) - if err != nil && strings.Contains(err.Error(), "publinks_pkey") { - return ErrPublinkNameConflict - } - return err -} - -func (f filesystem) ListPublinks(r Resource) ([]Publink, error) { - const q = "SELECT * FROM publinks WHERE root = $1::UUID" - if rows, err := f.db.Query(q, r.id); err != nil { - return nil, err - } else if links, err := pgx.CollectRows(rows, scanPublink); err != nil { - return nil, err - } else { - return links, nil - } -} diff --git a/server/internal/core/resource_create.go b/server/internal/core/resource_create.go index 82112748..494dc1ac 100644 --- a/server/internal/core/resource_create.go +++ b/server/internal/core/resource_create.go @@ -22,6 +22,12 @@ const ( ResourceBindConflictResolutionDelete = ResourceBindConflictResolution(4) // Delete existing resource before creating ) +func CheckResourceNameInvalid(s string) bool { + return s == "" || s == "." || s == ".." || strings.ContainsFunc(s, func(r rune) bool { + return r == 0 || r == '/' + }) +} + func (f filesystem) CreateResourceByPath(path string, id uuid.UUID, dir, createParents bool, conflictResolution ResourceBindConflictResolution) (Resource, error) { if id == uuid.Nil { id, _ = uuid.NewV7() diff --git a/server/internal/core/permissions.go b/server/internal/core/resource_permissions.go similarity index 100% rename from server/internal/core/permissions.go rename to server/internal/core/resource_permissions.go diff --git a/server/internal/core/resource_publink.go b/server/internal/core/resource_publink.go new file mode 100644 index 00000000..bf31c63e --- /dev/null +++ b/server/internal/core/resource_publink.go @@ -0,0 +1,48 @@ +package core + +import ( + "strings" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + "github.com/shroff/phylum/server/internal/crypt" +) + +func (f filesystem) CreatePublink(r Resource, id, password string, expires pgtype.Timestamp, accessLimit int) error { + if !r.hasPermission(PermissionShare | PermissionRead) { + return ErrInsufficientPermissions + } + if r.deleted.Valid { + return ErrResourceDeleted + } + + passwordHash := "" + + if password != "" { + hash, err := crypt.Generate(password) + if err != nil { + return err + } + passwordHash = hash + } + + const q = `INSERT INTO publinks(id, root, password_hash, expires, access_limit) VALUES + ($1::text, $2::uuid, $3::text, $4::timestamp, $5::int)` + + _, err := f.db.Exec(q, id, r.ID(), passwordHash, expires, accessLimit) + if err != nil && strings.Contains(err.Error(), "publinks_pkey") { + return ErrPublinkNameConflict + } + return err +} + +func (f filesystem) ListPublinks(r Resource) ([]Publink, error) { + const q = "SELECT * FROM publinks WHERE root = $1::UUID" + if rows, err := f.db.Query(q, r.id); err != nil { + return nil, err + } else if links, err := pgx.CollectRows(rows, scanPublink); err != nil { + return nil, err + } else { + return links, nil + } +} diff --git a/server/internal/core/manager.go b/server/internal/core/user_manager.go similarity index 100% rename from server/internal/core/manager.go rename to server/internal/core/user_manager.go diff --git a/server/internal/core/update.go b/server/internal/core/user_update.go similarity index 100% rename from server/internal/core/update.go rename to server/internal/core/user_update.go