From bf901a7aa385f7928078a514c7f2c2813021282c Mon Sep 17 00:00:00 2001 From: ycc Date: Sun, 18 Dec 2022 21:52:03 +0100 Subject: [PATCH] sendfile simple small files usermessage implementation --- client/config.go | 2 +- message.go | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/client/config.go b/client/config.go index c8916b1..15924aa 100644 --- a/client/config.go +++ b/client/config.go @@ -14,7 +14,7 @@ type Config struct { StoragePath string `json:"storage_path,omitempty"` MaxIdsPerUser int `json:"max_ids_per_user,omitempty"` MsgDbRollingPeriod int `json:"msg_db_rolling_period,omitempty"` - Chunksize int `json:"chunksize,omitempty"` + Chunksize int64 `json:"chunksize,omitempty"` ServerPollInterval int `json:"server_poll_interval,omitempty"` // GUI LastOpenChat string `json:"last_open_chat,omitempty"` diff --git a/message.go b/message.go index 69cd2a6..ea227af 100644 --- a/message.go +++ b/message.go @@ -1,5 +1,42 @@ package meowlib -func (*UserMessage) addFile(filename string) { +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 messge 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 = &UserMessage_ConversationStatus{} + msg.Status.LocalUuid = uuid.New().String() + + return nil }