From 63eda4d8d6822b2ecc64daf427ea8b98582bb85a Mon Sep 17 00:00:00 2001 From: Nikola Brdaroski Date: Fri, 5 Apr 2019 13:45:48 +0200 Subject: [PATCH 1/2] added UploadAndIndexZip function to use with serve zip feture --- netstorage.go | 54 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/netstorage.go b/netstorage.go index c98366b..dcfa5d5 100644 --- a/netstorage.go +++ b/netstorage.go @@ -48,7 +48,7 @@ func NewNetstorage(hostname, keyname, key string, ssl bool) *Netstorage { // Only for upload action. (Used by _request func) func _ifUploadAction(kwargs map[string]string) (*io.Reader, error) { var data io.Reader - if kwargs["action"] == "upload" { + if strings.Contains(kwargs["action"], "upload") { bArr, err := ioutil.ReadFile(kwargs["source"]) if err != nil { return nil, err @@ -147,6 +147,29 @@ func (ns *Netstorage) _request(kwargs map[string]string) (*http.Response, string return response, body, err } +func (ns *Netstorage) _upload(localSource, nsDestination string, action string) (*http.Response, string, error) { + s, err := os.Stat(localSource) + + if err != nil { + return nil, "", err + } + + if s.Mode().IsRegular() { + if strings.HasSuffix(nsDestination, "/") { + nsDestination = nsDestination + path.Base(localSource) + } + } else { + return nil, "", fmt.Errorf("[NetstorageError] You should upload a file, not %s", localSource) + } + + return ns._request(map[string]string{ + "action": action, + "method": "PUT", + "source": localSource, + "path": nsDestination, + }) +} + // Dir returns the directory structure func (ns *Netstorage) Dir(nsPath string) (*http.Response, string, error) { return ns._request(map[string]string{ @@ -270,24 +293,15 @@ func (ns *Netstorage) Symlink(nsTarget, nsDestination string) (*http.Response, s // will be the "localSource" parameter filename. // Note that you can upload only a file, not a directory. func (ns *Netstorage) Upload(localSource, nsDestination string) (*http.Response, string, error) { - s, err := os.Stat(localSource) - - if err != nil { - return nil, "", err - } - - if s.Mode().IsRegular() { - if strings.HasSuffix(nsDestination, "/") { - nsDestination = nsDestination + path.Base(localSource) - } - } else { - return nil, "", fmt.Errorf("[NetstorageError] You should upload a file, not %s", localSource) - } + return ns._upload(localSource, nsDestination, "upload") +} - return ns._request(map[string]string{ - "action": "upload", - "method": "PUT", - "source": localSource, - "path": nsDestination, - }) +// UploadAndIndexZip uploads an zip file and makes index to use in Serve from Zip feature. +// The first parameter is the local source path and the second is +// the Netstorage destination path. +// If you put the directory path on "nsDestination" parameter, that filename +// will be the "localSource" parameter filename. +// Note that you can upload only a file, not a directory. +func (ns *Netstorage) UploadAndIndexZip(localSource, nsDestination string) (*http.Response, string, error) { + return ns._upload(localSource, nsDestination, "upload&index-zip=1") } From 5b1129d67e282ee20ecce5706b8a590e165a6832 Mon Sep 17 00:00:00 2001 From: Nikola Brdaroski Date: Fri, 5 Apr 2019 13:49:09 +0200 Subject: [PATCH 2/2] updated readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 210da09..9a615c2 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,8 @@ ns.Rename(netstorageTarget, netstorageDestination) ns.Rmdir(netstorageDir) // remove empty direcoty ns.Stat(netstoragePath) ns.Symlink(netstorageTarget, netstorageDestination) -ns.Upload(localSource, netstorageDestination) +ns.Upload(localSource, netstorageDestination) // upload single file +ns.UploadAndIndexZip(localSource, netstorageDestination) // upload and index zip archive // INFO: can "Upload" Only a single file, not directory. ```