feat: add key creation
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user