-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align auto-convert feature between web gui and cli
We have been auto-converting images for the web gui, while prompting the user from the CLI composer. Given that the CLI prompt is causing issues with the new "non-interactive" cli composer, I believe it's reasonable to avoid this complexity by doing auto-convertion on the command line aswell. We could consider adding a config options to let the user control this behavior in the future. But until then, let's keep it simple. Resolves #431
- Loading branch information
1 parent
41b2d07
commit 04ef16e
Showing
4 changed files
with
85 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
"image/jpeg" | ||
"io" | ||
"log" | ||
"mime" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/la5nta/wl2k-go/fbb" | ||
"github.com/nfnt/resize" | ||
) | ||
|
||
func addAttachment(msg *fbb.Message, filename string, contentType string, r io.Reader) error { | ||
p, err := io.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
if ok, mediaType := isConvertableImageMediaType(filename, contentType); ok { | ||
log.Printf("Auto converting '%s' [%s]...", filename, mediaType) | ||
if converted, err := convertImage(p); err != nil { | ||
log.Printf("Error converting image: %s", err) | ||
} else { | ||
log.Printf("Done converting '%s'.", filename) | ||
ext := filepath.Ext(filename) | ||
filename = filename[:len(filename)-len(ext)] + ".jpg" | ||
p = converted | ||
} | ||
} | ||
msg.AddFile(fbb.NewFile(filename, p)) | ||
return nil | ||
} | ||
|
||
func isConvertableImageMediaType(filename, contentType string) (convertable bool, mediaType string) { | ||
if contentType != "" { | ||
mediaType, _, _ = mime.ParseMediaType(contentType) | ||
} | ||
if mediaType == "" { | ||
mediaType = mime.TypeByExtension(path.Ext(filename)) | ||
} | ||
|
||
switch mediaType { | ||
case "image/svg+xml": | ||
// This is a text file | ||
return false, mediaType | ||
default: | ||
return strings.HasPrefix(mediaType, "image/"), mediaType | ||
} | ||
} | ||
|
||
func convertImage(orig []byte) ([]byte, error) { | ||
img, _, err := image.Decode(bytes.NewReader(orig)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Scale down | ||
if img.Bounds().Dx() > 600 { | ||
img = resize.Resize(600, 0, img, resize.NearestNeighbor) | ||
} | ||
|
||
// Re-encode as low quality jpeg | ||
var buf bytes.Buffer | ||
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 40}); err != nil { | ||
return orig, err | ||
} | ||
if buf.Len() >= len(orig) { | ||
return orig, nil | ||
} | ||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters