You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
feat: v2 (#599)
Read https://github.com/filebrowser/filebrowser/pull/575.
Former-commit-id: 7aedcaaf72b863033e3f089d6df308d41a3fd00c [formerly bdbe4d49161b901c4adf9c245895a1be2d62e4a7] [formerly acfc1ec67c423e0b3e065a8c1f8897c5249af65b [formerly d309066def]]
Former-commit-id: 0c7d925a38a68ccabdf2c4bbd8c302ee89b93509 [formerly a6173925a1382955d93b334ded93f70d6dddd694]
Former-commit-id: e032e0804dd051df86f42962de2b39caec5318b7
This commit is contained in:
74
share/storage.go
Normal file
74
share/storage.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/errors"
|
||||
)
|
||||
|
||||
// StorageBackend is the interface to implement for a share storage.
|
||||
type StorageBackend interface {
|
||||
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}
|
||||
}
|
||||
|
||||
// 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() {
|
||||
s.Delete(link.Hash)
|
||||
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() {
|
||||
s.Delete(link.Hash)
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user