|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package actions |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | + |
| 25 | + "github.com/elastic/beats/v7/libbeat/beat" |
| 26 | + "github.com/elastic/beats/v7/libbeat/common" |
| 27 | + "github.com/elastic/beats/v7/libbeat/mime" |
| 28 | + "github.com/elastic/beats/v7/libbeat/processors" |
| 29 | + "github.com/elastic/beats/v7/libbeat/processors/checks" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + processors.RegisterPlugin("detect_mime_type", |
| 34 | + checks.ConfigChecked(NewDetectMimeType, |
| 35 | + checks.RequireFields("field", "target"), |
| 36 | + checks.AllowedFields("field", "target"))) |
| 37 | +} |
| 38 | + |
| 39 | +type mimeTypeProcessor struct { |
| 40 | + Field string `config:"field"` |
| 41 | + Target string `config:"target"` |
| 42 | +} |
| 43 | + |
| 44 | +// NewDetectMimeType constructs a new mime processor. |
| 45 | +func NewDetectMimeType(cfg *common.Config) (processors.Processor, error) { |
| 46 | + mimeType := &mimeTypeProcessor{} |
| 47 | + if err := cfg.Unpack(mimeType); err != nil { |
| 48 | + return nil, errors.Wrapf(err, "fail to unpack the detect_mime_type configuration") |
| 49 | + } |
| 50 | + |
| 51 | + return mimeType, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (m *mimeTypeProcessor) Run(event *beat.Event) (*beat.Event, error) { |
| 55 | + valI, err := event.GetValue(m.Field) |
| 56 | + if err != nil { |
| 57 | + // doesn't have the required fieldd value to analyze |
| 58 | + return event, nil |
| 59 | + } |
| 60 | + val, _ := valI.(string) |
| 61 | + if val == "" { |
| 62 | + // wrong type or not set |
| 63 | + return event, nil |
| 64 | + } |
| 65 | + if mimeType := mime.Detect(val); mimeType != "" { |
| 66 | + event.Fields.DeepUpdate(common.MapStr{ |
| 67 | + m.Target: mimeType, |
| 68 | + }) |
| 69 | + } |
| 70 | + return event, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (m *mimeTypeProcessor) String() string { |
| 74 | + return fmt.Sprintf("detect_mime_type=%+v->%+v", m.Field, m.Target) |
| 75 | +} |
0 commit comments