-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.go
More file actions
112 lines (106 loc) · 2.61 KB
/
image.go
File metadata and controls
112 lines (106 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2021 - 2025 PurpleSec Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package forwarder
import (
"context"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"image"
"image/jpeg"
"image/png"
"io"
"net/http"
"os"
"strings"
"github.com/corona10/goimagehash"
telegram "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var errNotImage = errors.New("not an image")
type reader struct {
h hash.Hash
r io.ReadCloser
}
type fileData struct {
Sum string
FileID string
Average uint64
}
func fnv(n string) uint32 {
h := uint32(2166136261)
for i := range n {
h *= 16777619
h ^= uint32(n[i])
}
return h
}
func (f fileData) String() string {
return fmt.Sprintf("Image(%x/%x/%x)", fnv(f.FileID), fnv(f.Sum), f.Average)
}
func (fileData) NeedsUpload() bool {
return false
}
func (f fileData) SendData() string {
return f.FileID
}
func (r *reader) Read(b []byte) (int, error) {
n, err := r.r.Read(b)
r.h.Write(b[0:n])
return n, err
}
func (fileData) UploadData() (string, io.Reader, error) {
return "", nil, os.ErrInvalid
}
func loadImage(x context.Context, bot *telegram.BotAPI, id string, mime string) (fileData, error) {
if len(mime) > 0 && !strings.HasPrefix(mime, "image/") {
return fileData{FileID: id}, errNotImage
}
f, err := bot.GetFile(telegram.FileConfig{FileID: id})
if err != nil {
return fileData{}, err
}
var (
z, _ = http.NewRequestWithContext(x, "GET", fmt.Sprintf(telegram.FileEndpoint, bot.Token, f.FilePath), nil)
d *http.Response
)
if d, err = bot.Client.Do(z); err != nil {
return fileData{}, err
}
var (
r = reader{h: sha512.New(), r: d.Body}
i image.Image
)
switch mime {
case "image/png":
i, err = png.Decode(&r)
default:
i, err = jpeg.Decode(&r)
}
if d.Body.Close(); err != nil {
return fileData{FileID: id}, errNotImage
}
h, err := goimagehash.PerceptionHash(i)
if err != nil {
return fileData{}, err
}
return fileData{
Sum: hex.EncodeToString(r.h.Sum(nil)),
FileID: id,
Average: h.GetHash(),
}, nil
}