Dear Gitlab users, due to maintenance reasons, Gitlab will not be available on Thursday 30.09.2021 from 5:00 pm to approximately 5:30 pm.

Commit 0eccb275 authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Add github.PutFile, reorganize passing of values.

parent 17149133
{
"ListenSpec": 8000,
"ListenSpec": 8081,
"Verbose": false,
"APIRoot": "/api/v1",
"FileAPI": "/file",
"WebhookAPI": "/hooks/receivers/github/events/",
"ZenodoRepo": {
"Zenodo": {
"prefix": "10.5072/zenodo.",
"host": "https://sandbox.zenodo.org",
"port": 443,
"token": "aBcDeFgHiJkLmNoPqRsTuVwXyZ"
},
"AllowedGit": {
"host": "https://github.com",
"Git": {
"host": "https://api.github.com",
"token": "aBcDeFgHiJkLmNoPqRsTuVwXyZ",
"user": "digicademy",
"repo": "digicademy/svsal",
"commit_keyword": "",
"commit_dontpublish_keyword": "test"
"commit_phrase": "",
"commit_dontpublish_phrase": "test"
},
"metadata": {
"fields": [
......
......@@ -2,11 +2,14 @@
package github
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"strconv"
"strings"
......@@ -90,7 +93,8 @@ type CommitDetails struct {
// CommitMeta stores information about committer, tree etc
type CommitMeta struct {
URL string
SHA string `json:"sha"`
NodeID string `json:"node_id"`
Author User
Committer User
Message string
......@@ -98,6 +102,9 @@ type CommitMeta struct {
Tree Commit
Parents []Commit
Verification Verification
URL string
HTMLURL string `json:"html_url"`
}
// Verification stores information about a commit's verification
......@@ -131,6 +138,7 @@ type File struct {
Additions int
Deletions int
Changes int
SHA string `json:"sha"`
Status string
PreviousFilename string `json:"previous_filename"`
Patch string
......@@ -139,6 +147,31 @@ type File struct {
ContentURL string `json:"content_url"`
}
// FileResponse stores a github response to a file create/update request
type FileResponse struct {
Content fileResponseContent
Commit CommitMeta
}
type fileResponseContent struct {
Name string
Path string
SHA string
Size int
URL string
HTMLURL string `json:"html_url"`
GitURL string `json:"git_url"`
DownloadURL string `json:"download_url"`
Type string
Links links `json:"_links"`
}
type links struct {
Self string
Git string
HTML string
}
// User stores information about a person
type User struct {
Name string
......@@ -263,14 +296,16 @@ type Repo struct {
// FileInfo stores a file URL, modification type and modification timestamp
// (to be used as value in a map keyed to the filenames)
type FileInfo struct {
URL string
Change string
Date time.Time
URL string
Change string
SHA string
Date time.Time
DoPublish bool
}
// ====== Functions =======
// 1. parse github push request -> get commits, check repo, pusher, keywords in commit msgs
// 1. parse github push request -> get commits, check repo, pusher, phrase in commit msgs
// 2. retrieve commits -> get files
// 3. retrieve files -> pipe to zenodo
......@@ -291,11 +326,11 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
log.Printf("Error processing push hook: %+v", err)
return false, nil, tei2zenodo.NewError("errInternal", fmt.Sprintf("error processing push hook: %s", err.Error()), 500, err)
}
if payload.Repository.FullName != conf.AllowedGit.Repo && conf.AllowedGit.Repo != "" {
if payload.Repository.FullName != conf.Git.Repo && conf.Git.Repo != "" {
log.Printf("Repo not allowed: %s", payload.Repository.FullName)
return false, nil, tei2zenodo.NewError("errBadConfig", fmt.Sprintf("repo not allowed: %s", payload.Repository.FullName), 500, nil)
}
if payload.Pusher.Name != conf.AllowedGit.User && conf.AllowedGit.User != "" {
if payload.Pusher.Name != conf.Git.User && conf.Git.User != "" {
log.Printf("Git user not allowed as pusher: %s", payload.Pusher.Name)
return false, nil, tei2zenodo.NewError("errBadConfig", fmt.Sprintf("git user not allowed as pusher: %s", payload.Pusher.Name), 500, nil)
}
......@@ -312,9 +347,9 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
if conf.Verbose {
log.Printf(" commit %s ...", sha)
}
if !(strings.Contains(c.Message, conf.AllowedGit.Keyword)) && conf.AllowedGit.Keyword != "" {
if !(strings.Contains(c.Message, conf.Git.Phrase)) && conf.Git.Phrase != "" {
if conf.Verbose {
log.Printf("Required keyword not contained in commit message for commit %s", sha)
log.Printf("Required phrase not contained in commit message for commit %s", sha)
}
continue
}
......@@ -333,11 +368,6 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
files[k] = v
}
}
if !(strings.Contains(c.Message, conf.AllowedGit.DontPublishKeyword)) && conf.AllowedGit.DontPublishKeyword != "" {
log.Printf("!!! Don't-publish-keyword '%s' contained in commit message for commit %s,\n", conf.AllowedGit.DontPublishKeyword, sha)
log.Printf("!!! not going to publish zenodo deposit !!!")
doPublish = false
}
}
// log.Printf("Parsed hook: %+v", payload)
if conf.Verbose {
......@@ -353,6 +383,7 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
}
case "ping":
{
log.Printf("Github Ping event received")
return false, nil, nil
}
default:
......@@ -369,7 +400,9 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
// Compile GET request to retrieve commit information
targetURI := strings.Replace(commitsURL, "{/sha}", "/"+sha, -1)
req, err := http.NewRequest("GET", targetURI, nil)
req.Header.Add("Authorization", "Bearer "+conf.AllowedGit.Token)
req.Header.Add("Authorization", "token "+conf.Git.Token)
req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send GET request
if conf.Verbose {
......@@ -404,6 +437,14 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
return nil, tei2zenodo.NewError("errParse", fmt.Sprintf("problem parsing github's response: %s", err), 500, err)
}
var doPublish bool
if !(strings.Contains(commit.Commit.Message, conf.Git.DontPublishPhrase)) && conf.Git.DontPublishPhrase != "" {
log.Printf("!!! Don't-publish-Phrase '%s' contained in commit message for commit %s,\n", conf.Git.DontPublishPhrase, sha)
doPublish = false
} else {
doPublish = true
}
// layout := "2006-01-02T15:04:05.000Z"
str := commit.Commit.Committer.Date
commitDate, err := time.Parse(time.RFC3339, str)
......@@ -412,15 +453,19 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
for _, f := range commit.Files {
filename := f.Filename
url := f.RawURL
sha := f.SHA
modType := f.Status
if modType != "deleted" && modType != "renamed" {
counter++
if conf.Verbose {
log.Printf(" %d. '%s' (%s): %s", counter, filename, modType, url)
log.Printf(" %d. %s '%s' (%s): %s", counter, sha, filename, modType, url)
} else {
log.Printf(" %d. '%s' (%s)", counter, filename, modType)
}
files[filename] = FileInfo{URL: url, Change: modType, Date: commitDate}
if !doPublish {
log.Printf("!!! not going to publish zenodo deposit for %s !!!", filename)
}
files[filename] = FileInfo{URL: url, Change: modType, Date: commitDate, SHA: sha, DoPublish: doPublish}
} else {
if conf.Verbose {
log.Printf(" ignore %s file %s: %s", modType, filename, url)
......@@ -429,3 +474,121 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
}
return files, nil
}
// PutFile uploads a file to the github repository
// it returns the commit sha and an error value
func PutFile(myDeposit *tei2zenodo.Deposit, c *tei2zenodo.Config) (string, error) {
conf := c.Git
filename := myDeposit.Filename
sha := myDeposit.FileGithubSHA
log.Printf("--- Upload new version to github ---")
// Read file from myDeposit
// fileContent := []byte(myDeposit.FileContent)
// reader := strings.NewReader(myDeposit.FileContent)
// buf := new(bytes.Buffer)
// buf.ReadFrom(reader)
// For testing: uri := "https://postman-echo.com/post"
// https://api.github.com/repos/:owner/:repo/contents/:path
uri := conf.Host + "/repos/" + conf.Repo + "/contents/" + filename // ?access_token=" + conf.Token
if c.Verbose {
log.Printf(" Put request to: %s", uri)
log.Printf(" Upload filename: %s", filename)
log.Printf(" Upload file: %s ...", myDeposit.FileContent[:80])
} else {
log.Printf(" Put file %s to github", filename)
}
// Compile upload form and prepare PUT request
// parameters:
// message string Required. The commit message.
// content string Required. The new file content, using Base64 encoding.
// sha string Required if you are updating a file. The blob SHA of the file being replaced.
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
msg, err := writer.CreateFormField("message")
if err != nil {
log.Printf("Problem creating PUT request body: %v ...", err)
return "", fmt.Errorf("problem creating PUT request body: %s", err)
}
msg.Write([]byte("Update zenodo DOI via tei2zenodo."))
shaID, err := writer.CreateFormField("sha")
if err != nil {
log.Printf("Problem creating PUT request body: %v ...", err)
return "", fmt.Errorf("problem creating PUT request body: %s", err)
}
shaID.Write([]byte(sha))
uploadContent, err := writer.CreateFormField("content")
if err != nil {
log.Printf("Problem creating PUT request body: %v ...", err)
return "", fmt.Errorf("problem creating PUT request body: %s", err)
}
encoder := base64.NewEncoder(base64.StdEncoding, uploadContent)
encoder.Write([]byte(myDeposit.FileContent))
encoder.Close()
// content.Close()
// content.Write(base64.StdEncoding.EncodeToString(fileContent))
/*
part, err := writer.CreateFormFile(`content`, filename)
if err != nil {
log.Printf("Problem creating PUT request body: %v ...", err)
return "", fmt.Errorf("problem creating PUT request body: %s", err)
}
io.Copy(part, reader)
*/
writer.Close()
req, err := http.NewRequest("PUT", uri, body)
if err != nil {
log.Printf("Problem creating PUT request: %v ...", err)
return "", fmt.Errorf("problem creating PUT request: %s", err)
}
// req.Header.Add(`Content-Type`, writer.FormDataContentType())
req.Header.Add("Authorization", "token "+conf.Token)
req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send PUT request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Problem sending PUT request: %v ...", err)
return "", fmt.Errorf("problem sending PUT request: %s", err)
}
defer resp.Body.Close()
// Handle problematic responses
responseContent, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Problem reading PUT response: %v ...", err)
return "", fmt.Errorf("problem reading PUT response: %s", err)
}
if strconv.Itoa(resp.StatusCode)[:1] != "2" {
log.Printf("Problem reported by github: %d %v. %s ...", resp.StatusCode, err, responseContent)
return "", fmt.Errorf("problem reported by github: %v. %s", err, responseContent)
}
// Parse response
var parsedContent FileResponse
err = json.Unmarshal(responseContent, &parsedContent)
if err != nil {
log.Printf("Problem parsing github's response: %v ...", err)
return "", fmt.Errorf("problem parsing github's response: %s", err)
}
// log.Printf("Response code %d, content: %s", resp.StatusCode, content)
if c.Verbose {
log.Printf(" Success (code %d (expected 200), %s Bytes). Commit SHA: %s", resp.StatusCode, strconv.Itoa(int(parsedContent.Content.Size)), parsedContent.Commit.SHA)
} else {
log.Printf(" Success.")
}
return "", nil
}
......@@ -37,7 +37,6 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
APIv1.POST(conf.FileAPI, func(c *gin.Context) {
var r io.ReadSeeker
var doPublish bool
var myDeposit tei2zenodo.Deposit
if conf.Verbose {
......@@ -69,13 +68,13 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// Get doPublish from request (false if not set)
if c.Request.FormValue(`doPublish`) == "True" {
doPublish = true
myDeposit.DoPublish = true
} else {
doPublish = false
myDeposit.DoPublish = false
}
// Send file to processing...
PFErr := zenodo.ProcessFile(&conf, &myDeposit, r, doPublish)
PFErr := zenodo.ProcessFile(r, &myDeposit, &conf)
if PFErr != nil {
switch PFErr.Typ {
case "errNoTEIXML":
......@@ -128,11 +127,15 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// Send each file to processing
for f := range files {
var myDeposit tei2zenodo.Deposit
log.Printf("\n")
log.Printf("Processing file '%s'", f)
myDeposit.Filename = strings.Replace(f, "/", "_", -1)
PDErr := zenodo.ProcessDownloadFile(&conf, &myDeposit, files[f].URL, doPublish)
var myDeposit tei2zenodo.Deposit
// myDeposit.Filename = strings.Replace(f, "/", "_", -1)
myDeposit.Filename = f
myDeposit.FileGithubSHA = files[f].SHA
myDeposit.DoPublish = files[f].DoPublish
myDeposit.FileGHURI = files[f].URL
PDErr := zenodo.ProcessDownloadFile(&myDeposit, &conf)
if PDErr != nil {
switch PDErr.Typ {
case "errNoTEIXML":
......@@ -148,6 +151,18 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
}
}
}
/*
// The upload to github requires more than just a token:
// you need to authenticate an app to act in lieu of the user in github...
// See https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/
commitSHA, GHErr := github.PutFile(&myDeposit, &conf)
if GHErr != nil {
log.Printf("Problem uploading file %s to github: %+v", f, GHErr)
AbortMsg(500, tei2zenodo.NewError("errGHProcessing", fmt.Sprintf("problem uploading file %s to github", myDeposit.Filename), 500, GHErr), c)
return
}
log.Printf(" New file %s committed to github (%s).", f, commitSHA)
*/
}
log.Printf("====== All done ======")
......
......@@ -48,15 +48,17 @@ type ZPostData struct {
// ====== Functions ======
// ProcessDownloadFile takes a URL, downloads the file and continues processing
func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, url string, doPublish bool) *tei2zenodo.Error {
func ProcessDownloadFile(myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) *tei2zenodo.Error {
// Compile GET request
targetURI := url
targetURI := myDeposit.FileGHURI
req, RQErr := http.NewRequest("GET", targetURI, nil)
if RQErr != nil {
log.Printf("Problem creating GET request: %v ...", RQErr)
return tei2zenodo.NewError("errInternal", fmt.Sprintf("error creating GET request: %s", RQErr.Error()), 500, RQErr)
}
req.Header.Add("Authorization", "Bearer "+conf.AllowedGit.Token)
req.Header.Add("Authorization", "Bearer "+conf.Git.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send GET request
client := &http.Client{}
......@@ -74,7 +76,7 @@ func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit,
file := buf.String()
r = strings.NewReader(file)
PFErr := ProcessFile(conf, myDeposit, r, doPublish)
PFErr := ProcessFile(r, myDeposit, conf)
if PFErr != nil {
switch PFErr.Typ {
case "errNoTEIXML":
......@@ -92,7 +94,9 @@ func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit,
}
// ProcessFile takes a file and processes parsing and zenodo upload
func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.ReadSeeker, doPublish bool) *tei2zenodo.Error {
func ProcessFile(r io.ReadSeeker, myDeposit *tei2zenodo.Deposit, conf *tei2zenodo.Config) *tei2zenodo.Error {
doPublish := myDeposit.DoPublish
var md tei2zenodo.ZMetadata
......@@ -153,12 +157,13 @@ func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.Re
r.Seek(0, 0)
log.Printf("--- Upload to zenodo ---")
url, PFErr := postFile(r, myDeposit.Filename, &md, conf)
uploadFilename := strings.Replace(myDeposit.Filename, "/", "_", -1) // We replace the / (SOLIDUS, U+002F) with ∕ (DIVISION SLASH, U+2215)
url, PFErr := postFile(r, uploadFilename, &md, conf)
if PFErr != nil {
log.Printf("Error uploading to zenodo, sending POST request: %v", PFErr)
return tei2zenodo.NewError("errInternal", fmt.Sprintf("error uploading to zenodo, sending POST request: %s", PFErr), 500, PFErr)
}
myDeposit.FileURI = url
myDeposit.FileZURI = url
log.Printf("--- Add metadata to zenodo ---")
PMErr := putMetadata(myDeposit, &md, conf)
......@@ -182,7 +187,7 @@ func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.Re
log.Printf("!!! log in and click on the 'Upload' button.\n")
log.Printf("!!! If you want these commits to be published in general, either\n")
log.Printf("!!! - set doPublish=True as request parameter or\n")
log.Printf("!!! - remove the 'commit_dontpublish_keyword' from\n")
log.Printf("!!! - remove the 'commit_dontpublish_phrase' from\n")
log.Printf("!!! either the tei2zenodo service config or your commit messages.\n")
}
return nil
......@@ -192,7 +197,7 @@ func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.Re
// it returns the DOI of the new deposit and an error value
func createDeposit(targetURI string, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error) {
conf := c.ZenodoRepo
conf := c.Zenodo
// Compile POST request
if c.Verbose {
log.Printf(" Post request to: %s", targetURI)
......@@ -220,6 +225,7 @@ func createDeposit(targetURI string, md *tei2zenodo.ZMetadata, c *tei2zenodo.Con
}
req.Header.Add("Content-Type", `application/json`)
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send POST request
client := &http.Client{}
......@@ -269,8 +275,8 @@ func createDeposit(targetURI string, md *tei2zenodo.ZMetadata, c *tei2zenodo.Con
// createNewDOI creates a Deposit from Scratch
// it returns a new zenodo DOI for the deposit and an error value
func createNewDOI(md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error) {
conf := c.ZenodoRepo
targetURI := conf.Host + ":" + strconv.Itoa(int(conf.Port)) + "/api/deposit/depositions"
conf := c.Zenodo
targetURI := conf.Host + "/api/deposit/depositions"
doi, err := createDeposit(targetURI, md, c)
if err != nil {
log.Printf("Problem creating new DOI/deposit: %v ...", err)
......@@ -282,7 +288,7 @@ func createNewDOI(md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error
// updateDeposit retrieves a zenodo deposit based on a gived DOI and creates a new version
// it returns a new zenodo DOI for the deposit and an error value
func updateDeposit(d *tei2zenodo.Deposit, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error) {
conf := c.ZenodoRepo
conf := c.Zenodo
if d.OldDOI[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", d.OldDOI)
return "", fmt.Errorf("invalid DOI value")
......@@ -290,9 +296,10 @@ func updateDeposit(d *tei2zenodo.Deposit, md *tei2zenodo.ZMetadata, c *tei2zenod
oldDOI := d.OldDOI[len(conf.Prefix):]
// Compile GET request
targetURI := conf.Host + ":" + strconv.Itoa(int(conf.Port)) + "/api/deposit/depositions/" + oldDOI
targetURI := conf.Host + "/api/deposit/depositions/" + oldDOI
req, err := http.NewRequest("GET", targetURI, nil)
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send GET request
if c.Verbose {
......@@ -345,7 +352,7 @@ func updateDeposit(d *tei2zenodo.Deposit, md *tei2zenodo.ZMetadata, c *tei2zenod
// getFiles gets all files associated to a deposit
// it returns a slice of tei2zenodo.ZFile objects
func getFiles(doi string, c *tei2zenodo.Config) ([]tei2zenodo.ZFile, error) {
conf := c.ZenodoRepo
conf := c.Zenodo
if doi[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", doi)
......@@ -354,9 +361,10 @@ func getFiles(doi string, c *tei2zenodo.Config) ([]tei2zenodo.ZFile, error) {
id := doi[len(conf.Prefix):]
// Compile GET request
targetURI := conf.Host + ":" + strconv.Itoa(int(conf.Port)) + "/api/deposit/depositions/" + id + "/files"
targetURI := conf.Host + "/api/deposit/depositions/" + id + "/files"
req, err := http.NewRequest("GET", targetURI, nil)
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send GET request
if c.Verbose {
......@@ -391,13 +399,17 @@ func getFiles(doi string, c *tei2zenodo.Config) ([]tei2zenodo.ZFile, error) {
return nil, fmt.Errorf("problem parsing resolver's response: %s", err)
}
if c.Verbose {
log.Printf(" Files: %+v", files)
}
return files, nil
}
// deleteFileByID deletes a file from a zenodo deposit by its ID
// it returns an error value
func deleteFileByID(doi string, id string, c *tei2zenodo.Config) error {
conf := c.ZenodoRepo
conf := c.Zenodo
if doi[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", doi)
......@@ -406,9 +418,10 @@ func deleteFileByID(doi string, id string, c *tei2zenodo.Config) error {
dID := doi[len(conf.Prefix):]
// Compile DELETE request
targetURI := conf.Host + ":" + strconv.Itoa(int(conf.Port)) + "/api/deposit/depositions/" + dID + "/files/" + id
targetURI := conf.Host + "/api/deposit/depositions/" + dID + "/files/" + id
req, err := http.NewRequest("DELETE", targetURI, nil)
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
// Send DELETE request
if c.Verbose {
......@@ -440,7 +453,7 @@ func deleteFileByID(doi string, id string, c *tei2zenodo.Config) error {
// mustDeleteFileByName deletes a file from a zenodo deposit by its name
// it returns an error value and fails when the file is not present
func mustDeleteFileByName(doi string, filename string, c *tei2zenodo.Config) error {
conf := c.ZenodoRepo
conf := c.Zenodo
if doi[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", doi)
......@@ -473,7 +486,7 @@ func mustDeleteFileByName(doi string, filename string, c *tei2zenodo.Config) err
// it does not raise an error if the file does not exist in the first place.
// it returns an error value
func deleteFileByName(doi string, filename string, c *tei2zenodo.Config) error {
conf := c.ZenodoRepo
conf := c.Zenodo
if doi[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", doi)
return fmt.Errorf("invalid DOI value")
......@@ -495,14 +508,14 @@ func deleteFileByName(doi string, filename string, c *tei2zenodo.Config) error {
return nil
}
}
log.Printf(" No file %s to delete.", filename)
log.Printf(" No file %s to delete.", filename)
return nil
}
// postFile posts a file to zenodo, taking the id from the md.DOI field
// it returns an URI for the upload and an error value
func postFile(r io.Reader, filename string, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error) {
conf := c.ZenodoRepo
conf := c.Zenodo
if md.DOI[:len(conf.Prefix)] != conf.Prefix {
log.Printf("Problem: DOI %s is not a zenodo DOI.", md.DOI)
......@@ -524,7 +537,7 @@ func postFile(r io.Reader, filename string, md *tei2zenodo.ZMetadata, c *tei2zen
reader := strings.NewReader(file)
// For testing: uri := "https://postman-echo.com/post"
uri := conf.Host + ":" + strconv.Itoa(int(conf.Port)) + "/api/deposit/depositions/" + id + "/files" // ?access_token=" + conf.Token
uri := conf.Host + "/api/deposit/depositions/" + id + "/files" // ?access_token=" + conf.Token
if c.Verbose {
log.Printf(" Post request to: %s", uri)
log.Printf(" Upload filename: %s", filename)
......@@ -559,6 +572,7 @@ func postFile(r io.Reader, filename string, md *tei2zenodo.ZMetadata, c *tei2zen
}
req.Header.Add(`Content-Type`, writer.FormDataContentType())
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Add("User-Agent", "tei2zenodo (https://gitlab.gwdg.de/rg-mpg-de/tei2zenodo/)")
reader.Seek(0, 0)
......@@ -602,7 +616,7 @@ func postFile(r io.Reader, filename string, md *tei2zenodo.ZMetadata, c *tei2zen