2022-11-27 21:08:34 +01:00
|
|
|
package meowlib
|
|
|
|
|
2022-12-18 21:52:03 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2022-11-27 21:08:34 +01:00
|
|
|
|
2022-12-18 21:52:03 +01:00
|
|
|
"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 {
|
2023-11-14 16:32:50 +01:00
|
|
|
return errors.New("cannot add file, file bigger than message size")
|
2022-12-18 21:52:03 +01:00
|
|
|
}
|
|
|
|
var totalsize int64
|
|
|
|
totalsize = 0
|
|
|
|
for _, f := range msg.Files {
|
|
|
|
totalsize += int64(f.Size)
|
|
|
|
}
|
|
|
|
totalsize += fi.Size()
|
|
|
|
if totalsize > maxMessageSize {
|
2022-12-19 12:15:10 +01:00
|
|
|
return errors.New("cannot add file, too many files, max message size exceeded")
|
2022-12-18 21:52:03 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2023-11-06 22:44:21 +01:00
|
|
|
msg.Status = &ConversationStatus{}
|
2022-12-18 21:52:03 +01:00
|
|
|
msg.Status.LocalUuid = uuid.New().String()
|
|
|
|
|
|
|
|
return nil
|
2022-11-27 21:08:34 +01:00
|
|
|
}
|