132 lines
2.9 KiB
Go
132 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/Thomasorus/Ronbun-CMS/internal/models"
|
|
)
|
|
|
|
type ImportedPages struct {
|
|
Pages []ImportedPage
|
|
}
|
|
|
|
type ImportedPage struct {
|
|
Name string `json:"name"`
|
|
Id string `json:"id"`
|
|
Slug string `json:"slug"`
|
|
Host string `json:"host"`
|
|
HostSlug string `json:"hostSlug"`
|
|
Bref string `json:"bref"`
|
|
Priv string `json:"priv"`
|
|
Menu string `json:"menu"`
|
|
Feed string `json:"feed"`
|
|
Created string `json:"created"`
|
|
RawText string `json:"rawText"`
|
|
Content string `json:"content"`
|
|
Generated string `json:"generated"`
|
|
ChildPages []ImportedPage `json:"childPages"`
|
|
Category string `json:"category"`
|
|
BreadCrumb []BreadcrumbItem `json:"breadcrumb"`
|
|
}
|
|
|
|
type BreadcrumbItem struct {
|
|
Path string
|
|
Label string
|
|
}
|
|
|
|
func (app *application) ImportKaku(w http.ResponseWriter, r *http.Request) {
|
|
jsonFile, err := os.Open("export.json")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
defer jsonFile.Close()
|
|
|
|
byteValue, _ := io.ReadAll(jsonFile)
|
|
|
|
var pages ImportedPages
|
|
json.Unmarshal(byteValue, &pages.Pages)
|
|
// Create an instance of application (replace with actual initialization as needed)
|
|
app.ImportPagesTree(pages)
|
|
}
|
|
|
|
func (app *application) ImportPagesTree(pages ImportedPages) error {
|
|
flatPages := flattenTree(pages.Pages)
|
|
err := app.pages.BatchInsertPages(flatPages)
|
|
|
|
if err != nil {
|
|
print(err.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func flattenTree(pages []ImportedPage) []models.Page {
|
|
var result []models.Page
|
|
var flatten func([]ImportedPage, *int) // parent_id parameter
|
|
|
|
var i = 0
|
|
flatten = func(nodes []ImportedPage, parentID *int) {
|
|
for _, node := range nodes {
|
|
var id = i
|
|
var hostId = 0
|
|
if parentID != nil {
|
|
hostId = *parentID
|
|
}
|
|
page := models.Page{
|
|
// Map fields from ImportedPage to models.Page as appropriate
|
|
ID: id,
|
|
Name: node.Name,
|
|
Slug: node.Slug,
|
|
Host: hostId,
|
|
Category: node.Category,
|
|
Summary: node.Bref,
|
|
Body: node.RawText,
|
|
Created: convertTime(node.Created),
|
|
Updated: convertTime(node.Generated),
|
|
Private: parsePriv(node.Priv),
|
|
Menu: parseFeed(node.Menu),
|
|
Feed: parseFeed(node.Feed),
|
|
}
|
|
if page.Name == "Home" {
|
|
page.Slug = "/"
|
|
}
|
|
result = append(result, page)
|
|
i++
|
|
if len(node.ChildPages) > 0 {
|
|
flatten(node.ChildPages, &page.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
flatten(pages, nil)
|
|
return result
|
|
}
|
|
|
|
func convertTime(stringDate string) time.Time {
|
|
parsedTime, err := time.Parse("2006-01-02T15:04:05.000Z", stringDate)
|
|
if err != nil {
|
|
return time.Time{}
|
|
}
|
|
return parsedTime
|
|
}
|
|
|
|
func parsePriv(priv string) int {
|
|
if priv == "true" {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func parseFeed(priv string) bool {
|
|
b, err := strconv.ParseBool(priv)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return b
|
|
}
|