Skip to content

Commit f2a9fc2

Browse files
author
Alexandre Nicolaie
committed
feat: add UploadContent
1 parent 90e6ddb commit f2a9fc2

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

netstorage.go

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,31 @@ func NewNetstorage(hostname, keyname, key string, ssl bool) *Netstorage {
4646
}
4747

4848
// Only for upload action. (Used by _request func)
49-
func _ifUploadAction(kwargs map[string]string) (*io.Reader, error) {
49+
func _ifUploadAction(kwargs map[string]interface{}) (io.Reader, error) {
5050
var data io.Reader
51-
if kwargs["action"] == "upload" {
52-
bArr, err := ioutil.ReadFile(kwargs["source"])
51+
if kwargs["action"].(string) == "upload" {
52+
bArr, err := ioutil.ReadFile(kwargs["source"].(string))
5353
if err != nil {
5454
return nil, err
5555
}
5656

5757
data = bytes.NewReader(bArr)
5858
}
59-
return &data, nil
59+
return data, nil
6060
}
6161

6262
// Reads http body from response, closes response.Body and
6363
// returns that string. (Used by _request func)
64-
func _getBody(kwargs map[string]string, response *http.Response) (string, error) {
64+
func _getBody(kwargs map[string]interface{}, response *http.Response) (string, error) {
6565
var body []byte
6666
var err error
67-
if kwargs["action"] == "download" && response.StatusCode == 200 {
68-
localDestination := kwargs["destination"]
67+
if kwargs["action"].(string) == "download" && response.StatusCode == 200 {
68+
localDestination := kwargs["destination"].(string)
6969

7070
if localDestination == "" {
71-
localDestination = path.Base(kwargs["path"])
71+
localDestination = path.Base(kwargs["path"].(string))
7272
} else if s, err := os.Stat(localDestination); err == nil && s.IsDir() {
73-
localDestination = path.Join(localDestination, path.Base(kwargs["path"]))
73+
localDestination = path.Join(localDestination, path.Base(kwargs["path"].(string)))
7474
}
7575

7676
out, err := os.Create(localDestination)
@@ -96,17 +96,17 @@ func _getBody(kwargs map[string]string, response *http.Response) (string, error)
9696
// Create the authorization headers with Netstorage struct values then
9797
// request to the Netstorage hostname, and return the response,
9898
// the body string and the error.
99-
func (ns *Netstorage) _request(kwargs map[string]string) (*http.Response, string, error) {
99+
func (ns *Netstorage) _request(kwargs map[string]interface{}) (*http.Response, string, error) {
100100
var err error
101101

102-
nsPath := kwargs["path"]
102+
nsPath := kwargs["path"].(string)
103103
if u, err := url.Parse(nsPath); strings.HasPrefix(nsPath, "/") && err == nil {
104104
nsPath = u.RequestURI()
105105
} else {
106106
return nil, "", fmt.Errorf("[Netstorage Error] Invalid netstorage path: %s", nsPath)
107107
}
108108

109-
acsAction := fmt.Sprintf("version=1&action=%s", kwargs["action"])
109+
acsAction := fmt.Sprintf("version=1&action=%s", kwargs["action"].(string))
110110
acsAuthData := fmt.Sprintf("5, 0.0.0.0, 0.0.0.0, %d, %d, %s",
111111
time.Now().Unix(),
112112
rand.Intn(100000),
@@ -117,13 +117,18 @@ func (ns *Netstorage) _request(kwargs map[string]string) (*http.Response, string
117117
mac.Write([]byte(acsAuthData + signString))
118118
acsAuthSign := base64.StdEncoding.EncodeToString(mac.Sum(nil))
119119

120-
data, err := _ifUploadAction(kwargs)
121-
if err != nil {
122-
return nil, "", err
120+
var data io.Reader
121+
if _, exists := kwargs["content"]; exists {
122+
data = kwargs["content"].(io.Reader)
123+
} else {
124+
data, err = _ifUploadAction(kwargs)
125+
if err != nil {
126+
return nil, "", err
127+
}
123128
}
124129

125-
request, err := http.NewRequest(kwargs["method"],
126-
fmt.Sprintf("http%s://%s%s", ns.Ssl, ns.Hostname, nsPath), *data)
130+
request, err := http.NewRequest(kwargs["method"].(string),
131+
fmt.Sprintf("http%s://%s%s", ns.Ssl, ns.Hostname, nsPath), data)
127132

128133
if err != nil {
129134
return nil, "", err
@@ -149,7 +154,7 @@ func (ns *Netstorage) _request(kwargs map[string]string) (*http.Response, string
149154

150155
// Dir returns the directory structure
151156
func (ns *Netstorage) Dir(nsPath string) (*http.Response, string, error) {
152-
return ns._request(map[string]string{
157+
return ns._request(map[string]interface{}{
153158
"action": "dir&format=xml",
154159
"method": "GET",
155160
"path": nsPath,
@@ -173,7 +178,7 @@ func (ns *Netstorage) Download(path ...string) (*http.Response, string, error) {
173178
localDestination = path[1]
174179
}
175180

176-
return ns._request(map[string]string{
181+
return ns._request(map[string]interface{}{
177182
"action": "download",
178183
"method": "GET",
179184
"path": nsSource,
@@ -183,7 +188,7 @@ func (ns *Netstorage) Download(path ...string) (*http.Response, string, error) {
183188

184189
// Du returns the disk usage information for a directory
185190
func (ns *Netstorage) Du(nsPath string) (*http.Response, string, error) {
186-
return ns._request(map[string]string{
191+
return ns._request(map[string]interface{}{
187192
"action": "du&format=xml",
188193
"method": "GET",
189194
"path": nsPath,
@@ -192,7 +197,7 @@ func (ns *Netstorage) Du(nsPath string) (*http.Response, string, error) {
192197

193198
// Stat returns the information about an object structure
194199
func (ns *Netstorage) Stat(nsPath string) (*http.Response, string, error) {
195-
return ns._request(map[string]string{
200+
return ns._request(map[string]interface{}{
196201
"action": "stat&format=xml",
197202
"method": "GET",
198203
"path": nsPath,
@@ -201,7 +206,7 @@ func (ns *Netstorage) Stat(nsPath string) (*http.Response, string, error) {
201206

202207
// Mkdir creates an empty directory
203208
func (ns *Netstorage) Mkdir(nsPath string) (*http.Response, string, error) {
204-
return ns._request(map[string]string{
209+
return ns._request(map[string]interface{}{
205210
"action": "mkdir",
206211
"method": "POST",
207212
"path": nsPath,
@@ -210,7 +215,7 @@ func (ns *Netstorage) Mkdir(nsPath string) (*http.Response, string, error) {
210215

211216
// Rmdir deletes an empty directory
212217
func (ns *Netstorage) Rmdir(nsPath string) (*http.Response, string, error) {
213-
return ns._request(map[string]string{
218+
return ns._request(map[string]interface{}{
214219
"action": "rmdir",
215220
"method": "POST",
216221
"path": nsPath,
@@ -219,7 +224,7 @@ func (ns *Netstorage) Rmdir(nsPath string) (*http.Response, string, error) {
219224

220225
// Mtime changes a file’s mtime
221226
func (ns *Netstorage) Mtime(nsPath string, mtime int64) (*http.Response, string, error) {
222-
return ns._request(map[string]string{
227+
return ns._request(map[string]interface{}{
223228
"action": fmt.Sprintf("mtime&format=xml&mtime=%d", mtime),
224229
"method": "POST",
225230
"path": nsPath,
@@ -228,7 +233,7 @@ func (ns *Netstorage) Mtime(nsPath string, mtime int64) (*http.Response, string,
228233

229234
// Delete deletes an object/symbolic link
230235
func (ns *Netstorage) Delete(nsPath string) (*http.Response, string, error) {
231-
return ns._request(map[string]string{
236+
return ns._request(map[string]interface{}{
232237
"action": "delete",
233238
"method": "POST",
234239
"path": nsPath,
@@ -238,7 +243,7 @@ func (ns *Netstorage) Delete(nsPath string) (*http.Response, string, error) {
238243
// QuickDelete deletes a directory (i.e., recursively delete a directory tree)
239244
// In order to use this func, you need to the privilege on the CP Code.
240245
func (ns *Netstorage) QuickDelete(nsPath string) (*http.Response, string, error) {
241-
return ns._request(map[string]string{
246+
return ns._request(map[string]interface{}{
242247
"action": "quick-delete&quick-delete=imreallyreallysure",
243248
"method": "POST",
244249
"path": nsPath,
@@ -247,7 +252,7 @@ func (ns *Netstorage) QuickDelete(nsPath string) (*http.Response, string, error)
247252

248253
// Rename renames a file or symbolic link.
249254
func (ns *Netstorage) Rename(nsTarget, nsDestination string) (*http.Response, string, error) {
250-
return ns._request(map[string]string{
255+
return ns._request(map[string]interface{}{
251256
"action": "rename&destination=" + url.QueryEscape(nsDestination),
252257
"method": "POST",
253258
"path": nsTarget,
@@ -256,7 +261,7 @@ func (ns *Netstorage) Rename(nsTarget, nsDestination string) (*http.Response, st
256261

257262
// Symlink creates a symbolic link.
258263
func (ns *Netstorage) Symlink(nsTarget, nsDestination string) (*http.Response, string, error) {
259-
return ns._request(map[string]string{
264+
return ns._request(map[string]interface{}{
260265
"action": "symlink&target=" + url.QueryEscape(nsTarget),
261266
"method": "POST",
262267
"path": nsDestination,
@@ -284,10 +289,24 @@ func (ns *Netstorage) Upload(localSource, nsDestination string) (*http.Response,
284289
return nil, "", fmt.Errorf("[NetstorageError] You should upload a file, not %s", localSource)
285290
}
286291

287-
return ns._request(map[string]string{
292+
return ns._request(map[string]interface{}{
288293
"action": "upload",
289294
"method": "PUT",
290295
"source": localSource,
291296
"path": nsDestination,
292297
})
293298
}
299+
300+
// UploadContent uploads an object directly with its content.
301+
func (ns *Netstorage) UploadContent(reader io.Reader, nsDestination string) (*http.Response, string, error) {
302+
if strings.HasSuffix(nsDestination, "/") {
303+
return nil, "", fmt.Errorf("[NetstorageError] Destination path should not be a directory")
304+
}
305+
306+
return ns._request(map[string]interface{}{
307+
"action": "upload",
308+
"method": "PUT",
309+
"content": reader,
310+
"path": nsDestination,
311+
})
312+
}

netstorage_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package netstorage
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -117,6 +118,17 @@ func TestNetstorage(t *testing.T) {
117118
return
118119
}
119120

121+
res, body, err = ns.UploadContent(bytes.NewBufferString(testString), tempNsFile)
122+
wrong = assertEqual(t, res.StatusCode, 200,
123+
"Upload",
124+
fmt.Sprintf("[TEST] Upload content '%s' to %s done\n", testString, tempNsFile),
125+
body,
126+
err,
127+
)
128+
if wrong {
129+
return
130+
}
131+
120132
res, body, err = ns.Du(tempNsDir)
121133
wrong = assertEqual(t, res.StatusCode, 200,
122134
"Du",

0 commit comments

Comments
 (0)