ソースを参照

Decouple gin-doi

Temporarily copied gin-doi functions while starting to remove dependence
of GOGS on the other services.
Achilleas Koutsou 6 年 前
コミット
a8bdce8bc6
5 ファイル変更135 行追加32 行削除
  1. 0 1
      .travis.yml
  2. 0 3
      docker/build.sh
  3. 35 5
      routes/doi.go
  4. 85 17
      routes/repo/view.go
  5. 15 6
      routes/search.go

+ 0 - 1
.travis.yml

@@ -12,7 +12,6 @@ env:
 
 script:
   - go get github.com/docopt/docopt-go
-  - go get github.com/G-Node/gin-doi/src
   - go get gopkg.in/yaml.v2
   - go get github.com/G-Node/gin-dex/gindex
   - go build -v -tags "pam"

+ 0 - 3
docker/build.sh

@@ -5,12 +5,9 @@ set -e
 # Set temp environment vars
 export GOPATH=/tmp/go
 export PATH=/usr/local/go/bin:${PATH}:${GOPATH}/bin
-export GO15VENDOREXPERIMENT=1
 
-#
 go get golang.org/x/crypto/bcrypt
 go get github.com/jteeuwen/go-bindata
-go get github.com/G-Node/gin-doi/src
 go get github.com/G-Node/gin-dex/gindex
 go get github.com/G-Node/git-module
 go get gopkg.in/yaml.v2

+ 35 - 5
routes/doi.go

@@ -1,13 +1,17 @@
 package routes
 
 import (
-	"github.com/G-Node/gogs/pkg/context"
-	"github.com/G-Node/gogs/pkg/setting"
+	"crypto/aes"
+	"crypto/cipher"
+	"crypto/rand"
+	"encoding/base64"
 	"fmt"
+	"io"
 	"net/http"
-	"github.com/G-Node/gin-doi/src"
-	log "gopkg.in/clog.v1"
 
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/setting"
+	log "gopkg.in/clog.v1"
 )
 
 func RequestDoi(c *context.Context) {
@@ -16,7 +20,7 @@ func RequestDoi(c *context.Context) {
 		return
 	}
 	token := c.GetCookie(setting.SessionConfig.CookieName)
-	token, err := ginDoi.Encrypt([]byte(setting.Doi.DoiKey), token)
+	token, err := encrypt([]byte(setting.Doi.DoiKey), token)
 	if err != nil {
 		log.Error(0, "Could not encrypt Secret key:%s", err)
 		c.Status(http.StatusInternalServerError)
@@ -26,3 +30,29 @@ func RequestDoi(c *context.Context) {
 		c.User.Name, token)
 	c.Redirect(url)
 }
+
+// NOTE: TEMPORARY COPY FROM gin-doi
+
+// encrypt string to base64 crypto using AES
+func encrypt(key []byte, text string) (string, error) {
+	plaintext := []byte(text)
+
+	block, err := aes.NewCipher(key)
+	if err != nil {
+		return "", err
+	}
+
+	// The IV needs to be unique, but not secure. Therefore it's common to
+	// include it at the beginning of the ciphertext.
+	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
+	iv := ciphertext[:aes.BlockSize]
+	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
+		return "", err
+	}
+
+	stream := cipher.NewCFBEncrypter(block, iv)
+	stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
+
+	// convert to base64
+	return base64.URLEncoding.EncodeToString(ciphertext), nil
+}

+ 85 - 17
routes/repo/view.go

@@ -5,24 +5,24 @@
 package repo
 
 import (
+	"bufio"
 	"bytes"
+	"crypto/md5"
+	"encoding/hex"
+	"encoding/json"
+	"encoding/xml"
 	"fmt"
 	gotemplate "html/template"
+	"io"
 	"io/ioutil"
+	"net/http"
+	"os"
 	"path"
 	"strings"
 
-	"github.com/Unknwon/paginater"
-	log "gopkg.in/clog.v1"
-
 	"github.com/G-Node/git-module"
-
-	"bufio"
-	"io"
-	"os"
-
-	"github.com/G-Node/gin-doi/src"
 	"github.com/G-Node/go-annex"
+	"github.com/G-Node/godML/odml"
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/pkg/context"
 	"github.com/G-Node/gogs/pkg/markup"
@@ -30,12 +30,11 @@ import (
 	"github.com/G-Node/gogs/pkg/template"
 	"github.com/G-Node/gogs/pkg/template/highlight"
 	"github.com/G-Node/gogs/pkg/tool"
+	"github.com/Unknwon/paginater"
 	"github.com/go-macaron/captcha"
-	"gopkg.in/yaml.v2"
-	"github.com/G-Node/godML/odml"
-	"encoding/json"
-	"encoding/xml"
 	"golang.org/x/net/html/charset"
+	log "gopkg.in/clog.v1"
+	"gopkg.in/yaml.v2"
 )
 
 const (
@@ -82,7 +81,7 @@ func renderDirectory(c *context.Context, treeLink string) {
 				log.Trace("Doi Blob could not be read:%v", err)
 			}
 			buf, err := ioutil.ReadAll(doiData)
-			doiInfo := ginDoi.CBerry{}
+			doiInfo := DOIRegInfo{}
 			err = yaml.Unmarshal(buf, &doiInfo)
 			if err != nil {
 				log.Trace("Doi Blob could not be unmarshalled:%v", err)
@@ -92,7 +91,7 @@ func renderDirectory(c *context.Context, treeLink string) {
 			doi := GDoiRepo(c, setting.Doi.DoiBase)
 			//ddata, err := ginDoi.GDoiMData(doi, "https://api.datacite.org/works/") //todo configure URL?
 
-			c.Data["DoiReg"] = ginDoi.IsRegsitredDoi(doi)
+			c.Data["DoiReg"] = IsRegisteredDOI(doi)
 			c.Data["doi"] = doi
 
 		}
@@ -511,11 +510,80 @@ func Forks(c *context.Context) {
 
 // Get the (theoretical ) doi for a repository. Make sure its tge doi for the Base repo
 // in cas eits a repo from the doi user
-func GDoiRepo(c *context.Context, doiBAse string) string {
+func GDoiRepo(c *context.Context, doiBase string) string {
 	repoN := c.Repo.Repository.FullName()
 	// check whether this repo belongs to doi and is a fork
 	if c.Repo.Repository.IsFork && c.Repo.Owner.Name == "doi" {
 		repoN = c.Repo.Repository.BaseRepo.FullName()
 	}
-	return ginDoi.MakeDoi(ginDoi.RepoP2UUID(repoN), doiBAse)
+	uuid := repoPathToUUID(repoN)
+	return doiBase + uuid[:6]
+}
+
+// NOTE: TEMPORARY COPIES FROM gin-doi
+
+// uuidMap is a map between registered repositories and their UUIDs for datasets registered before the new UUID generation method was implemented.
+// This map is required because the current method of computing UUIDs differs from the older method and this lookup is used to handle the old-method UUIDs.
+var uuidMap = map[string]string{
+	"INT/multielectrode_grasp":                   "f83565d148510fede8a277f660e1a419",
+	"ajkumaraswamy/HB-PAC_disinhibitory_network": "1090f803258557299d287c4d44a541b2",
+	"steffi/Kleineidam_et_al_2017":               "f53069de4c4921a3cfa8f17d55ef98bb",
+	"Churan/Morris_et_al_Frontiers_2016":         "97bc1456d3f4bca2d945357b3ec92029",
+	"fabee/efish_locking":                        "6953bbf0087ba444b2d549b759de4a06",
+}
+
+// repoPathToUUID computes a UUID from a repository path.
+func repoPathToUUID(URI string) string {
+	if doi, ok := uuidMap[URI]; ok {
+		return doi
+	}
+	currMd5 := md5.Sum([]byte(URI))
+	return hex.EncodeToString(currMd5[:])
+}
+
+// DOIRegInfo holds all the metadata and information necessary for a DOI registration request.
+type DOIRegInfo struct {
+	Missing     []string
+	DOI         string
+	UUID        string
+	FileSize    int64
+	Title       string
+	Authors     []Author
+	Description string
+	Keywords    []string
+	References  []Reference
+	Funding     []string
+	License     *License
+	DType       string
+}
+
+type Author struct {
+	FirstName   string
+	LastName    string
+	Affiliation string
+	ID          string
+}
+
+type Reference struct {
+	Reftype string
+	Name    string
+	DOI     string
+}
+
+type License struct {
+	Name string
+	URL  string
+}
+
+func IsRegisteredDOI(doi string) bool {
+	url := fmt.Sprintf("https://doi.org/%s", doi)
+	resp, err := http.Get(url)
+	if err != nil {
+		log.Trace("Could not query for doi: %s at %s", doi, url)
+		return false
+	}
+	if resp.StatusCode != http.StatusNotFound {
+		return true
+	}
+	return false
 }

+ 15 - 6
routes/search.go

@@ -1,15 +1,16 @@
 package routes
 
 import (
-	"github.com/G-Node/gogs/pkg/context"
-	"net/http"
-	"github.com/G-Node/gin-dex/gindex"
-	"encoding/json"
 	"bytes"
-	"io/ioutil"
-	"github.com/G-Node/gogs/pkg/setting"
+	"encoding/json"
 	"fmt"
+	"io/ioutil"
+	"net/http"
 	"strconv"
+
+	"github.com/G-Node/gin-dex/gindex"
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/setting"
 	"github.com/Sirupsen/logrus"
 )
 
@@ -106,3 +107,11 @@ func ExploreCommits(c *context.Context) {
 	c.Data["Commits"] = res.Commits
 	c.HTML(200, EXPLORE_COMMITS)
 }
+
+type SearchRequest struct {
+	Token  string
+	CsrfT  string
+	UserID int64
+	Querry string
+	SType  int64
+}