Commit e9f2a83e authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Initial commit?

parent 38ee0d84
......@@ -6,11 +6,11 @@ import (
"strings"
"github.com/sirupsen/logrus"
"gitlab.gwdg.de/rg-mpg-de/policey"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
// ConfigureLogging will take the logging configuration and also adds a few default parameters.
func ConfigureLogging(config *policey.LoggingConfig) (*logrus.Entry, error) {
func ConfigureLogging(config *tei2zenodo.LoggingConfig) (*logrus.Entry, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
......
......@@ -4,8 +4,6 @@ import (
"bytes"
"log"
"net/http"
"strconv"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
......@@ -38,8 +36,8 @@ func SetupRoutes() *gin.Engine {
// log.Printf("Here is the file: %s", buf)
// Parse TEI file
var md zenodo2tei.ZMetadata
doi, err := xml.ParseTEI(%md, buf)
var md *tei2zenodo.ZMetadata
doi, err := xml.ParseTEI(md, buf)
if err != nil {
log.Printf("Error (%s) parsing TEI file: %v", err, buf)
AbortMsg(500, err, c)
......@@ -48,29 +46,41 @@ func SetupRoutes() *gin.Engine {
switch doi {
case "":
// prereserve doi
err := zenodo.GetDOI(buf, %md, c.Writer)
err := zenodo.GetDOI(buf, md)
if err != nil {
log.Printf("Error creating DOI reservation deposit: %v", err)
AbortMsg(500, err, c)
}
// If we have any, stream laws
timestamp := time.Now().UTC().Format("(2006-01-02_15-04-05)")
downloadName := "policey_ordinances_" + rangeString +
timestamp + "." + format
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+downloadName)
// timestamp := time.Now().UTC().Format("(2006-01-02_15-04-05)")
// c.Writer.Header().Set("Content-Disposition", "attachment; filename="+downloadName)
c.Writer.Header().Set("Content-Description", "File Transfer")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
c.Writer.Header().Set("X-Accel-Buffering", "no")
c.Writer.Header().Set("Content-type", "text/xml")
err := zenodo.PostFile(buf, %md, c.Writer)
if err != nil {
log.Printf("Error posting file: %v", err)
AbortMsg(500, err, c)
}
err := zenodo.PutMetadata(buf, %md, c.Writer)
/*
err := zenodo.PostFile(buf, %md)
if err != nil {
log.Printf("Error posting file: %v", err)
AbortMsg(500, err, c)
}
err := zenodo.Publish(buf, %md, c.Writer)
err := zenodo.PutMetadata(buf, %md)
if err != nil {
log.Printf("Error posting file: %v", err)
AbortMsg(500, err, c)
}
case else:
err := zenodo.Publish(buf, %md)
if err != nil {
log.Printf("Error posting file: %v", err)
AbortMsg(500, err, c)
}
*/
default:
log.Printf("DOI present: %v", doi)
}
})
APIv1.GET("/webhook", func(c *gin.Context) {
......
......@@ -2,60 +2,95 @@ package xml
import (
"bytes"
"encoding/xml"
"io"
"strings"
"log"
"github.com/antchfx/xmlquery"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/zenodo"
)
// ParsesTEI reads a TEI file and parses its metadata into a ZMetadata variable. Returns a doi (maybe empty) and an error value.
func ParseTEI(md *tei2zenodo.ZMetadata, buf *bytes.Buffer) string, error {
// ParseTEI reads a TEI file and parses its metadata into a ZMetadata variable. Returns a doi (maybe empty) and an error value.
func ParseTEI(md *tei2zenodo.ZMetadata, buf *bytes.Buffer) (string, error) {
// This is wih antchfx/xmlquery...
doc, err := xmlquery.Parse(buf)
if err != nil {
log.Printf("Could not parse xml.\n")
return "", err
}
idno := xmlquery.FindOne(doc, "//publicationStmt//idno[@type='doi']")
var doi string
if n := idno; n != nil {
doi = n.InnerText()
log.Printf("doi found: %s\n", n.InnerText())
} else {
doi = ""
log.Printf("no doi found.\n")
}
return doi, nil
// this is with standard xml module, decoding everything...
/*
decoder := xml.NewDecoder(strings.NewReader(string(buf)))
list := xmlquery.Find(doc, "//author")
//Iterate through the fields of a struct
v := reflect.ValueOf(md)
typeOfS := v.Type()
for i := 0; i< v.NumField(); i++ {
val := v.Field(i).Interface().(int)
s.Base[typeOfS.Field(i).Name] = val
}
return s
for {
// err is ignore here. IF you are reading from a XML file
// do not ignore err and also check for io.EOF
token, _ := decoder.Token()
for i, n := range xmlquery.Find(doc, "//item/title") {
fmt.Printf("#%d %s\n", i, n.InnerText())
}
if token == nil {
break
}
expr, err := xpath.Compile("sum(//book/price)")
price := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64)
*/
switch Element := token.(type) {
case xml.StartElement:
if Element.Name.Local == "teiHeader" {
fmt.Println("Element name is : ", Element.Name.Local)
// This is with standard xml module, decoding everything...
/*
decoder := xml.NewDecoder(strings.NewReader(string(buf)))
err := decoder.DecodeElement(&l, &Element)
if err != nil {
fmt.Println(err)
}
for {
fmt.Println("Element value is : ", l.Loc)
}
}
// err is ignore here. IF you are reading from a XML file
// do not ignore err and also check for io.EOF
token, _ := decoder.Token()
if token == nil {
break
}
// example on handling XML attribute
switch Element := token.(type) {
case xml.StartElement:
if Element.Name.Local == "teiHeader" {
fmt.Println("Element name is : ", Element.Name.Local)
switch Element := token.(type) {
case xml.StartElement:
if Element.Name.Local == "title" {
fmt.Println("Element name is : ", Element.Name.Local)
attrName := Element.Attr[0].Name.Local
attrValue := Element.Attr[0].Value
fmt.Printf("Attribute name is [%s] and value is [%s] \n", attrName, attrValue)
err := decoder.DecodeElement(&l, &Element)
if err != nil {
fmt.Println(err)
}
fmt.Println("Element value is : ", l.Loc)
}
}
// example on handling XML attribute
switch Element := token.(type) {
case xml.StartElement:
if Element.Name.Local == "title" {
fmt.Println("Element name is : ", Element.Name.Local)
attrName := Element.Attr[0].Name.Local
attrValue := Element.Attr[0].Value
fmt.Printf("Attribute name is [%s] and value is [%s] \n", attrName, attrValue)
}
}
}
}
}
*/
*/
}
......@@ -2,54 +2,55 @@ package zenodo
import (
"bytes"
"encoding/json"
"net/http"
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
func GetDOI (buf *bytes.Buffer) {
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
// GetDOI creates an empty zenodo deposit and reserves a DOI
func GetDOI(buf *bytes.Buffer, md *tei2zenodo.ZMetadata) error {
resp, err := http.Post("http://example.com/upload", "image/jpeg", buf)
if err != nil {
// handle error
}
defer resp.Body.Close()
return nil
/* This call should return something like this, from which we want to retrieve the id:
{
"created": "2016-06-15T16:10:03.319363+00:00",
"files": [],
"id": 1234,
"links": {
"discard": "https://zenodo.org/api/deposit/depositions/1234/actions/discard",
"edit": "https://zenodo.org/api/deposit/depositions/1234/actions/edit",
"files": "https://zenodo.org/api/deposit/depositions/1234/files",
"publish": "https://zenodo.org/api/deposit/depositions/1234/actions/publish",
"newversion": "https://zenodo.org/api/deposit/depositions/1234/actions/newversion",
"self": "https://zenodo.org/api/deposit/depositions/1234"
},
"metadata": {
"prereserve_doi": {
"doi": "10.5072/zenodo.1234",
"recid": 1234
}
},
"modified": "2016-06-15T16:10:03.319371+00:00",
"owner": 1,
"record_id": 1234,
"state": "unsubmitted",
"submitted": false,
"title": ""
}
{
"created": "2016-06-15T16:10:03.319363+00:00",
"files": [],
"id": 1234,
"links": {
"discard": "https://zenodo.org/api/deposit/depositions/1234/actions/discard",
"edit": "https://zenodo.org/api/deposit/depositions/1234/actions/edit",
"files": "https://zenodo.org/api/deposit/depositions/1234/files",
"publish": "https://zenodo.org/api/deposit/depositions/1234/actions/publish",
"newversion": "https://zenodo.org/api/deposit/depositions/1234/actions/newversion",
"self": "https://zenodo.org/api/deposit/depositions/1234"
},
"metadata": {
"prereserve_doi": {
"doi": "10.5072/zenodo.1234",
"recid": 1234
}
},
"modified": "2016-06-15T16:10:03.319371+00:00",
"owner": 1,
"record_id": 1234,
"state": "unsubmitted",
"submitted": false,
"title": ""
}
*/
}
func PostFile
func PutMetadata
// func PostFile
func Publish
// func PutMetadata
// func Publish
// The order is this:
......
File added
......@@ -26,8 +26,8 @@ type RepoConfig struct {
Token string
}
// AllowedGit specifies git repositories from which webhooks are accepted.
type AllowedGit struct {
// GitConfig specifies git repositories from which webhooks are accepted.
type GitConfig struct {
Host string
User string
Repo string
......@@ -39,11 +39,6 @@ type MetadataConfig struct {
XPath string
}
// Service is the interface for the main application
type Service interface {
PostFile(id string) (*Territory, error)
}
// Link HATEOAS-links our resources
type Link struct {
Rel string `json:"rel"`
......@@ -82,7 +77,7 @@ type ZMetadata struct {
RelatedIdentifiers []ZIdentifier `json:"related_identifiers"` // optional
References []string `json:"references"` // optional
Communities []ZCommunity `json:"communities"` // optional
Grants []ZGrants `json:"grants"` // optional
Grants []ZGrant `json:"grants"` // optional
JournalTitle string `json:"journal_title"` // optional
JournalVolume string `json:"journal_volume"` // optional
JournalIssue string `json:"journal_issue"` // optional
......@@ -99,7 +94,7 @@ type ZMetadata struct {
ImprintPlace string `json:"imprint_place"` // optional
PartofTitle string `json:"partof_title"` // optional
PartofPages string `json:"partof_pages"` // optional
ThesisSupervisors []ZCreators `json:"thesis_supervisors"` // optional
ThesisSupervisors []ZCreator `json:"thesis_supervisors"` // optional
ThesisUniversity string `json:"thesis_university"` // optional
Subjects []ZSubject `json:"subjects"` // optional
Version string `json:"version"` // optional
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment