feat: add basic workings
This commit is contained in:
@@ -30,9 +30,9 @@ func ListKeys(db *gorm.DB) ([]AccessKey, error) {
|
||||
return keys, err
|
||||
}
|
||||
|
||||
func FindKeyByName(db *gorm.DB, name string) (AccessKey, error) {
|
||||
func FindKeyByKeyID(db *gorm.DB, name string) (AccessKey, error) {
|
||||
var key AccessKey
|
||||
err := db.Where("key_name = ?", name).First(&key).Error
|
||||
err := db.Where("key_id = ?", name).First(&key).Error
|
||||
return key, err
|
||||
}
|
||||
|
||||
@@ -70,9 +70,9 @@ func ListFiles(db *gorm.DB) ([]File, error) {
|
||||
return files, err
|
||||
}
|
||||
|
||||
func FindFileByName(db *gorm.DB, name string) (File, error) {
|
||||
func FindFileByFileID(db *gorm.DB, name string) (File, error) {
|
||||
var file File
|
||||
err := db.Where("file_name = ?", name).First(&file).Error
|
||||
err := db.Where("file_id = ?", name).First(&file).Error
|
||||
return file, err
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ type AccessKey struct {
|
||||
ID int `gorm:"primaryKey;not null;"`
|
||||
MetaName string
|
||||
// UUID for safe storage
|
||||
KeyName string `gorm:"not null;"`
|
||||
KeyID string `gorm:"not null;"`
|
||||
// We don't store the key itself, we hash the key
|
||||
KeyHash string `gorm:"uniqueIndex;not null;"`
|
||||
// revoked status
|
||||
@@ -83,7 +83,7 @@ type File struct {
|
||||
MediaType utility.MediaType `gorm:"type:varchar(20);not null;"`
|
||||
// the name given by the user
|
||||
MetaName string
|
||||
FileName string `gorm:"not null;"`
|
||||
FileID string `gorm:"not null;"`
|
||||
FilePath string `gorm:"not null;"`
|
||||
// hex encoded sha512 checksum
|
||||
Checksum string `gorm:"uniqueIndex;not null;"`
|
||||
|
||||
@@ -25,13 +25,13 @@ func BuildFileRecord(r io.Reader, metaName string, contentDirectory string) (Fil
|
||||
return File{}, err
|
||||
}
|
||||
|
||||
safeName := security.GenerateSafeCategoryName(category, ext)
|
||||
safeName := security.GenerateSafeName() + ext
|
||||
destPath := filepath.Join(contentDirectory, safeName)
|
||||
|
||||
f := File{
|
||||
MediaType: category,
|
||||
MetaName: metaName,
|
||||
FileName: safeName,
|
||||
FileID: safeName,
|
||||
FilePath: destPath,
|
||||
Checksum: checksum,
|
||||
}
|
||||
@@ -39,16 +39,21 @@ func BuildFileRecord(r io.Reader, metaName string, contentDirectory string) (Fil
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func BuildKeyRecord(keyHash string, metaName string, expiresAt time.Time) AccessKey {
|
||||
safeName := security.GenerateSafeName()
|
||||
func BuildKeyRecord(keyHash string, metaName string, expiresAt time.Time) (AccessKey, error) {
|
||||
now := time.Now()
|
||||
if expiresAt.Before(now) {
|
||||
return AccessKey{}, fmt.Errorf("key is already expired")
|
||||
}
|
||||
|
||||
safeName := "orbits_" + security.GenerateSafeName()
|
||||
|
||||
k := AccessKey{
|
||||
MetaName: metaName,
|
||||
KeyName: safeName,
|
||||
KeyID: safeName,
|
||||
KeyHash: keyHash,
|
||||
Revoked: false,
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
|
||||
return k
|
||||
return k, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user