owl-blogs/cmd/owl/import_v1.go

119 lines
2.7 KiB
Go
Raw Normal View History

2023-07-09 19:27:59 +00:00
package main
import (
2023-07-09 20:12:06 +00:00
"fmt"
2023-07-13 18:39:24 +00:00
"os"
2023-07-09 20:12:06 +00:00
"owl-blogs/domain/model"
"owl-blogs/importer"
"owl-blogs/infra"
2023-07-13 18:39:24 +00:00
"path"
2023-07-09 20:12:06 +00:00
2023-07-09 19:27:59 +00:00
"github.com/spf13/cobra"
)
2023-07-09 20:12:06 +00:00
var userPath string
2023-07-09 19:27:59 +00:00
func init() {
rootCmd.AddCommand(importCmd)
2023-07-09 20:12:06 +00:00
importCmd.Flags().StringVarP(&userPath, "path", "p", "", "Path to the user folder")
2023-07-09 19:27:59 +00:00
importCmd.MarkFlagRequired("path")
}
var importCmd = &cobra.Command{
Use: "import",
Short: "Import data from v1",
Long: `Import data from v1`,
Run: func(cmd *cobra.Command, args []string) {
2023-07-09 20:12:06 +00:00
db := infra.NewSqliteDB(DbPath)
app := App(db)
posts, err := importer.AllUserPosts(userPath)
if err != nil {
panic(err)
}
for _, post := range posts {
2023-07-13 18:39:24 +00:00
existing, _ := app.EntryService.FindById(post.Id)
if existing != nil {
continue
}
2023-07-09 20:12:06 +00:00
fmt.Println(post.Meta.Type)
2023-07-13 18:39:24 +00:00
// import assets
mediaDir := path.Join(userPath, post.MediaDir())
println(mediaDir)
files := importer.ListDir(mediaDir)
for _, file := range files {
// mock entry to pass to binary service
entry := &model.Article{}
entry.SetID(post.Id)
fileData, err := os.ReadFile(path.Join(mediaDir, file))
if err != nil {
panic(err)
}
app.BinaryService.CreateEntryFile(file, fileData, entry)
}
2023-07-09 20:12:06 +00:00
switch post.Meta.Type {
case "article":
article := model.Article{}
article.SetID(post.Id)
2023-07-13 18:39:24 +00:00
article.SetPublishedAt(&post.Meta.Date)
article.SetMetaData(&model.ArticleMetaData{
2023-07-09 20:12:06 +00:00
Title: post.Meta.Title,
Content: post.Content,
})
app.EntryService.Create(&article)
case "bookmark":
case "reply":
case "photo":
2023-07-13 18:39:24 +00:00
photo := model.Image{}
photo.SetID(post.Id)
photo.SetPublishedAt(&post.Meta.Date)
photo.SetMetaData(&model.ImageMetaData{
Title: post.Meta.Title,
Content: post.Content,
ImageId: post.Meta.PhotoPath,
})
app.EntryService.Create(&photo)
2023-07-09 20:12:06 +00:00
case "note":
2023-07-13 18:39:24 +00:00
note := model.Note{}
note.SetID(post.Id)
note.SetPublishedAt(&post.Meta.Date)
note.SetMetaData(&model.NoteMetaData{
Content: post.Content,
})
app.EntryService.Create(&note)
2023-07-09 20:12:06 +00:00
case "recipe":
2023-07-13 18:39:24 +00:00
recipe := model.Recipe{}
recipe.SetID(post.Id)
recipe.SetPublishedAt(&post.Meta.Date)
recipe.SetMetaData(&model.RecipeMetaData{
Title: post.Meta.Title,
Yield: post.Meta.Recipe.Yield,
Duration: post.Meta.Recipe.Duration,
Ingredients: post.Meta.Recipe.Ingredients,
Content: post.Content,
})
app.EntryService.Create(&recipe)
2023-07-09 20:12:06 +00:00
case "page":
2023-07-13 18:39:24 +00:00
page := model.Page{}
page.SetID(post.Id)
page.SetPublishedAt(&post.Meta.Date)
page.SetMetaData(&model.PageMetaData{
Title: post.Meta.Title,
Content: post.Content,
})
app.EntryService.Create(&page)
2023-07-09 20:12:06 +00:00
default:
panic("Unknown type")
}
}
2023-07-09 19:27:59 +00:00
},
}