Make golangci-lint happy
This commit is contained in:
parent
8bad84e5d6
commit
75b9cb7364
@ -18,16 +18,31 @@ type MessagePart struct {
|
|||||||
Image []byte
|
Image []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bmumble) decodeImage(uri string, parts *[]MessagePart) error {
|
||||||
|
// Decode the data:image/... URI
|
||||||
|
image, err := dataurl.DecodeString(uri)
|
||||||
|
if err != nil {
|
||||||
|
b.Log.WithError(err).Info("No image extracted")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Determine the file extensions for that image
|
||||||
|
ext, err := mime.ExtensionsByType(image.MediaType.ContentType())
|
||||||
|
if err != nil || len(ext) == 0 {
|
||||||
|
b.Log.WithError(err).Infof("No file extension registered for MIME type '%s'", image.MediaType.ContentType())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Add the image to the MessagePart slice
|
||||||
|
*parts = append(*parts, MessagePart{"", ext[0], image.Data})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bmumble) tokenize(t *string) ([]MessagePart, error) {
|
func (b *Bmumble) tokenize(t *string) ([]MessagePart, error) {
|
||||||
// `^(.*?)` matches everyting before the image
|
// `^(.*?)` matches everything before the image
|
||||||
// `!\[[^\]]*\]\(` matches the `]+)` matches the data: URI used by Mumble
|
// `(data:image\/[^)]+)` matches the data: URI used by Mumble
|
||||||
// `\)` matches the closing parenthesis after the URI
|
// `\)` matches the closing parenthesis after the URI
|
||||||
// `(.*)$` matches the remaining text to be examined in the next iteration
|
// `(.*)$` matches the remaining text to be examined in the next iteration
|
||||||
p, err := regexp.Compile(`^(.*?)!\[[^\]]*\]\((data:image\/[^)]+)\)(.*)$`)
|
p := regexp.MustCompile(`^(.*?)!\[[^\]]*\]\((data:image\/[^)]+)\)(.*)$`)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
remaining := *t
|
remaining := *t
|
||||||
var parts []MessagePart
|
var parts []MessagePart
|
||||||
for {
|
for {
|
||||||
@ -41,26 +56,23 @@ func (b *Bmumble) tokenize(t *string) ([]MessagePart, error) {
|
|||||||
}
|
}
|
||||||
return parts, nil
|
return parts, nil
|
||||||
}
|
}
|
||||||
|
// tokens[1] is the text before the image
|
||||||
if len(tokens[1]) > 0 {
|
if len(tokens[1]) > 0 {
|
||||||
parts = append(parts, MessagePart{tokens[1], "", nil})
|
parts = append(parts, MessagePart{tokens[1], "", nil})
|
||||||
}
|
}
|
||||||
|
// tokens[2] is the image URL
|
||||||
uri, err := dataurl.UnescapeToString(strings.ReplaceAll(tokens[2], " ", ""))
|
uri, err := dataurl.UnescapeToString(strings.ReplaceAll(tokens[2], " ", ""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.WithError(err).Info("URL unescaping failed")
|
b.Log.WithError(err).Info("URL unescaping failed")
|
||||||
} else {
|
remaining = tokens[3]
|
||||||
|
continue
|
||||||
|
}
|
||||||
b.Log.Debugf("Raw data: URL: %s", uri)
|
b.Log.Debugf("Raw data: URL: %s", uri)
|
||||||
image, err := dataurl.DecodeString(uri)
|
err = b.decodeImage(uri, &parts)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
ext, err := mime.ExtensionsByType(image.MediaType.ContentType())
|
b.Log.WithError(err).Info("Decoding the image failed")
|
||||||
if ext != nil && len(ext) > 0 {
|
|
||||||
parts = append(parts, MessagePart{"", ext[0], image.Data})
|
|
||||||
} else {
|
|
||||||
b.Log.WithError(err).Infof("No file extension registered for MIME type '%s'", image.MediaType.ContentType())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
b.Log.WithError(err).Info("No image extracted")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// tokens[3] is the text after the image, processed in the next iteration
|
||||||
remaining = tokens[3]
|
remaining = tokens[3]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user