2017-08-19 11:35:44 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/asdine/storm"
|
2017-08-20 07:49:09 +00:00
|
|
|
"github.com/asdine/storm/q"
|
2018-04-23 21:42:52 +00:00
|
|
|
fb "github.com/filebrowser/filebrowser"
|
2017-08-19 11:35:44 +00:00
|
|
|
)
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// ShareStore is a shareable links store.
|
2017-08-19 11:35:44 +00:00
|
|
|
type ShareStore struct {
|
|
|
|
DB *storm.DB
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// Get gets a Share Link from an hash.
|
2018-04-23 21:42:52 +00:00
|
|
|
func (s ShareStore) Get(hash string) (*fb.ShareLink, error) {
|
|
|
|
var v fb.ShareLink
|
2017-08-19 11:35:44 +00:00
|
|
|
err := s.DB.One("Hash", hash, &v)
|
2017-08-20 07:42:38 +00:00
|
|
|
if err == storm.ErrNotFound {
|
2018-04-23 21:42:52 +00:00
|
|
|
return nil, fb.ErrNotExist
|
2017-08-20 07:42:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 06:33:16 +00:00
|
|
|
return &v, err
|
2017-08-19 11:35:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// GetPermanent gets the permanent link from a path.
|
2018-04-23 21:42:52 +00:00
|
|
|
func (s ShareStore) GetPermanent(path string) (*fb.ShareLink, error) {
|
|
|
|
var v fb.ShareLink
|
2017-08-20 07:49:09 +00:00
|
|
|
err := s.DB.Select(q.Eq("Path", path), q.Eq("Expires", false)).First(&v)
|
|
|
|
if err == storm.ErrNotFound {
|
2018-04-23 21:42:52 +00:00
|
|
|
return nil, fb.ErrNotExist
|
2017-08-20 07:49:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 06:33:16 +00:00
|
|
|
return &v, err
|
2017-08-20 07:49:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// GetByPath gets all the links for a specific path.
|
2018-04-23 21:42:52 +00:00
|
|
|
func (s ShareStore) GetByPath(hash string) ([]*fb.ShareLink, error) {
|
|
|
|
var v []*fb.ShareLink
|
2017-08-19 11:35:44 +00:00
|
|
|
err := s.DB.Find("Path", hash, &v)
|
2017-08-20 07:42:38 +00:00
|
|
|
if err == storm.ErrNotFound {
|
2018-04-23 21:42:52 +00:00
|
|
|
return v, fb.ErrNotExist
|
2017-08-20 07:42:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// Gets retrieves all the shareable links.
|
2018-04-23 21:42:52 +00:00
|
|
|
func (s ShareStore) Gets() ([]*fb.ShareLink, error) {
|
|
|
|
var v []*fb.ShareLink
|
2017-08-19 11:35:44 +00:00
|
|
|
err := s.DB.All(&v)
|
2017-08-21 06:33:16 +00:00
|
|
|
if err == storm.ErrNotFound {
|
2018-04-23 21:42:52 +00:00
|
|
|
return v, fb.ErrNotExist
|
2017-08-21 06:33:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 11:35:44 +00:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// Save stores a Share Link on the database.
|
2018-04-23 21:42:52 +00:00
|
|
|
func (s ShareStore) Save(l *fb.ShareLink) error {
|
2017-08-19 11:35:44 +00:00
|
|
|
return s.DB.Save(l)
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:27:03 +00:00
|
|
|
// Delete deletes a Share Link from the database.
|
2017-08-19 11:35:44 +00:00
|
|
|
func (s ShareStore) Delete(hash string) error {
|
2018-04-23 21:42:52 +00:00
|
|
|
return s.DB.DeleteStruct(&fb.ShareLink{Hash: hash})
|
2017-08-19 11:35:44 +00:00
|
|
|
}
|