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

Function UploadTaskPlugin

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

Source from the content-addressed store, hash-verified

105}
106
107func UploadTaskPlugin(c *gin.Context) {
108 var request taskPluginUploadRequest
109 if err := c.ShouldBindJSON(&request); err != nil {
110 common.ApiErrorMsg(c, err.Error())
111 return
112 }
113 if len(request.Source) > maxTaskPluginSourceBytes {
114 common.ApiErrorMsg(c, "plugin source exceeds 8 MiB")
115 return
116 }
117 marketplaceKind := ""
118 if indexURL := strings.TrimSpace(request.MarketplaceIndexURL); indexURL != "" {
119 if !slices.ContainsFunc(setting.GetTaskPluginMarketplaceSources(), func(source setting.TaskPluginMarketplaceSource) bool {
120 return strings.TrimSpace(source.IndexURL) == indexURL
121 }) {
122 common.ApiErrorT(c, "The plugin's marketplace source is not in the source list")
123 return
124 }
125 marketplaceKind = setting.TaskPluginMarketplaceSourceKind(indexURL)
126 }
127 if expected := strings.TrimSpace(request.SourceSha256); expected != "" {
128 actual := fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source)))
129 if !strings.EqualFold(actual, expected) {
130 common.ApiErrorMsg(c, "plugin source sha256 mismatch")
131 return
132 }
133 }
134 temporary := jsplugin.NewRegistry()
135 loaded, err := temporary.Register(request.Source, jsplugin.Options{})
136 if err != nil {
137 taskPluginCompileError(c, err)
138 return
139 }
140 if err = jsplugin.ValidateV1Meta(loaded.Meta); err != nil {
141 common.ApiErrorMsg(c, err.Error())
142 return
143 }
144 // A plugin from a community or third-party source must not take the key
145 // of a built-in plugin, and its key carries its author's name.
146 if marketplaceKind != "" && marketplaceKind != setting.TaskPluginMarketplaceKindOfficial {
147 if taskPluginHasFactory(loaded.Meta.Key) {
148 common.ApiErrorT(c, "A community plugin cannot use the key of the built-in plugin {{key}}", map[string]any{"key": loaded.Meta.Key})
149 return
150 }
151 if !strings.Contains(loaded.Meta.Key, "_") {
152 common.ApiErrorT(c, "A community plugin's key must start with its author's GitHub name and an underscore, like alice_foo")
153 return
154 }
155 }
156 icon := strings.TrimSpace(request.Icon)
157 if icon != "" {
158 if _, _, err = jsplugin.DecodeIconDataURI(icon); err != nil {
159 common.ApiErrorMsg(c, err.Error())
160 return
161 }
162 }
163 enabled := true
164 if request.Enabled != nil {

Calls 15

RegisterMethod · 0.95
HasIconMethod · 0.95
ApiErrorMsgFunction · 0.92
ApiErrorTFunction · 0.92
NewRegistryFunction · 0.92
PreflightRoutingConflictFunction · 0.92
GetTaskPluginVersionFunction · 0.92
ApiErrorFunction · 0.92
LongTextTypeAlias · 0.92