Add file visibility (PRIVATE/PUBLIC) + public files API

Adds a visibility enum to files with PRIVATE (default) and PUBLIC states.
PUBLIC files are accessible via an unauthenticated /api/files/v1/{fileID}
endpoint that redirects to a presigned S3 URL. Introduces pkg/file service
to manage file operations. Logo uploads (trust centers, organizations,
frameworks, references) are marked PUBLIC; other files are PRIVATE.
Includes database migration and backfill for existing logos.

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2026-03-18 17:28:10 +01:00
parent 9237ad8ce2
commit a5743729f7
20 changed files with 339 additions and 10 deletions

View File

@@ -30,16 +30,17 @@ import (
type (
File struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
BucketName string `db:"bucket_name"`
MimeType string `db:"mime_type"`
FileName string `db:"file_name"`
FileKey string `db:"file_key"`
FileSize int64 `db:"file_size"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt *time.Time `db:"deleted_at"`
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
BucketName string `db:"bucket_name"`
MimeType string `db:"mime_type"`
FileName string `db:"file_name"`
FileKey string `db:"file_key"`
FileSize int64 `db:"file_size"`
Visibility FileVisibility `db:"visibility"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt *time.Time `db:"deleted_at"`
}
Files []*File
@@ -93,6 +94,7 @@ SELECT
file_name,
file_key,
file_size,
visibility,
created_at,
updated_at,
deleted_at
@@ -145,6 +147,7 @@ INSERT INTO
file_name,
file_key,
file_size,
visibility,
created_at,
updated_at,
deleted_at
@@ -158,6 +161,7 @@ VALUES (
@file_name,
@file_key,
@file_size,
@visibility,
@created_at,
@updated_at,
@deleted_at
@@ -173,6 +177,7 @@ VALUES (
"file_name": f.FileName,
"file_key": f.FileKey,
"file_size": f.FileSize,
"visibility": f.Visibility,
"created_at": f.CreatedAt,
"updated_at": f.UpdatedAt,
"deleted_at": f.DeletedAt,
@@ -192,6 +197,55 @@ VALUES (
return nil
}
func (f *File) LoadPublicByID(
ctx context.Context,
conn pg.Conn,
fileID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
bucket_name,
mime_type,
file_name,
file_key,
file_size,
visibility,
created_at,
updated_at,
deleted_at
FROM
files
WHERE
id = @file_id
AND visibility = 'PUBLIC'
AND deleted_at IS NULL
LIMIT 1;
`
args := pgx.StrictNamedArgs{"file_id": fileID}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query file: %w", err)
}
defer rows.Close()
file, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[File])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect file: %w", err)
}
*f = file
return nil
}
func (f File) SoftDelete(ctx context.Context, conn pg.Conn, scope Scoper) error {
q := `
UPDATE files

View File

@@ -0,0 +1,57 @@
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"database/sql/driver"
"fmt"
)
type FileVisibility string
const (
FileVisibilityPrivate FileVisibility = "PRIVATE"
FileVisibilityPublic FileVisibility = "PUBLIC"
)
func (fv FileVisibility) String() string {
return string(fv)
}
func (fv *FileVisibility) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for FileVisibility: %T", value)
}
switch s {
case "PRIVATE":
*fv = FileVisibilityPrivate
case "PUBLIC":
*fv = FileVisibilityPublic
default:
return fmt.Errorf("invalid FileVisibility value: %q", s)
}
return nil
}
func (fv FileVisibility) Value() (driver.Value, error) {
return fv.String(), nil
}

View File

@@ -0,0 +1,41 @@
-- Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
--
-- Permission to use, copy, modify, and/or distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
-- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
-- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
-- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-- PERFORMANCE OF THIS SOFTWARE.
ALTER TABLE files ADD COLUMN visibility TEXT NOT NULL DEFAULT 'PRIVATE';
-- Backfill trust center logos to PUBLIC
UPDATE files SET visibility = 'PUBLIC'
WHERE id IN (
SELECT logo_file_id FROM trust_centers WHERE logo_file_id IS NOT NULL
UNION
SELECT dark_logo_file_id FROM trust_centers WHERE dark_logo_file_id IS NOT NULL
);
-- Backfill organization logos to PUBLIC
UPDATE files SET visibility = 'PUBLIC'
WHERE id IN (
SELECT logo_file_id FROM organizations WHERE logo_file_id IS NOT NULL
UNION
SELECT horizontal_logo_file_id FROM organizations WHERE horizontal_logo_file_id IS NOT NULL
);
-- Backfill framework logos to PUBLIC
UPDATE files SET visibility = 'PUBLIC'
WHERE id IN (
SELECT light_logo_file_id FROM frameworks WHERE light_logo_file_id IS NOT NULL
UNION
SELECT dark_logo_file_id FROM frameworks WHERE dark_logo_file_id IS NOT NULL
);
ALTER TABLE files ALTER COLUMN visibility DROP DEFAULT;