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