143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package albums
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"photodisk/internal/auth"
|
|
"photodisk/internal/config"
|
|
db "photodisk/internal/db/sqlite"
|
|
"photodisk/internal/fs"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Metrics struct {
|
|
Files int `json:"files" db:"files"`
|
|
FilesSize int `json:"files_size" db:"files_size"`
|
|
Visits int `json:"visits" db:"visits"`
|
|
Downloads int `json:"downloads" db:"downloads"`
|
|
FavLists int `json:"fav_lists" db:"fav_lists"`
|
|
}
|
|
|
|
type Comment struct {
|
|
ID string `json:"id" db:"id"`
|
|
AlbumID string `json:"album_id" db:"album_id"`
|
|
PhotoID string `json:"photo_id" db:"photo_id"`
|
|
Message string `json:"message" db:"message"`
|
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
|
}
|
|
|
|
type FavouriteList struct {
|
|
ID string `json:"id" db:"id"`
|
|
AlbumID string `json:"album_id" db:"album_id"`
|
|
PhotoID string `json:"photo_id" db:"photo_id"`
|
|
}
|
|
|
|
type Album struct {
|
|
ID string `json:"id" db:"id"`
|
|
Name string `json:"name" db:"name"`
|
|
Password string `json:"-" db:"password"`
|
|
IsActive bool `json:"is_active" db:"is_active"`
|
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
|
ExpireAt time.Time `json:"expire_at" db:"expire_at"`
|
|
Watermarked bool `json:"watermarked" db:"watermarked"`
|
|
|
|
AllowDownloads bool `json:"allow_downloads" db:"allow_downloads"`
|
|
AllowComments bool `json:"allow_comments" db:"allow_comments"`
|
|
AllowFavourite bool `json:"allow_favourite" db:"allow_favourite"`
|
|
|
|
Metrics
|
|
}
|
|
|
|
func GenerateAlbumId() string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
func CreateAlbum(album Album) error {
|
|
if strings.TrimSpace(album.Name) == "" {
|
|
return errors.New("name is empty")
|
|
}
|
|
|
|
hashedPassword := ""
|
|
if album.Password != "" {
|
|
var err error
|
|
hashedPassword, err = auth.HashPassword(album.Password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
album.Password = hashedPassword
|
|
album.Watermarked = false
|
|
album.CreatedAt = time.Now()
|
|
album.ExpireAt = time.Now().Add(config.Get().AlbumTtl)
|
|
|
|
q := `INSERT INTO albums (id, name, is_active, created_at, expire_at, password, watermarked)
|
|
VALUES (:id, :name, 1, :created_at, :expire_at, :password, :watermarked)`
|
|
_, err := db.GetDb().NamedExec(q, album)
|
|
if err != nil {
|
|
fs.DeleteAlbum(album.ID)
|
|
return err
|
|
}
|
|
|
|
err = fs.CreateAlbum(album.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteAlbum(id string) error {
|
|
if err := fs.DeleteAlbum(id); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err := db.GetDb().Exec("DELETE FROM albums WHERE id = ?", id)
|
|
return err
|
|
}
|
|
|
|
func ListAlbums() ([]Album, error) {
|
|
var albums []Album
|
|
err := db.GetDb().Select(&albums, "SELECT * FROM albums ORDER BY created_at DESC")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return albums, nil
|
|
}
|
|
|
|
func GetAlbum(id string, password string) (Album, error) {
|
|
var album Album
|
|
err := db.GetDb().Get(&album, "SELECT * FROM albums WHERE id = ?", id)
|
|
if err != nil {
|
|
return album, err
|
|
}
|
|
|
|
if album.Password != "" {
|
|
if err := auth.CheckPasswordHash(password, album.Password); err != nil {
|
|
return album, err
|
|
}
|
|
}
|
|
|
|
return album, nil
|
|
}
|
|
|
|
func UpdateAlbum(album Album) error {
|
|
if strings.TrimSpace(album.Name) == "" {
|
|
return errors.New("name is empty")
|
|
}
|
|
|
|
_, err := db.GetDb().NamedExec(`UPDATE albums SET
|
|
name = :name,
|
|
is_active = :is_active,
|
|
expire_at = :expire_at,
|
|
password = :password,
|
|
watermarked = :watermarked,
|
|
allow_downloads = :allow_download,
|
|
allow_comments = :allow_comment,
|
|
allow_favourite = :allow_favourite
|
|
WHERE id = :id`, album)
|
|
return err
|
|
}
|