filebrowser/storage/bolt/share.go

78 lines
1.6 KiB
Go
Raw Normal View History

package bolt
import (
2024-04-01 16:24:06 +00:00
"errors"
"github.com/asdine/storm/v3"
"github.com/asdine/storm/v3/q"
2020-05-31 23:12:36 +00:00
2024-04-01 16:24:06 +00:00
fbErrors "github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/share"
)
type shareBackend struct {
db *storm.DB
}
func (s shareBackend) All() ([]*share.Link, error) {
var v []*share.Link
err := s.db.All(&v)
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
return v, fbErrors.ErrNotExist
}
return v, err
}
func (s shareBackend) FindByUserID(id uint) ([]*share.Link, error) {
var v []*share.Link
err := s.db.Select(q.Eq("UserID", id)).Find(&v)
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
return v, fbErrors.ErrNotExist
}
return v, err
}
func (s shareBackend) GetByHash(hash string) (*share.Link, error) {
var v share.Link
err := s.db.One("Hash", hash, &v)
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
return nil, fbErrors.ErrNotExist
}
return &v, err
}
func (s shareBackend) GetPermanent(path string, id uint) (*share.Link, error) {
var v share.Link
err := s.db.Select(q.Eq("Path", path), q.Eq("Expire", 0), q.Eq("UserID", id)).First(&v)
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
return nil, fbErrors.ErrNotExist
}
return &v, err
}
func (s shareBackend) Gets(path string, id uint) ([]*share.Link, error) {
var v []*share.Link
err := s.db.Select(q.Eq("Path", path), q.Eq("UserID", id)).Find(&v)
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
return v, fbErrors.ErrNotExist
}
return v, err
}
func (s shareBackend) Save(l *share.Link) error {
return s.db.Save(l)
}
func (s shareBackend) Delete(hash string) error {
2020-05-31 23:12:36 +00:00
err := s.db.DeleteStruct(&share.Link{Hash: hash})
2024-04-01 16:24:06 +00:00
if errors.Is(err, storm.ErrNotFound) {
2020-05-31 23:12:36 +00:00
return nil
}
return err
}