43 lines
850 B
Go
43 lines
850 B
Go
package meowlib
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (msg *UserMessage) AddFile(filename string, maxMessageSize int64) error {
|
|
fi, err := os.Stat(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if fi.Size() > maxMessageSize {
|
|
return errors.New("cannot add file, file bigger than message size")
|
|
}
|
|
var totalsize int64
|
|
totalsize = 0
|
|
for _, f := range msg.Files {
|
|
totalsize += int64(f.Size)
|
|
}
|
|
totalsize += fi.Size()
|
|
if totalsize > maxMessageSize {
|
|
return errors.New("cannot add file, too many files, max message size exceeded")
|
|
}
|
|
data, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var file File
|
|
file.Filename = filename
|
|
file.Size = uint64(fi.Size())
|
|
file.Data = data
|
|
msg.Files = append(msg.Files, &file)
|
|
|
|
msg.Status = &ConversationStatus{}
|
|
msg.Status.Uuid = uuid.New().String()
|
|
|
|
return nil
|
|
}
|