1
1
package llm
2
2
3
3
import (
4
+ "crypto/md5"
4
5
"encoding/base64"
5
6
"encoding/json"
7
+ "fmt"
6
8
"io"
7
9
"mime"
8
10
"net/http"
@@ -88,14 +90,16 @@ func (a *Attachment) MarshalJSON() ([]byte, error) {
88
90
Filename string `json:"filename,omitempty"`
89
91
Type string `json:"type"`
90
92
Bytes uint64 `json:"bytes"`
93
+ Hash string `json:"hash,omitempty"`
91
94
Caption string `json:"caption,omitempty"`
92
95
}
93
96
94
97
j .Type = a .Type ()
95
98
j .Caption = a .Caption ()
99
+ j .Hash = a .Hash ()
100
+ j .Filename = a .Filename ()
96
101
if a .meta != nil {
97
102
j .Id = a .meta .Id
98
- j .Filename = a .meta .Filename
99
103
j .Bytes = uint64 (len (a .meta .Data ))
100
104
} else if a .image != nil {
101
105
j .Bytes = uint64 (len (a .image .Data ))
@@ -116,6 +120,13 @@ func (a *Attachment) String() string {
116
120
////////////////////////////////////////////////////////////////////////////////
117
121
// PUBLIC METHODS
118
122
123
+ // Compute and print the MD5 hash
124
+ func (a * Attachment ) Hash () string {
125
+ hash := md5 .New ()
126
+ hash .Write (a .Data ())
127
+ return fmt .Sprintf ("%x" , hash .Sum (nil ))
128
+ }
129
+
119
130
// Write out attachment
120
131
func (a * Attachment ) Write (w io.Writer ) (int , error ) {
121
132
if a .meta != nil {
@@ -129,11 +140,14 @@ func (a *Attachment) Write(w io.Writer) (int, error) {
129
140
130
141
// Return the filename of an attachment
131
142
func (a * Attachment ) Filename () string {
132
- if a .meta != nil {
143
+ if a .meta != nil && a . meta . Filename != "" {
133
144
return a .meta .Filename
134
- } else {
135
- return ""
136
145
}
146
+ // Obtain filename from MD5
147
+ if ext , err := mime .ExtensionsByType (a .Type ()); err == nil && len (ext ) > 0 {
148
+ return a .Hash () + ext [0 ]
149
+ }
150
+ return ""
137
151
}
138
152
139
153
// Return the raw attachment data
0 commit comments