Optionally download attachments
This commit is contained in:
parent
d291d281a2
commit
5e2c21dd27
99
main.go
99
main.go
@ -29,6 +29,7 @@ type Arguments struct {
|
|||||||
Spacekey string `arg:"required" help:"Spacekey to export (required)"`
|
Spacekey string `arg:"required" help:"Spacekey to export (required)"`
|
||||||
User string `arg:"required" help:"User used for authentication (required)"`
|
User string `arg:"required" help:"User used for authentication (required)"`
|
||||||
Token string `arg:"required" help:"Token used for authentication (required)"`
|
Token string `arg:"required" help:"Token used for authentication (required)"`
|
||||||
|
WithAttachments bool `arg:"-a,--attachments" help:"Download attachments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) writeIndex(w io.Writer) {
|
func (n *Node) writeIndex(w io.Writer) {
|
||||||
@ -38,7 +39,7 @@ func (n *Node) writeIndex(w io.Writer) {
|
|||||||
<ul>
|
<ul>
|
||||||
{{ range $index, $element := .Children}}
|
{{ range $index, $element := .Children}}
|
||||||
<li>
|
<li>
|
||||||
<a href="pages/{{$element.Data.Id}}.html">{{ $element.Data.Title }}</a>
|
<a href="content/{{$element.Data.Id}}.html">{{ $element.Data.Title }}</a>
|
||||||
{{ template "tree" $element }}
|
{{ template "tree" $element }}
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
@ -91,7 +92,7 @@ func (n *Node) addChild(child *Node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeHTML(n *Node) {
|
func writeHTML(n *Node) {
|
||||||
os.Chdir("pages")
|
os.Chdir("content")
|
||||||
f, err := os.Create(n.Data.Id + ".html")
|
f, err := os.Create(n.Data.Id + ".html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error creating file for ID %v", n.Data.Id)
|
log.Fatalf("Error creating file for ID %v", n.Data.Id)
|
||||||
@ -247,6 +248,55 @@ type contentResult struct {
|
|||||||
} `json:"_links"`
|
} `json:"_links"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type attachmentResult struct {
|
||||||
|
Results []struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
MacroRenderedOutput struct {
|
||||||
|
} `json:"macroRenderedOutput"`
|
||||||
|
Metadata struct {
|
||||||
|
MediaType string `json:"mediaType"`
|
||||||
|
} `json:"metadata"`
|
||||||
|
Extensions struct {
|
||||||
|
MediaType string `json:"mediaType"`
|
||||||
|
FileSize int `json:"fileSize"`
|
||||||
|
Comment string `json:"comment"`
|
||||||
|
MediaTypeDescription string `json:"mediaTypeDescription"`
|
||||||
|
FileID string `json:"fileId"`
|
||||||
|
CollectionName string `json:"collectionName"`
|
||||||
|
} `json:"extensions"`
|
||||||
|
Expandable struct {
|
||||||
|
ChildTypes string `json:"childTypes"`
|
||||||
|
Container string `json:"container"`
|
||||||
|
Operations string `json:"operations"`
|
||||||
|
SchedulePublishDate string `json:"schedulePublishDate"`
|
||||||
|
Children string `json:"children"`
|
||||||
|
Restrictions string `json:"restrictions"`
|
||||||
|
History string `json:"history"`
|
||||||
|
Ancestors string `json:"ancestors"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
Descendants string `json:"descendants"`
|
||||||
|
Space string `json:"space"`
|
||||||
|
} `json:"_expandable"`
|
||||||
|
Links struct {
|
||||||
|
Webui string `json:"webui"`
|
||||||
|
Self string `json:"self"`
|
||||||
|
Download string `json:"download"`
|
||||||
|
} `json:"_links"`
|
||||||
|
} `json:"results"`
|
||||||
|
Start int `json:"start"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
Size int `json:"size"`
|
||||||
|
Links struct {
|
||||||
|
Base string `json:"base"`
|
||||||
|
Context string `json:"context"`
|
||||||
|
Self string `json:"self"`
|
||||||
|
} `json:"_links"`
|
||||||
|
}
|
||||||
|
|
||||||
func getRequest(args Arguments, url string) ([]byte, error) {
|
func getRequest(args Arguments, url string) ([]byte, error) {
|
||||||
b64Auth := base64.StdEncoding.EncodeToString([]byte(args.User + ":" + args.Token))
|
b64Auth := base64.StdEncoding.EncodeToString([]byte(args.User + ":" + args.Token))
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@ -294,6 +344,42 @@ func getContentFromID(args Arguments, id string) contentResult {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func downloadAttachmentsForPage(args Arguments, page *Node) {
|
||||||
|
url := args.Baseurl + "/wiki/rest/api/content/" + page.Data.Id + "/child/attachment"
|
||||||
|
body, err := getRequest(args, url)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var result attachmentResult
|
||||||
|
json.Unmarshal(body, &result)
|
||||||
|
for _, att := range result.Results {
|
||||||
|
filename := att.Title
|
||||||
|
url := args.Baseurl + "/wiki/" + att.Links.Download
|
||||||
|
out, err := os.Create(filename)
|
||||||
|
log.Printf("Writing attachment: %v", filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create file: %v", err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
body, err := getRequest(args, url)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to download file: %v", err)
|
||||||
|
}
|
||||||
|
_, err = out.Write(body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO polish html, replace img attachment links, table styling
|
||||||
|
// in html müssen die href zu den files geändert werden
|
||||||
|
// href order img class
|
||||||
|
// data-linked-resource-type="attachment"
|
||||||
|
// data-linked-resource-default-alias="image-20210609-123740.png"
|
||||||
|
// example with image: ROBODEVOPS/pages/3036938774.html
|
||||||
|
// example with href: ROBODEVOPS/pages/2165571587.html
|
||||||
|
}
|
||||||
|
|
||||||
func createPageTree(s *spaceResult) *Node {
|
func createPageTree(s *spaceResult) *Node {
|
||||||
var parent *Node
|
var parent *Node
|
||||||
root := Node{Data: NodeData{Title: "root"}}
|
root := Node{Data: NodeData{Title: "root"}}
|
||||||
@ -332,9 +418,9 @@ func main() {
|
|||||||
root := createPageTree(&result)
|
root := createPageTree(&result)
|
||||||
|
|
||||||
fname := "index.html"
|
fname := "index.html"
|
||||||
os.Mkdir("content", 0744)
|
os.Mkdir(args.Spacekey, 0744)
|
||||||
os.Mkdir("content/pages", 0744)
|
os.Mkdir(args.Spacekey+"/content", 0744)
|
||||||
os.Chdir("content")
|
os.Chdir(args.Spacekey)
|
||||||
f, err := os.Create(fname)
|
f, err := os.Create(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open %v", fname)
|
log.Fatalf("Failed to open %v", fname)
|
||||||
@ -347,5 +433,8 @@ func main() {
|
|||||||
content := getContentFromID(args, page.Data.Id)
|
content := getContentFromID(args, page.Data.Id)
|
||||||
page.Data.Body = content.Body.View.Value
|
page.Data.Body = content.Body.View.Value
|
||||||
writeHTML(page)
|
writeHTML(page)
|
||||||
|
if args.WithAttachments {
|
||||||
|
downloadAttachmentsForPage(args, page)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user