feat(download-client): add label to Deluge if it does not exist (#1761)

* feat(download-client): Add label to deluge if it does not exist

* refactor(downloadclient): deluge set label

---------

Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
KyokoMiki 2024-10-20 20:04:00 +08:00 committed by GitHub
parent 5df6e78d3b
commit ae779e5461
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -137,9 +137,7 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
}
if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = labelPluginActive.SetTorrentLabel(ctx, torrentHash, action.Label)
if err != nil {
if err := delugeSetOrCreateTorrentLabel(ctx, labelPluginActive, client.Name, torrentHash, action.Label); err != nil {
return nil, errors.Wrap(err, "could not set label: %s on client: %s", action.Label, client.Name)
}
}
@ -185,10 +183,8 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
}
if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = labelPluginActive.SetTorrentLabel(ctx, torrentHash, action.Label)
if err != nil {
return nil, errors.Wrap(err, "could not set label: %v on client: %s", action.Label, client.Name)
if err := delugeSetOrCreateTorrentLabel(ctx, labelPluginActive, client.Name, torrentHash, action.Label); err != nil {
return nil, errors.Wrap(err, "could not set label: %s on client: %s", action.Label, client.Name)
}
}
}
@ -199,6 +195,29 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
return nil, nil
}
// delugeSetOrCreateTorrentLabel set torrent label if it exists or create label if it does not
func delugeSetOrCreateTorrentLabel(ctx context.Context, plugin *deluge.LabelPlugin, clientName string, hash string, label string) error {
err := plugin.SetTorrentLabel(ctx, hash, label)
if err != nil {
// if label does not exist the client will throw an RPC error.
// We can parse that and check for specific error for Unknown Label and then create the label
var rpcErr deluge.RPCError
if errors.As(err, &rpcErr) && rpcErr.ExceptionMessage == "Unknown Label" {
if addErr := plugin.AddLabel(ctx, label); addErr != nil {
return errors.Wrap(addErr, "could not add label: %s on client: %s", label, clientName)
}
if err = plugin.SetTorrentLabel(ctx, hash, label); err != nil {
return errors.Wrap(err, "could not set label: %s on client: %s", label, clientName)
}
} else {
return errors.Wrap(err, "could not set label: %s on client: %s", label, clientName)
}
}
return nil
}
func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, action *domain.Action, release domain.Release) ([]string, error) {
//downloadClient := client.Client.(*deluge.ClientV2)
downloadClient := deluge.NewV2(deluge.Settings{
@ -248,9 +267,7 @@ func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, a
}
if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = labelPluginActive.SetTorrentLabel(ctx, torrentHash, action.Label)
if err != nil {
if err := delugeSetOrCreateTorrentLabel(ctx, labelPluginActive, client.Name, torrentHash, action.Label); err != nil {
return nil, errors.Wrap(err, "could not set label: %s on client: %s", action.Label, client.Name)
}
}
@ -295,9 +312,7 @@ func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, a
}
if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = labelPluginActive.SetTorrentLabel(ctx, torrentHash, action.Label)
if err != nil {
if err := delugeSetOrCreateTorrentLabel(ctx, labelPluginActive, client.Name, torrentHash, action.Label); err != nil {
return nil, errors.Wrap(err, "could not set label: %s on client: %s", action.Label, client.Name)
}
}
@ -310,7 +325,6 @@ func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, a
}
func (s *service) prepareDelugeOptions(action *domain.Action) (deluge.Options, error) {
// set options
options := deluge.Options{}