feat: add key creation

This commit is contained in:
2026-04-28 23:04:15 +02:00
parent 0a98ee455f
commit 88812ec865
14 changed files with 289 additions and 115 deletions
+21 -5
View File
@@ -4,31 +4,33 @@ import (
"fmt"
"io"
"log/slog"
"orbits-server/internal/shared/security"
"orbits-server/internal/shared/utility"
"path/filepath"
"time"
)
// it has been made more general for DRY purposes
// this function should only be called after manually checking the filetype
func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (File, error) {
ext := filepath.Ext(origName)
func BuildFileRecord(r io.Reader, metaName string, contentDirectory string) (File, error) {
ext := filepath.Ext(metaName)
category := utility.CategorizeMediaType(ext)
if category == utility.Unspecified {
return File{}, fmt.Errorf("unsupported filetype")
}
checksum, err := utility.GenerateHashFromReader(r)
checksum, err := security.HashFileReader(r)
if err != nil {
slog.Error("failed to calculate hash of file at given path", "error", err)
return File{}, err
}
safeName := utility.GenerateSafeName(category, ext)
safeName := security.GenerateSafeCategoryName(category, ext)
destPath := filepath.Join(contentDirectory, safeName)
f := File{
MediaType: category,
MetaName: origName,
MetaName: metaName,
FileName: safeName,
FilePath: destPath,
Checksum: checksum,
@@ -36,3 +38,17 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
return f, nil
}
func BuildKeyRecord(keyHash string, metaName string, expiresAt time.Time) AccessKey {
safeName := security.GenerateSafeName()
k := AccessKey{
MetaName: metaName,
KeyName: safeName,
KeyHash: keyHash,
Revoked: false,
ExpiresAt: expiresAt,
}
return k
}