Skip to content

Commit

Permalink
✨ うごく
Browse files Browse the repository at this point in the history
  • Loading branch information
x8xx committed Dec 5, 2020
1 parent b636775 commit 7ab802d
Show file tree
Hide file tree
Showing 19 changed files with 551 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hugo/
8 changes: 8 additions & 0 deletions domain/article.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package domain

type Article struct {
Id string
Title string
Body string
Tags []string
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/kindai-csg/d-blog-engine

go 1.15

require github.com/gin-gonic/gin v1.6.3
require (
github.com/gin-gonic/gin v1.6.3
github.com/gomarkdown/markdown v0.0.0-20201113031856-722100d81a8e
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/gomarkdown/markdown v0.0.0-20201113031856-722100d81a8e h1:/Y3B7hM9H3TOWPhe8eWGBGS4r09pjvS5Z0uoPADyjmU=
github.com/gomarkdown/markdown v0.0.0-20201113031856-722100d81a8e/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
Expand All @@ -32,11 +34,25 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc h1:LMEBgNcZUqXaP7evD1PZcL6EcDVa2QOFuI+cqM3+AJM=
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc/go.mod h1:N8UOSI6/c2yOpa/XDz3KVUiegocTziPiqNkeNTMiG1k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
94 changes: 94 additions & 0 deletions infrastructure/growi_api_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package infrastructure

import "time"

// auto generate https://mholt.github.io/json-to-go/
type GrowiPageGetResponse struct {
Page struct {
Status string `json:"status"`
Grant int `json:"grant"`
GrantedUsers []interface{} `json:"grantedUsers"`
Liker []interface{} `json:"liker"`
SeenUsers []string `json:"seenUsers"`
CommentCount int `json:"commentCount"`
PageID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Path string `json:"path"`
Creator struct {
IsGravatarEnabled bool `json:"isGravatarEnabled"`
IsEmailPublished bool `json:"isEmailPublished"`
Lang string `json:"lang"`
Status int `json:"status"`
Admin bool `json:"admin"`
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
LastLoginAt time.Time `json:"lastLoginAt"`
ImageAttachment struct {
CreaterImage_ID string `json:"_id"`
FilePathProxied string `json:"filePathProxied"`
DownloadPathProxied string `json:"downloadPathProxied"`
CreaterImageID string `json:"id"`
} `json:"imageAttachment"`
} `json:"creator"`
LastUpdateUser struct {
IsGravatarEnabled bool `json:"isGravatarEnabled"`
IsEmailPublished bool `json:"isEmailPublished"`
Lang string `json:"lang"`
Status int `json:"status"`
Admin bool `json:"admin"`
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
LastLoginAt time.Time `json:"lastLoginAt"`
ImageAttachment struct {
LastUpdateImage_ID string `json:"_id"`
FilePathProxied string `json:"filePathProxied"`
DownloadPathProxied string `json:"downloadPathProxied"`
LastUpdateImageID string `json:"id"`
} `json:"imageAttachment"`
} `json:"lastUpdateUser"`
RedirectTo interface{} `json:"redirectTo"`
GrantedGroup interface{} `json:"grantedGroup"`
V int `json:"__v"`
Revision struct {
Format string `json:"format"`
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
Path string `json:"path"`
Body string `json:"body"`
Author struct {
IsGravatarEnabled bool `json:"isGravatarEnabled"`
IsEmailPublished bool `json:"isEmailPublished"`
Lang string `json:"lang"`
Status int `json:"status"`
Admin bool `json:"admin"`
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
LastLoginAt time.Time `json:"lastLoginAt"`
ImageAttachment struct {
RevisionImage_ID string `json:"_id"`
FilePathProxied string `json:"filePathProxied"`
DownloadPathProxied string `json:"downloadPathProxied"`
RevisionImageID string `json:"id"`
} `json:"imageAttachment"`
} `json:"author"`
V int `json:"__v"`
} `json:"revision"`
ID string `json:"id"`
} `json:"page"`
Ok bool `json:"ok"`
}

type GrowiGetPageTagResponse struct {
Tags []string `json:"tags"`
Ok bool `json:"ok"`
}
181 changes: 145 additions & 36 deletions infrastructure/growi_handler.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,156 @@
package infrastructure

import (
"fmt"
"net/http"
"crypto/tls"
"io/ioutil"
"crypto/tls"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"fmt"

"github.com/kindai-csg/d-blog-engine/domain"
"gopkg.in/xmlpath.v2"
)


type GrowiHandler struct {
endpoint string
token string
endpoint string
token string
userId string
password string
}

func NewGrowiHandler(endpoint string, token string) *GrowiHandler {
return &GrowiHandler {
endpoint,
token,
}
func NewGrowiHandler(endpoint string, token string, userId string, password string) *GrowiHandler {
return &GrowiHandler{
endpoint,
token,
userId,
password,
}
}

func (handler *GrowiHandler) GetPage(path string) (string, error) {
url := handler.endpoint + "pages.get"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer " + handler.token)

params := req.URL.Query();
params.Add("access_token", handler.token)
params.Add("path", path)
req.URL.RawQuery = params.Encode()

fmt.Println(req.URL.String())
client := &http.Client{
Transport: &http.Transport {
TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },
},
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
fmt.Println(resp.StatusCode)
byteArray, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(byteArray))
return "", nil
func (handler *GrowiHandler) GetPage(path string) (domain.Article, error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

// get page body
url := handler.endpoint + "/_api/pages.get"
req, _ := http.NewRequest("GET", url, nil)

params := req.URL.Query()
params.Add("access_token", handler.token)
params.Add("path", path)
req.URL.RawQuery = params.Encode()

resp, err := client.Do(req)
if err != nil {
return domain.Article{}, err
}
defer resp.Body.Close()

byteArray, _ := ioutil.ReadAll(resp.Body)
jsonBytes := ([]byte)(byteArray)
pageData := new(GrowiPageGetResponse)
if err := json.Unmarshal(jsonBytes, pageData); err != nil {
return domain.Article{}, err
}

article := domain.Article {
Id: pageData.Page.PageID,
Body:pageData.Page.Revision.Body,
}

// get page tags
url = handler.endpoint + "/_api/pages.getPageTag"
req, _ = http.NewRequest("GET", url, nil)

params = req.URL.Query()
params.Add("access_token", handler.token)
params.Add("pageId", article.Id)
req.URL.RawQuery = params.Encode()

resp, err = client.Do(req)
if err != nil {
return domain.Article{}, err
}

byteArray, _ = ioutil.ReadAll(resp.Body)
jsonBytes = ([]byte)(byteArray)
tagData := new(GrowiGetPageTagResponse)
if err := json.Unmarshal(jsonBytes, tagData); err != nil {
return domain.Article{}, err
}
article.Tags = tagData.Tags
fmt.Println("testtags")
fmt.Println(article.Tags)

defer resp.Body.Close()
return article, nil
}

func (handler *GrowiHandler) DownloadFile(paths []string, downloadDir string) error {
// generate client & cookie
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
client := &http.Client{
Jar: jar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

// get csrf from login page
req, _ := http.NewRequest("GET", handler.endpoint + "/login", nil)
resp, err := client.Do(req)
csrfPath := xmlpath.MustCompile(`/html/body/@data-csrftoken`)
root, err := xmlpath.ParseHTML(resp.Body)
if err != nil {
return err
}
iter := csrfPath.Iter(root)
if !iter.Next() {
return errors.New("get csrf")
}
csrf := iter.Node().String()
resp.Body.Close()

// post login
loginData := url.Values{}
loginData.Add("loginForm[username]", handler.userId)
loginData.Add("loginForm[password]", handler.password)
loginData.Add("_csrf", csrf)
req, _ = http.NewRequest("POST", handler.endpoint + "/login", strings.NewReader(loginData.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err = client.Do(req)
if err != nil {
return err
}

// download file
for _, path := range paths {
req, _ = http.NewRequest("GET", handler.endpoint + "/download/" + path, nil)
resp, err = client.Do(req)
if err != nil {
return err
}

file, err := os.OpenFile(downloadDir + path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
io.Copy(file, resp.Body)
file.Close()
}

return nil
}
Loading

0 comments on commit 7ab802d

Please sign in to comment.