Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip archive index action implemented for Serve from Zip feature #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
Expand Down
54 changes: 34 additions & 20 deletions netstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}