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