diff --git a/internal/action/deluge.go b/internal/action/deluge.go index 85ba50a..a58ec69 100644 --- a/internal/action/deluge.go +++ b/internal/action/deluge.go @@ -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{}