2020-08-08 02:05:35 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-22 00:20:21 +00:00
|
|
|
"strconv"
|
2020-08-08 02:05:35 +00:00
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager"
|
2023-10-16 05:15:12 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
2022-04-25 05:55:05 +00:00
|
|
|
"github.com/stashapp/stash/pkg/plugin"
|
2023-11-01 21:58:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil"
|
2020-08-08 02:05:35 +00:00
|
|
|
)
|
|
|
|
|
2024-02-22 00:20:21 +00:00
|
|
|
func toPluginArgs(args []*plugin.PluginArgInput) plugin.OperationInput {
|
|
|
|
ret := make(plugin.OperationInput)
|
|
|
|
for _, a := range args {
|
|
|
|
ret[a.Key] = toPluginArgValue(a.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func toPluginArgValue(arg *plugin.PluginValueInput) interface{} {
|
|
|
|
if arg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case arg.Str != nil:
|
|
|
|
return *arg.Str
|
|
|
|
case arg.I != nil:
|
|
|
|
return *arg.I
|
|
|
|
case arg.B != nil:
|
|
|
|
return *arg.B
|
|
|
|
case arg.F != nil:
|
|
|
|
return *arg.F
|
|
|
|
case arg.O != nil:
|
|
|
|
return toPluginArgs(arg.O)
|
|
|
|
case arg.A != nil:
|
|
|
|
var ret []interface{}
|
|
|
|
for _, v := range arg.A {
|
|
|
|
ret = append(ret, toPluginArgValue(v))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) RunPluginTask(
|
|
|
|
ctx context.Context,
|
|
|
|
pluginID string,
|
|
|
|
taskName *string,
|
|
|
|
description *string,
|
|
|
|
args []*plugin.PluginArgInput,
|
|
|
|
argsMap map[string]interface{},
|
|
|
|
) (string, error) {
|
|
|
|
if argsMap == nil {
|
|
|
|
// convert args to map
|
|
|
|
// otherwise ignore args in favour of args map
|
|
|
|
argsMap = toPluginArgs(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := manager.GetInstance()
|
|
|
|
jobID := m.RunPluginTask(ctx, pluginID, taskName, description, argsMap)
|
|
|
|
return strconv.Itoa(jobID), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) RunPluginOperation(
|
|
|
|
ctx context.Context,
|
|
|
|
pluginID string,
|
|
|
|
args map[string]interface{},
|
|
|
|
) (interface{}, error) {
|
|
|
|
if args == nil {
|
|
|
|
args = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
m := manager.GetInstance()
|
2024-02-22 00:20:21 +00:00
|
|
|
return m.PluginCache.RunPlugin(ctx, pluginID, args)
|
2020-08-08 02:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *mutationResolver) ReloadPlugins(ctx context.Context) (bool, error) {
|
2023-11-28 02:56:46 +00:00
|
|
|
manager.GetInstance().RefreshPluginCache()
|
2020-08-08 02:05:35 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2023-10-16 05:15:12 +00:00
|
|
|
|
|
|
|
func (r *mutationResolver) SetPluginsEnabled(ctx context.Context, enabledMap map[string]bool) (bool, error) {
|
|
|
|
c := config.GetInstance()
|
|
|
|
|
|
|
|
existingDisabled := c.GetDisabledPlugins()
|
|
|
|
var newDisabled []string
|
|
|
|
|
|
|
|
// remove plugins that are no longer disabled
|
|
|
|
for _, disabledID := range existingDisabled {
|
|
|
|
if enabled, found := enabledMap[disabledID]; !enabled || !found {
|
|
|
|
newDisabled = append(newDisabled, disabledID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add plugins that are newly disabled
|
|
|
|
for pluginID, enabled := range enabledMap {
|
|
|
|
if !enabled {
|
2023-11-01 21:58:32 +00:00
|
|
|
newDisabled = sliceutil.AppendUnique(newDisabled, pluginID)
|
2023-10-16 05:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:56:18 +00:00
|
|
|
c.SetInterface(config.DisabledPlugins, newDisabled)
|
2023-10-16 05:15:12 +00:00
|
|
|
|
|
|
|
if err := c.Write(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|