MCPcopy Create free account
hub / github.com/QuantumNous/new-api / UploadTaskPlugin

Function UploadTaskPlugin

controller/task_plugin.go:40–98  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

38}
39
40func UploadTaskPlugin(c *gin.Context) {
41 var request taskPluginUploadRequest
42 if err := c.ShouldBindJSON(&request); err != nil {
43 common.ApiErrorMsg(c, err.Error())
44 return
45 }
46 if len(request.Source) > maxTaskPluginSourceBytes {
47 common.ApiErrorMsg(c, "plugin source exceeds 1 MiB")
48 return
49 }
50 if expected := strings.TrimSpace(request.SourceSha256); expected != "" {
51 actual := fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source)))
52 if !strings.EqualFold(actual, expected) {
53 common.ApiErrorMsg(c, "plugin source sha256 mismatch")
54 return
55 }
56 }
57 temporary := jsplugin.NewRegistry()
58 loaded, err := temporary.Register(request.Source, jsplugin.Options{})
59 if err != nil {
60 common.ApiErrorMsg(c, err.Error())
61 return
62 }
63 if err = jsplugin.ValidateV1Meta(loaded.Meta); err != nil {
64 common.ApiErrorMsg(c, err.Error())
65 return
66 }
67 icon := strings.TrimSpace(request.Icon)
68 if icon != "" {
69 if _, _, err = jsplugin.DecodeIconDataURI(icon); err != nil {
70 common.ApiErrorMsg(c, err.Error())
71 return
72 }
73 }
74 enabled := true
75 if request.Enabled != nil {
76 enabled = *request.Enabled
77 }
78 if enabled && !request.Force {
79 if err = jsplugin.PreflightRoutingConflict(jsplugin.DefaultRegistry.Generation(), loaded); err != nil {
80 common.ApiErrorMsg(c, err.Error())
81 return
82 }
83 }
84 plugin := model.TaskPlugin{
85 Key: loaded.Meta.Key, APIVersion: loaded.Meta.APIVersion, Version: loaded.Meta.Version,
86 Source: request.Source, SourceHash: fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source))),
87 Icon: icon, Enabled: enabled, Remark: request.Remark,
88 }
89 if err = model.SaveTaskPlugin(&plugin); err != nil {
90 common.ApiError(c, err)
91 return
92 }
93 if err = syncTaskPluginsOnceContext(c.Request.Context()); err != nil {
94 common.ApiError(c, err)
95 return
96 }
97 common.ApiSuccess(c, taskPluginDetail{Plugin: &plugin, Meta: loaded.Meta, Source: plugin.Source, Layer: "override", HasIcon: plugin.HasIcon()})

Calls 13

RegisterMethod · 0.95
HasIconMethod · 0.95
ApiErrorMsgFunction · 0.92
NewRegistryFunction · 0.92
ValidateV1MetaFunction · 0.92
DecodeIconDataURIFunction · 0.92
PreflightRoutingConflictFunction · 0.92
SaveTaskPluginFunction · 0.92
ApiErrorFunction · 0.92
ApiSuccessFunction · 0.92
GenerationMethod · 0.80