-
Notifications
You must be signed in to change notification settings - Fork 2
Add Matrix metadata for LINE images #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Adri11334
wants to merge
10
commits into
beeper:main
Choose a base branch
from
Adri11334:line-image-fileinfo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29c67c7
fix: add Matrix metadata for LINE images
Adri11334 4d06d2b
fix: tighten LINE image metadata detection
Adri11334 5950144
fix: refine LINE image metadata detection
Adri11334 7677aaf
fix: make SVG image extension deterministic
Adri11334 4601bb5
fix: scan LINE image ftyp compatible brands
Adri11334 1e8a279
fix: harden LINE image ftyp parsing
Adri11334 3d2edb9
fix: harden LINE image brand detection
Adri11334 eb5f321
fix: avoid LINE image brand allocation
Adri11334 3631053
fix: retry original LINE media processing
Adri11334 a4d8c11
Merge pull request #1 from beeper/main
Adri11334 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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,159 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "encoding/json" | ||
| "image" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "maunium.net/go/mautrix/event" | ||
| "maunium.net/go/mautrix/id" | ||
|
|
||
| "github.com/highesttt/matrix-line-messenger/pkg/line" | ||
| ) | ||
|
|
||
| func lineImageEventContent(mxc id.ContentURIString, file *event.EncryptedFileInfo, fileName string, info *event.FileInfo, relatesTo *event.RelatesTo) *event.MessageEventContent { | ||
| return &event.MessageEventContent{ | ||
| MsgType: event.MsgImage, | ||
| Body: fileName, | ||
| URL: mxc, | ||
| File: file, | ||
| Info: info, | ||
| RelatesTo: relatesTo, | ||
| } | ||
| } | ||
|
|
||
| type lineImageMedia struct { | ||
| fileName string | ||
| mimeType string | ||
| info *event.FileInfo | ||
| usedMimeFallback bool | ||
| decodeErr error | ||
| } | ||
|
|
||
| func lineImageMediaInfo(data []byte) lineImageMedia { | ||
| mimeType, usedFallback := detectLineImageMimeType(data) | ||
| info := &event.FileInfo{ | ||
| MimeType: mimeType, | ||
| Size: len(data), | ||
| } | ||
|
|
||
| var decodeErr error | ||
| if config, _, err := image.DecodeConfig(bytes.NewReader(data)); err == nil { | ||
| info.Width = config.Width | ||
| info.Height = config.Height | ||
| } else if !usedFallback && shouldLogLineImageDecodeError(mimeType) { | ||
| decodeErr = err | ||
| } | ||
|
|
||
| return lineImageMedia{ | ||
| fileName: "image." + imageExtensionForMIME(mimeType), | ||
| mimeType: mimeType, | ||
| info: info, | ||
| usedMimeFallback: usedFallback, | ||
| decodeErr: decodeErr, | ||
| } | ||
| } | ||
|
|
||
| func detectLineImageMimeType(data []byte) (string, bool) { | ||
| mimeType := normalizedImageMIMEType(http.DetectContentType(data)) | ||
| if strings.HasPrefix(mimeType, "image/") { | ||
| return mimeType, false | ||
| } | ||
|
|
||
| if len(data) >= 12 && lineImageBrandAt(data, 4) == [4]byte{'f', 't', 'y', 'p'} { | ||
| boxSize := binary.BigEndian.Uint32(data[:4]) | ||
| if boxSize >= 16 && uint64(boxSize) <= uint64(len(data)) { | ||
| detected := "" | ||
| for offset, end := 8, int(boxSize); offset+4 <= end; { | ||
| switch lineImageBrandAt(data, offset) { | ||
| case [4]byte{'a', 'v', 'i', 'f'}, [4]byte{'a', 'v', 'i', 's'}: | ||
| return "image/avif", false | ||
| case [4]byte{'h', 'e', 'i', 'c'}, [4]byte{'h', 'e', 'i', 'x'}, [4]byte{'h', 'e', 'v', 'c'}, [4]byte{'h', 'e', 'v', 'x'}: | ||
| return "image/heic", false | ||
| case [4]byte{'h', 'e', 'i', 'f'}, [4]byte{'h', 'e', 'i', 'm'}, [4]byte{'h', 'e', 'i', 's'}, [4]byte{'m', 'i', 'f', '1'}, [4]byte{'m', 's', 'f', '1'}: | ||
| detected = "image/heif" | ||
| } | ||
| if offset == 8 { | ||
| offset = 16 | ||
| } else { | ||
| offset += 4 | ||
| } | ||
| } | ||
| if detected != "" { | ||
| return detected, false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return "image/jpeg", true | ||
| } | ||
|
|
||
| func lineImageBrandAt(data []byte, offset int) [4]byte { | ||
| return [4]byte{data[offset], data[offset+1], data[offset+2], data[offset+3]} | ||
| } | ||
|
|
||
| func imageExtensionForMIME(mimeType string) string { | ||
| switch normalizedMIMEType := normalizedImageMIMEType(mimeType); normalizedMIMEType { | ||
| case "image/jpeg": | ||
| return "jpg" | ||
| case "image/svg+xml": | ||
| return "svg" | ||
| case "image/x-icon", "image/vnd.microsoft.icon": | ||
| return "ico" | ||
| case "image/png", "image/gif", "image/webp", "image/heic", "image/heif", "image/avif", "image/bmp", "image/tiff": | ||
| return strings.TrimPrefix(normalizedMIMEType, "image/") | ||
| } | ||
| return "jpg" | ||
| } | ||
|
|
||
| func shouldLogLineImageDecodeError(mimeType string) bool { | ||
| switch normalizedImageMIMEType(mimeType) { | ||
| case "image/jpeg", "image/png", "image/gif", "image/webp": | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func normalizedImageMIMEType(mimeType string) string { | ||
| normalizedMIMEType, _, _ := strings.Cut(mimeType, ";") | ||
| return strings.ToLower(strings.TrimSpace(normalizedMIMEType)) | ||
| } | ||
|
|
||
| type lineMediaContentInfo struct { | ||
| Category string `json:"category"` | ||
| } | ||
|
|
||
| func parseLineMediaContentInfo(metadata map[string]string) (lineMediaContentInfo, bool) { | ||
| if metadata == nil || metadata["MEDIA_CONTENT_INFO"] == "" { | ||
| return lineMediaContentInfo{}, false | ||
| } | ||
|
|
||
| var info lineMediaContentInfo | ||
| if err := json.Unmarshal([]byte(metadata["MEDIA_CONTENT_INFO"]), &info); err != nil { | ||
| return lineMediaContentInfo{}, false | ||
| } | ||
|
|
||
| return info, true | ||
| } | ||
|
|
||
| func lineMediaCategory(metadata map[string]string) string { | ||
| info, ok := parseLineMediaContentInfo(metadata) | ||
| if !ok { | ||
| return "" | ||
| } | ||
| return info.Category | ||
| } | ||
|
|
||
| func lineOBSDownloadOptions(metadata map[string]string, isPlainMedia bool) line.OBSDownloadOptions { | ||
| opts := line.OBSDownloadOptions{ | ||
| OBSPop: metadata["OBS_POP"], | ||
| } | ||
| if isPlainMedia && lineMediaCategory(metadata) == "original" { | ||
| opts.TID = "original" | ||
| opts.MaxProcessingRetries = line.OBSOriginalMediaMaxRetries | ||
| } | ||
| return opts | ||
| } |
This file contains hidden or 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,30 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/highesttt/matrix-line-messenger/pkg/line" | ||
| ) | ||
|
|
||
| func TestLineOBSDownloadOptionsUsesLongRetryWindowForOriginalMedia(t *testing.T) { | ||
| metadata := map[string]string{ | ||
| "MEDIA_CONTENT_INFO": `{"category":"original","extension":"jpeg"}`, | ||
| "OBS_POP": "pop-token", | ||
| } | ||
|
|
||
| plainOptions := lineOBSDownloadOptions(metadata, true) | ||
| if plainOptions.TID != "original" { | ||
| t.Fatalf("unexpected TID for original plain media: got %q", plainOptions.TID) | ||
| } | ||
| if plainOptions.OBSPop != "pop-token" { | ||
| t.Fatalf("unexpected OBS_POP: got %q", plainOptions.OBSPop) | ||
| } | ||
| if plainOptions.MaxProcessingRetries != line.OBSOriginalMediaMaxRetries { | ||
| t.Fatalf("unexpected retry limit: got %d, want %d", plainOptions.MaxProcessingRetries, line.OBSOriginalMediaMaxRetries) | ||
| } | ||
|
|
||
| encryptedOptions := lineOBSDownloadOptions(metadata, false) | ||
| if encryptedOptions.MaxProcessingRetries != 0 { | ||
| t.Fatalf("unexpected retry override for encrypted media: got %d", encryptedOptions.MaxProcessingRetries) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.