Skip to content

Commit

Permalink
feat: add option to delete storage media files
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Dec 11, 2024
1 parent 22f76e3 commit 6d127c6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 13 deletions.
4 changes: 4 additions & 0 deletions pkg/storage/alist.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ func (a *Alist) UploadProgress() float64 {
}
return a.progresser()
}

func (a *Alist) RemoveAll(path string) error {
return nil
}
1 change: 1 addition & 0 deletions pkg/storage/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Storage interface {
ReadFile(string) ([]byte, error)
WriteFile(string, []byte) error
UploadProgress() float64
RemoveAll(path string) error
}

type uploadFunc func(destPath string, destInfo fs.FileInfo, srcReader io.Reader, mimeType *mimetype.MIME) error
Expand Down
4 changes: 4 additions & 0 deletions pkg/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ func (l *LocalStorage) WriteFile(name string, data []byte) error {
func (l *LocalStorage) UploadProgress() float64 {
return 0
}

func (i *LocalStorage) RemoveAll(path string) error {
return os.RemoveAll(path)
}
4 changes: 4 additions & 0 deletions pkg/storage/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ func (w *WebdavStorage) UploadProgress() float64 {
}
return w.progresser()
}

func (w *WebdavStorage) RemoveAll(path string) error {
return w.fs.RemoveAll(path)
}
2 changes: 1 addition & 1 deletion server/core/importlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (c *Client) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) {
}

func (c *Client) checkMovieFolder(m *ent.Media) error {
var storageImpl, err = c.getStorage(m.StorageID, media.MediaTypeMovie)
var storageImpl, err = c.GetStorage(m.StorageID, media.MediaTypeMovie)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions server/core/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Client) writeNfoFile(historyId int) error {
}

if md.MediaType == media.MediaTypeTv { //tvshow.nfo
st, err := c.getStorage(md.StorageID, media.MediaTypeTv)
st, err := c.GetStorage(md.StorageID, media.MediaTypeTv)
if err != nil {
return errors.Wrap(err, "get storage")
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *Client) writeNfoFile(historyId int) error {
}

} else if md.MediaType == media.MediaTypeMovie { //movie.nfo
st, err := c.getStorage(md.StorageID, media.MediaTypeMovie)
st, err := c.GetStorage(md.StorageID, media.MediaTypeMovie)
if err != nil {
return errors.Wrap(err, "get storage")
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *Client) writePlexmatch(historyId int) error {
if series.MediaType != media.MediaTypeTv { //.plexmatch only support tv series
return nil
}
st, err := c.getStorage(series.StorageID, media.MediaTypeTv)
st, err := c.GetStorage(series.StorageID, media.MediaTypeTv)
if err != nil {
return errors.Wrap(err, "get storage")
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *Client) nfoSupportEnabled() bool {
return c.db.GetSetting(db.SettingNfoSupportEnabled) == "true"
}

func (c *Client) getStorage(storageId int, mediaType media.MediaType) (storage.Storage, error) {
func (c *Client) GetStorage(storageId int, mediaType media.MediaType) (storage.Storage, error) {
st := c.db.GetStorage(storageId)
targetPath := st.TvPath
if mediaType == media.MediaTypeMovie {
Expand Down
4 changes: 2 additions & 2 deletions server/core/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *Client) moveCompletedTask(id int) (err1 error) {
}
st := c.db.GetStorage(series.StorageID)
log.Infof("move task files to target dir: %v", r.TargetDir)
stImpl, err := c.getStorage(st.ID, series.MediaType)
stImpl, err := c.GetStorage(st.ID, series.MediaType)
if err != nil {
return err
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (c *Client) CheckDownloadedSeriesFiles(m *ent.Media) error {
}
log.Infof("check files in directory: %s", m.TargetDir)

var storageImpl, err = c.getStorage(m.StorageID, media.MediaTypeTv)
var storageImpl, err = c.GetStorage(m.StorageID, media.MediaTypeTv)
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions server/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"polaris/log"
"polaris/server/core"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
Expand Down Expand Up @@ -170,9 +171,30 @@ func (s *Server) DeleteFromWatchlist(c *gin.Context) (interface{}, error) {
if err != nil {
return nil, errors.Wrap(err, "convert")
}

deleteFiles := c.Query("delete_files")
if strings.ToLower(deleteFiles) == "true" {
//will delete local media file
log.Infof("will delete local media files for %d", id)
m, err := s.db.GetMedia(id)
if err != nil {
log.Warnf("get media: %v", err)
} else {
st, err := s.core.GetStorage(m.StorageID, m.MediaType)
if err != nil {
log.Warnf("get storage error: %v", err)
} else {
if err := st.RemoveAll(m.TargetDir); err != nil {
log.Warnf("remove all : %v", err)
}
}
}
}

if err := s.db.DeleteMedia(id); err != nil {
return nil, errors.Wrap(err, "delete db")
}
os.RemoveAll(filepath.Join(db.ImgPath, ids)) //delete image related

return "success", nil
}
6 changes: 3 additions & 3 deletions ui/lib/providers/series_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class SeriesDetailData
return SeriesDetails.fromJson(rsp.data);
}

Future<void> delete() async {
final dio = await APIs.getDio();
var resp = await dio.delete("${APIs.seriesDetailUrl}$id");
Future<void> delete(bool removeFiles ) async {
final dio = APIs.getDio();
var resp = await dio.delete("${APIs.seriesDetailUrl}$id", queryParameters: {"delete_files": removeFiles});
var rsp = ServerResponse.fromJson(resp.data);
if (rsp.code != 0) {
throw rsp.message;
Expand Down
18 changes: 15 additions & 3 deletions ui/lib/widgets/detail_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,25 @@ class _DetailCardState extends ConsumerState<DetailCard> {
}

Future<void> showConfirmDialog(BuildContext oriContext) {
var deleteFiles = false;
return showDialog<void>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("确认删除:"),
content: Text("${widget.details.name}"),
title: const Text("确认删除"),
content: StatefulBuilder(builder: (context, setState) {
return CheckboxListTile(
value: deleteFiles,
title: Text("删除媒体文件"),
onChanged: (v) {
setState(
() {
deleteFiles = v!;
},
);
});
}),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
Expand All @@ -209,7 +221,7 @@ class _DetailCardState extends ConsumerState<DetailCard> {
ref
.read(mediaDetailsProvider(widget.details.id.toString())
.notifier)
.delete()
.delete(deleteFiles)
.then((v) {
if (oriContext.mounted) {
oriContext.go(widget.details.mediaType == "tv"
Expand Down

0 comments on commit 6d127c6

Please sign in to comment.