-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
551 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hugo/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.