-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfileupload.go
More file actions
54 lines (42 loc) · 1.25 KB
/
fileupload.go
File metadata and controls
54 lines (42 loc) · 1.25 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
package utils
import (
"os"
"path/filepath"
stream "github.com/GetStream/stream-chat-go/v5"
"github.com/spf13/cobra"
)
type uploadType string
const (
uploadTypeFile uploadType = "file"
uploadTypeImage uploadType = "image"
)
func UploadFile(c *stream.Client, cmd *cobra.Command, chType, chID, userID, filePath string) (string, error) {
return uploadFile(c, cmd, uploadTypeFile, chType, chID, userID, filePath)
}
func UploadImage(c *stream.Client, cmd *cobra.Command, chType, chID, userID, filePath string) (string, error) {
return uploadFile(c, cmd, uploadTypeImage, chType, chID, userID, filePath)
}
func uploadFile(c *stream.Client, cmd *cobra.Command, uploadtype uploadType, chType, chID, userID, filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer func() {
_ = file.Close()
}()
req := stream.SendFileRequest{
User: &stream.User{ID: userID},
FileName: filepath.Base(file.Name()),
Reader: file,
}
var resp *stream.SendFileResponse
if uploadtype == uploadTypeImage {
resp, err = c.Channel(chType, chID).SendImage(cmd.Context(), req)
} else {
resp, err = c.Channel(chType, chID).SendFile(cmd.Context(), req)
}
if err != nil {
return "", err
}
return resp.File, nil
}