2019-01-05 22:44:33 +00:00
|
|
|
package share
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StorageBackend is the interface to implement for a share storage.
|
|
|
|
type StorageBackend interface {
|
2020-12-24 18:02:28 +00:00
|
|
|
All() ([]*Link, error)
|
|
|
|
FindByUserID(id uint) ([]*Link, error)
|
2019-01-05 22:44:33 +00:00
|
|
|
GetByHash(hash string) (*Link, error)
|
|
|
|
GetPermanent(path string, id uint) (*Link, error)
|
|
|
|
Gets(path string, id uint) ([]*Link, error)
|
|
|
|
Save(s *Link) error
|
|
|
|
Delete(hash string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage is a storage.
|
|
|
|
type Storage struct {
|
|
|
|
back StorageBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorage creates a share links storage from a backend.
|
|
|
|
func NewStorage(back StorageBackend) *Storage {
|
|
|
|
return &Storage{back: back}
|
|
|
|
}
|
|
|
|
|
2020-12-24 18:02:28 +00:00
|
|
|
// All wraps a StorageBackend.All.
|
|
|
|
func (s *Storage) All() ([]*Link, error) {
|
|
|
|
links, err := s.back.All()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, link := range links {
|
|
|
|
if link.Expire != 0 && link.Expire <= time.Now().Unix() {
|
|
|
|
if err := s.Delete(link.Hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links[:i], links[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindByUserID wraps a StorageBackend.FindByUserID.
|
|
|
|
func (s *Storage) FindByUserID(id uint) ([]*Link, error) {
|
|
|
|
links, err := s.back.FindByUserID(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, link := range links {
|
|
|
|
if link.Expire != 0 && link.Expire <= time.Now().Unix() {
|
|
|
|
if err := s.Delete(link.Hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links[:i], links[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// GetByHash wraps a StorageBackend.GetByHash.
|
|
|
|
func (s *Storage) GetByHash(hash string) (*Link, error) {
|
|
|
|
link, err := s.back.GetByHash(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Expire != 0 && link.Expire <= time.Now().Unix() {
|
2020-05-31 23:12:36 +00:00
|
|
|
if err := s.Delete(link.Hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
return nil, errors.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermanent wraps a StorageBackend.GetPermanent
|
|
|
|
func (s *Storage) GetPermanent(path string, id uint) (*Link, error) {
|
|
|
|
return s.back.GetPermanent(path, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets wraps a StorageBackend.Gets
|
|
|
|
func (s *Storage) Gets(path string, id uint) ([]*Link, error) {
|
|
|
|
links, err := s.back.Gets(path, id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, link := range links {
|
|
|
|
if link.Expire != 0 && link.Expire <= time.Now().Unix() {
|
2020-05-31 23:12:36 +00:00
|
|
|
if err := s.Delete(link.Hash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
links = append(links[:i], links[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save wraps a StorageBackend.Save
|
|
|
|
func (s *Storage) Save(l *Link) error {
|
|
|
|
return s.back.Save(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete wraps a StorageBackend.Delete
|
|
|
|
func (s *Storage) Delete(hash string) error {
|
|
|
|
return s.back.Delete(hash)
|
|
|
|
}
|