Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 9e30b1c

Browse files
committed
changed return type for StoreFile
1 parent 8f49c82 commit 9e30b1c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

storage.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,37 @@ import (
88
"net/url"
99
)
1010

11+
// StoreFileResult incluses the file id and url. The ID is required
12+
// when deleting file
13+
type StoreFileResult struct {
14+
ID string `json:"id"`
15+
URL string `json:"url"`
16+
}
17+
1118
// StoreFile uploads a new file and returns its public URL using SB CDN.
12-
func StoreFile(token, filename string, file io.ReadSeeker) (string, error) {
19+
func StoreFile(token, filename string, file io.ReadSeeker) (StoreFileResult, error) {
20+
var res StoreFileResult
21+
1322
// multipart form data
1423
var buf bytes.Buffer
1524
w := multipart.NewWriter(&buf)
1625

1726
fw, err := w.CreateFormFile("file", filename)
1827
if err != nil {
19-
return "", fmt.Errorf("error creating form field: %v", err)
28+
return res, fmt.Errorf("error creating form field: %v", err)
2029
}
2130

2231
if _, err := io.Copy(fw, file); err != nil {
23-
return "", fmt.Errorf("error copying file data to form field: %v", err)
32+
return res, fmt.Errorf("error copying file data to form field: %v", err)
2433
}
2534

2635
w.Close()
2736

28-
var fileURL string
29-
if err := request(token, "POST", "/storage/upload", w.FormDataContentType(), &buf, &fileURL); err != nil {
30-
return "", fmt.Errorf("error while uploading file: %v", err)
37+
if err := request(token, "POST", "/storage/upload", w.FormDataContentType(), &buf, &res); err != nil {
38+
return res, fmt.Errorf("error while uploading file: %v", err)
3139
}
3240

33-
return fileURL, nil
41+
return res, nil
3442
}
3543

3644
// DownloadFile retrieves the file content as []byte

0 commit comments

Comments
 (0)