From 23edf31321260c377cab0c09a80d8b7ddb53ea04 Mon Sep 17 00:00:00 2001 From: Eryk Kulikowski Date: Mon, 20 Feb 2023 14:59:59 +0100 Subject: [PATCH] zip files upload workaround: upload via sword api --- image/app/dataverse/dataverse_write.go | 11 ++++++++ image/app/dataverse/sword.go | 35 ++++++++++++++++++++++++++ image/app/dataverse/version.go | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/image/app/dataverse/dataverse_write.go b/image/app/dataverse/dataverse_write.go index eb26a11..3016e38 100644 --- a/image/app/dataverse/dataverse_write.go +++ b/image/app/dataverse/dataverse_write.go @@ -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" diff --git a/image/app/dataverse/sword.go b/image/app/dataverse/sword.go index a0d4453..6822c3c 100644 --- a/image/app/dataverse/sword.go +++ b/image/app/dataverse/sword.go @@ -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 { @@ -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 +} diff --git a/image/app/dataverse/version.go b/image/app/dataverse/version.go index 31bb420..390c9d8 100644 --- a/image/app/dataverse/version.go +++ b/image/app/dataverse/version.go @@ -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