Skip to content

Commit

Permalink
zip files upload workaround: upload via sword api
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykKul committed Feb 20, 2023
1 parent 5eeb374 commit 23edf31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
11 changes: 11 additions & 0 deletions image/app/dataverse/dataverse_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ func requestBody(data []byte) (io.Reader, string) {
}

func ApiAddReplaceFile(ctx context.Context, dbId int64, id, token, user, persistentId string, wg *sync.WaitGroup, async_err *core.ErrorHolder) (io.WriteCloser, error) {
if strings.HasSuffix(id, ".zip") {
// workaround: upload via SWORD api
if dbId != 0 {
err := DeleteFile(ctx, token, user, dbId)
if err != nil {
return nil, err
}
}
return uploadViaSword(ctx, dbId, id, token, user, persistentId, wg, async_err)
}

url := config.GetConfig().DataverseServer + "/api/v1/datasets/:persistentId/add?persistentId=" + persistentId
if dbId != 0 {
url = config.GetConfig().DataverseServer + "/api/v1/files/" + fmt.Sprint(dbId) + "/replace"
Expand Down
35 changes: 35 additions & 0 deletions image/app/dataverse/sword.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
package dataverse

import (
"archive/zip"
"context"
"fmt"
"integration/app/config"
"integration/app/core"
"io"
"net/http"
"sync"
)

func swordDelete(ctx context.Context, token, user string, id int64) error {
Expand All @@ -34,3 +37,35 @@ func swordDelete(ctx context.Context, token, user string, id int64) error {
}
return nil
}

func uploadViaSword(ctx context.Context, dbId int64, id, token, user, persistentId string, wg *sync.WaitGroup, async_err *core.ErrorHolder) (io.WriteCloser, error) {
url := config.GetConfig().DataverseServer + "/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/" + persistentId
pr, pw := io.Pipe()
zipWriter := zip.NewWriter(pw)
writer, _ := zipWriter.Create(id)
request, _ := http.NewRequestWithContext(ctx, "POST", url, pr)
request.Header.Add("Content-Type", "application/zip")
request.Header.Add("Content-Disposition", "filename=example.zip")
request.Header.Add("Packaging", "http://purl.org/net/sword/package/SimpleZip")
request.SetBasicAuth(token, "")

wg.Add(1)
go func(req http.Request) {
defer wg.Done()
defer pr.Close()
resp, err := http.DefaultClient.Do(request)
if err != nil {
if async_err != nil {
async_err.Err = err
}
return
}
defer resp.Body.Close()
if resp.StatusCode != 201 && async_err != nil {
b, _ := io.ReadAll(resp.Body)
async_err.Err = fmt.Errorf("writing file in %s failed: %d - %s", persistentId, resp.StatusCode, string(b))
}
}(*request)

return core.NewWritterCloser(writer, zipWriter, pw), nil
}
2 changes: 1 addition & 1 deletion image/app/dataverse/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type dvVersion string
var version = getVersion()

var filesCleanup = "5.13"
var urlSigning = "https://github.com/IQSS/dataverse/pull/9383" // will be replaced with verion when pull request is merged (needs delete via native api to work)
var urlSigning = "https://github.com/IQSS/dataverse/pull/9383" // needs delete via native api and upload zip via native api to work
var directUpload = "https://github.com/IQSS/dataverse/pull/9003" // will be replaced with verion when pull request is merged
var slashInPermissions = "https://github.com/IQSS/dataverse/pull/8995" // will be replaced with verion when pull request is merged
var nativeApiDelete = "https://github.com/IQSS/dataverse/pull/9383" // will be replaced with verion when pull request is merged
Expand Down

0 comments on commit 23edf31

Please sign in to comment.