mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-08 20:49:45 -06:00
[server][core] Inline some publink queries
This commit is contained in:
@@ -44,28 +44,6 @@ func (q *Queries) CreatePublink(ctx context.Context, arg CreatePublinkParams) er
|
||||
return err
|
||||
}
|
||||
|
||||
const getPublink = `-- name: GetPublink :one
|
||||
SELECT id, name, created, deleted, created_by, root, accessed, max_accesses, password_hash, expires FROM publinks p WHERE name = $1::text AND deleted IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) GetPublink(ctx context.Context, name string) (Publink, error) {
|
||||
row := q.db.QueryRow(ctx, getPublink, name)
|
||||
var i Publink
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Created,
|
||||
&i.Deleted,
|
||||
&i.CreatedBy,
|
||||
&i.Root,
|
||||
&i.Accessed,
|
||||
&i.MaxAccesses,
|
||||
&i.PasswordHash,
|
||||
&i.Expires,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const markPublinkAccess = `-- name: MarkPublinkAccess :exec
|
||||
UPDATE publinks SET accessed = accessed + 1 WHERE id = $1
|
||||
`
|
||||
@@ -73,39 +51,4 @@ UPDATE publinks SET accessed = accessed + 1 WHERE id = $1
|
||||
func (q *Queries) MarkPublinkAccess(ctx context.Context, id int32) error {
|
||||
_, err := q.db.Exec(ctx, markPublinkAccess, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const publinksByCreator = `-- name: PublinksByCreator :many
|
||||
SELECT id, name, created, deleted, created_by, root, accessed, max_accesses, password_hash, expires FROM publinks WHERE created_by = $1::text
|
||||
`
|
||||
|
||||
func (q *Queries) PublinksByCreator(ctx context.Context, username string) ([]Publink, error) {
|
||||
rows, err := q.db.Query(ctx, publinksByCreator, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Publink
|
||||
for rows.Next() {
|
||||
var i Publink
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Created,
|
||||
&i.Deleted,
|
||||
&i.CreatedBy,
|
||||
&i.Root,
|
||||
&i.Accessed,
|
||||
&i.MaxAccesses,
|
||||
&i.PasswordHash,
|
||||
&i.Expires,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
}
|
||||
@@ -69,22 +69,17 @@ func Open(ctx context.Context, username string, root uuid.UUID, fullAccess bool)
|
||||
}
|
||||
|
||||
func OpenFromPublink(ctx context.Context, name string, password string, path string) (Resource, error) {
|
||||
d := db.Get()
|
||||
// Check exists
|
||||
s, err := d.GetPublink(ctx, name)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = ErrParentNotFound
|
||||
}
|
||||
link, err := getPublink(ctx, name)
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
|
||||
// check password
|
||||
if s.PasswordHash != "" {
|
||||
if link.PasswordHash != "" {
|
||||
if password == "" {
|
||||
return Resource{}, ErrInsufficientPermissions
|
||||
}
|
||||
if ok, err := crypt.VerifyPassword(password, s.PasswordHash); err != nil {
|
||||
if ok, err := crypt.VerifyPassword(password, link.PasswordHash); err != nil {
|
||||
return Resource{}, err
|
||||
} else if !ok {
|
||||
return Resource{}, ErrInsufficientPermissions
|
||||
@@ -92,12 +87,12 @@ func OpenFromPublink(ctx context.Context, name string, password string, path str
|
||||
}
|
||||
|
||||
// check expiration
|
||||
if s.Expires.Valid && s.Expires.Time.Before(time.Now()) {
|
||||
if link.Expires.Valid && link.Expires.Time.Before(time.Now()) {
|
||||
return Resource{}, ErrResourceNotFound
|
||||
}
|
||||
|
||||
// check max accesses
|
||||
if s.MaxAccesses > 0 && s.MaxAccesses <= s.Accessed {
|
||||
if link.MaxAccesses > 0 && link.MaxAccesses <= link.Accessed {
|
||||
return Resource{}, ErrResourceNotFound
|
||||
}
|
||||
|
||||
@@ -105,13 +100,27 @@ func OpenFromPublink(ctx context.Context, name string, password string, path str
|
||||
ctx: ctx,
|
||||
db: db.Get(),
|
||||
cs: storage.Get(),
|
||||
rootID: s.Root,
|
||||
rootID: link.Root,
|
||||
fullAccess: true, // TODO: #permissions Replace with permissions int
|
||||
}
|
||||
|
||||
return f.ResourceByPath(path)
|
||||
}
|
||||
|
||||
func getPublink(ctx context.Context, name string) (Publink, error) {
|
||||
q := "SELECT FROM publinks p WHERE name = $1::TEXT AND deleted IS NULL"
|
||||
if rows, err := db.Get().Query(ctx, q, name); err != nil {
|
||||
return Publink{}, err
|
||||
} else if link, err := pgx.CollectExactlyOneRow(rows, scanPublink); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = ErrParentNotFound
|
||||
}
|
||||
return Publink{}, err
|
||||
} else {
|
||||
return link, nil
|
||||
}
|
||||
}
|
||||
|
||||
func CheckNameInvalid(s string) bool {
|
||||
return strings.ContainsFunc(s, func(r rune) bool {
|
||||
return r == 0 || r == '/'
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
-- name: GetPublink :one
|
||||
SELECT * FROM publinks p WHERE name = @name::text AND deleted IS NULL;
|
||||
|
||||
-- name: MarkPublinkAccess :exec
|
||||
UPDATE publinks SET accessed = accessed + 1 WHERE id = $1;
|
||||
|
||||
@@ -12,7 +9,4 @@ INSERT INTO publinks(name, created_by, root, password_hash, expires, max_accesse
|
||||
@password_hash::text,
|
||||
sqlc.narg('expires')::timestamp,
|
||||
@max_accesses::int
|
||||
);
|
||||
|
||||
-- name: PublinksByCreator :many
|
||||
SELECT * FROM publinks WHERE created_by = @username::text;
|
||||
);
|
||||
Reference in New Issue
Block a user