2020-05-13 13:16:17 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func jpeg(buf []byte) bool {
|
|
|
|
return len(buf) > 2 &&
|
|
|
|
buf[0] == 0xFF &&
|
|
|
|
buf[1] == 0xD8 &&
|
|
|
|
buf[2] == 0xFF
|
|
|
|
}
|
|
|
|
|
|
|
|
func png(buf []byte) bool {
|
|
|
|
return len(buf) > 3 &&
|
|
|
|
buf[0] == 0x89 && buf[1] == 0x50 &&
|
|
|
|
buf[2] == 0x4E && buf[3] == 0x47
|
|
|
|
}
|
|
|
|
|
|
|
|
func gif(buf []byte) bool {
|
|
|
|
return len(buf) > 2 &&
|
|
|
|
buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46
|
|
|
|
}
|
|
|
|
|
|
|
|
func webp(buf []byte) bool {
|
|
|
|
return len(buf) > 11 &&
|
|
|
|
buf[8] == 0x57 && buf[9] == 0x45 &&
|
|
|
|
buf[10] == 0x42 && buf[11] == 0x50
|
|
|
|
}
|
|
|
|
|
|
|
|
func ImageType(buf []byte) protobuf.ImageMessage_ImageType {
|
2020-05-14 05:40:40 +00:00
|
|
|
switch {
|
|
|
|
case jpeg(buf):
|
2020-05-13 13:16:17 +00:00
|
|
|
return protobuf.ImageMessage_JPEG
|
2020-05-14 05:40:40 +00:00
|
|
|
case png(buf):
|
2020-05-13 13:16:17 +00:00
|
|
|
return protobuf.ImageMessage_PNG
|
2020-05-14 05:40:40 +00:00
|
|
|
case gif(buf):
|
2020-05-13 13:16:17 +00:00
|
|
|
return protobuf.ImageMessage_GIF
|
2020-05-14 05:40:40 +00:00
|
|
|
case webp(buf):
|
2020-05-13 13:16:17 +00:00
|
|
|
return protobuf.ImageMessage_WEBP
|
2020-05-14 05:40:40 +00:00
|
|
|
default:
|
|
|
|
return protobuf.ImageMessage_UNKNOWN_IMAGE_TYPE
|
2020-05-13 13:16:17 +00:00
|
|
|
}
|
|
|
|
}
|