| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package matrix |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "image" |
| 23 | "os" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/gabriel-vasile/mimetype" |
| 28 | "gopkg.in/vansante/go-ffprobe.v2" |
| 29 | |
| 30 | "maunium.net/go/mautrix/event" |
| 31 | |
| 32 | "maunium.net/go/gomuks/debug" |
| 33 | ) |
| 34 | |
| 35 | func getImageInfo(path string) (event.FileInfo, error) { |
| 36 | var info event.FileInfo |
| 37 | file, err := os.Open(path) |
| 38 | if err != nil { |
| 39 | return info, fmt.Errorf("failed to open image to get info: %w", err) |
| 40 | } |
| 41 | cfg, _, err := image.DecodeConfig(file) |
| 42 | if err != nil { |
| 43 | return info, fmt.Errorf("failed to get image info: %w", err) |
| 44 | } |
| 45 | info.Width = cfg.Width |
| 46 | info.Height = cfg.Height |
| 47 | return info, nil |
| 48 | } |
| 49 | |
| 50 | func getFFProbeInfo(mimeClass, path string) (msgtype event.MessageType, info event.FileInfo, err error) { |
| 51 | ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second) |
| 52 | defer cancelFn() |
| 53 | var probedInfo *ffprobe.ProbeData |
| 54 | probedInfo, err = ffprobe.ProbeURL(ctx, path) |
| 55 | if err != nil { |
| 56 | err = fmt.Errorf("failed to get %s info with ffprobe: %w", mimeClass, err) |
| 57 | return |
| 58 | } |
| 59 | if mimeClass == "audio" { |
| 60 | msgtype = event.MsgAudio |
| 61 | stream := probedInfo.FirstAudioStream() |
| 62 | if stream != nil { |
| 63 | info.Duration = int(stream.DurationTs) |
| 64 | } |
| 65 | } else { |
| 66 | msgtype = event.MsgVideo |
| 67 | stream := probedInfo.FirstVideoStream() |
| 68 | if stream != nil { |
| 69 | info.Duration = int(stream.DurationTs) |
| 70 | info.Width = stream.Width |
| 71 | info.Height = stream.Height |
| 72 | } |
| 73 | } |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | func getMediaInfo(path string) (msgtype event.MessageType, info event.FileInfo, err error) { |
| 78 | var mime *mimetype.MIME |
| 79 | mime, err = mimetype.DetectFile(path) |
| 80 | if err != nil { |
| 81 | err = fmt.Errorf("failed to get content type: %w", err) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | mimeClass := strings.SplitN(mime.String(), "/", 2)[0] |
| 86 | switch mimeClass { |
| 87 | case "image": |
| 88 | msgtype = event.MsgImage |
| 89 | info, err = getImageInfo(path) |
| 90 | if err != nil { |
| 91 | debug.Printf("Failed to get image info for %s: %v", err) |
| 92 | err = nil |
| 93 | } |
| 94 | case "audio", "video": |
| 95 | msgtype, info, err = getFFProbeInfo(mimeClass, path) |
| 96 | if err != nil { |
| 97 | debug.Printf("Failed to get ffprobe info for %s: %v", err) |
| 98 | err = nil |
| 99 | } |
| 100 | default: |
| 101 | msgtype = event.MsgFile |
| 102 | } |
| 103 | info.MimeType = mime.String() |
| 104 | |
| 105 | return |
| 106 | } |