Skip to content

Commit

Permalink
feat: activity
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Jul 12, 2024
1 parent 799884f commit 5257b07
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Torrent interface {
Progress() int
Stop() error
Start() error
Remove() error
Remove(deleteData bool) error
Save() string
Exists() bool
}
Expand Down
28 changes: 11 additions & 17 deletions pkg/storage/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/emersion/go-webdav"
"github.com/pkg/errors"
)

type FileInfo struct {
Path string
Size int64
Expand All @@ -22,7 +23,6 @@ type FileInfo struct {
ETag string
}


type WebdavStorage struct {
fs *webdav.Client
}
Expand All @@ -32,27 +32,22 @@ func NewWebdavStorage(url, user, password string) (*WebdavStorage, error) {
if err != nil {
return nil, errors.Wrap(err, "new webdav")
}
fs, _ := c.ReadDir(context.TODO(), "./", false)
for _, f := range fs {
log.Infof("file: %v", f)
}
return &WebdavStorage{
fs: c,
}, nil
}


func (w *WebdavStorage) Move(local, remote string) error {

err := filepath.Walk(local, func(path string, info fs.FileInfo, err error) error {
name := filepath.Join(remote, info.Name())
if info.IsDir() {

if err := w.fs.Mkdir(context.TODO(), name); err != nil {
return errors.Wrapf(err, "mkdir %v", name)
}

} else {//is file
} else { //is file
if writer, err := w.fs.Create(context.TODO(), name); err != nil {
return errors.Wrapf(err, "create file %s", name)
} else {
Expand All @@ -68,6 +63,7 @@ func (w *WebdavStorage) Move(local, remote string) error {
}
}
}
log.Infof("file copy complete: %d", name)
return nil
})
if err != nil {
Expand All @@ -76,23 +72,21 @@ func (w *WebdavStorage) Move(local, remote string) error {
return os.RemoveAll(local)
}

func (w *WebdavStorage) ReadDir(dir string) ([]FileInfo,error) {
func (w *WebdavStorage) ReadDir(dir string) ([]FileInfo, error) {
fi, err := w.fs.ReadDir(context.TODO(), dir, false)
if err != nil {
return nil, err
}
var res []FileInfo = make([]FileInfo, 0, len(fi))
for _, f := range fi {
res = append(res, FileInfo{
Path: f.Path,
Size : f.Size,
ModTime : f.ModTime,
IsDir : f.IsDir,
MIMEType : f.MIMEType,
ETag : f.ETag,

Path: f.Path,
Size: f.Size,
ModTime: f.ModTime,
IsDir: f.IsDir,
MIMEType: f.MIMEType,
ETag: f.ETag,
})
}
return res, nil
}

4 changes: 2 additions & 2 deletions pkg/transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func (t *Torrent) Start() error {
return t.c.TorrentStartIDs(context.TODO(), []int64{t.ID})
}

func (t *Torrent) Remove() error {
func (t *Torrent) Remove(deleteData bool) error {
return t.c.TorrentRemove(context.TODO(), transmissionrpc.TorrentRemovePayload{
IDs: []int64{t.ID},
DeleteLocalData: true,
DeleteLocalData: deleteData,
})
}

Expand Down
41 changes: 38 additions & 3 deletions server/activity.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package server

import "github.com/gin-gonic/gin"
import (
"polaris/log"
"strconv"

func (s *Server) GetRunningActivities(c *gin.Context) (interface{}, error) {
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)

func (s *Server) GetAllActivities(c *gin.Context) (interface{}, error) {
his := s.db.GetHistories()

return his, nil
}

func (s *Server) RemoveActivity(c *gin.Context) (interface{}, error) {
ids := c.Param("id")
id, err := strconv.Atoi(ids)
if err != nil {
return nil, errors.Wrap(err, "convert")
}
his := s.db.GetHistory(id)
if his == nil {
log.Errorf("no record of id: %d", id)
return nil, nil
}
torrent := s.tasks[his.ID]
if torrent != nil {
if err := torrent.Remove(true); err != nil {
return nil, errors.Wrap(err, "remove torrent")
}
delete(s.tasks, his.ID)
}

err = s.db.DeleteHistory(id)
if err != nil {
return nil, errors.Wrap(err, "db")
}
log.Infof("history record successful deleted: %v", his.SourceTitle)
return nil, nil
}
}
7 changes: 6 additions & 1 deletion server/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ func (s *Server) checkTasks() {
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
if t.Progress() == 100 {
log.Infof("task is done: %v", t.Name())
s.moveCompletedTask(id)
go s.moveCompletedTask(id)
}
}
}

func (s *Server) moveCompletedTask(id int) error {
torrent := s.tasks[id]
r := s.db.GetHistory(id)
s.db.SetHistoryComplete(r.ID)
tt := s.tasks[r.ID]
tt.Remove(false)
delete(s.tasks, r.ID)

series := s.db.GetSeriesDetails(r.SeriesID)
st := s.db.GetStorage(series.StorageID)
if st.Implementation == db.ImplWebdav {
Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func (s *Server) Serve() error {
setting.POST("/auth", HttpHandler(s.EnableAuth))
setting.GET("/auth", HttpHandler(s.GetAuthSetting))
}
activity := api.Group("/activity")
{
activity.GET("/", HttpHandler(s.GetAllActivities))
activity.DELETE("/:id", HttpHandler(s.RemoveActivity))
}

tv := api.Group("/tv")
{
Expand Down

0 comments on commit 5257b07

Please sign in to comment.