uni/WEB43-diary/internal/models/image.go

40 lines
1.1 KiB
Go

package models
import (
"path/filepath"
"strconv"
"time"
"gitea.zokki.net/zokki/uni/web43-diary/context"
"gitea.zokki.net/zokki/uni/web43-diary/internal/database"
)
type Image struct {
database.SQLTable
ID uint32 `json:"id,omitempty" db:"NOT NULL PRIMARY KEY AUTO_INCREMENT"`
CreationTime time.Time `json:"creationTime,omitempty" db:"NOT NULL DEFAULT CURRENT_TIMESTAMP"`
OwnerID uint32 `json:"ownerId,omitempty" db:"NOT NULL" foreignKey:"user(ID) ON DELETE CASCADE"`
Owner *User `json:"owner,omitempty" db:"-"`
Title string `json:"title,omitempty" db:"NOT NULL"`
Type string `json:"type,omitempty" db:"NOT NULL"`
}
func init() {
database.AddCreateTableQueue(&Image{})
}
func (image *Image) LoadForeignValues(ctx *context.Context) error {
var err error
image.Owner, err = database.GetOne(ctx, &User{ID: image.OwnerID})
return err
}
func (image *Image) Path() string {
path, _ := filepath.Abs(filepath.Join("uploads", strconv.FormatUint(uint64(image.ID), 10)))
return path
}
func (image *Image) Dir() string {
return filepath.Dir(image.Path())
}