diff options
Diffstat (limited to 'src/cmd/export')
-rw-r--r-- | src/cmd/export/assets.go | 129 | ||||
-rw-r--r-- | src/cmd/export/main.go | 15 | ||||
-rw-r--r-- | src/cmd/export/posts.go | 15 |
3 files changed, 153 insertions, 6 deletions
diff --git a/src/cmd/export/assets.go b/src/cmd/export/assets.go new file mode 100644 index 0000000..2e62cbd --- /dev/null +++ b/src/cmd/export/assets.go @@ -0,0 +1,129 @@ +package main + +import ( + "bytes" + "compress/gzip" + "context" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "strings" + + "dev.mediocregopher.com/mediocre-blog.git/src/post/asset" + "dev.mediocregopher.com/mediocre-go-lib.git/mctx" + "dev.mediocregopher.com/mediocre-go-lib.git/mlog" + "github.com/nlepage/go-tarfs" +) + +func writeArchiveAsset( + assetStore asset.Store, + assetsDirPath string, + id string, +) error { + buf := new(bytes.Buffer) + if err := assetStore.Get(id, buf); err != nil { + return fmt.Errorf("loading into buffer: %w", err) + } + + gzipR, err := gzip.NewReader(buf) + if err != nil { + return fmt.Errorf("decompressing as gzip: %w", err) + } + + tarFS, err := tarfs.New(gzipR) + if err != nil { + return fmt.Errorf("parsing as tar: %w", err) + } + + return fs.WalkDir(tarFS, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("walking path %q: %w", path, err) + } else if d.IsDir() { + return nil + } + + var ( + dirPath = filepath.Join(assetsDirPath, id, filepath.Dir(path)) + dstPath = filepath.Join(dirPath, d.Name()) + srcPath = path + ) + + if err := os.MkdirAll(dirPath, 0755); err != nil { + return fmt.Errorf("creating directory %q: %w", dirPath, err) + } + + dstF, err := os.Create(dstPath) + if err != nil { + return fmt.Errorf("opening dst path %q: %w", dstPath, err) + } + defer dstF.Close() + + srcF, err := tarFS.Open(srcPath) + if err != nil { + return fmt.Errorf("opening path %q within the tar: %w", srcPath, err) + } + defer srcF.Close() + + if _, err = io.Copy(dstF, srcF); err != nil { + return fmt.Errorf("copying %q into %q: %w", srcPath, dstPath, err) + } + + return nil + }) +} + +func writeAsset( + assetStore asset.Store, + assetsDirPath string, + id string, +) error { + if strings.HasSuffix(id, ".tgz") { + return writeArchiveAsset(assetStore, assetsDirPath, id) + } + + assetPath := filepath.Join(assetsDirPath, id) + + f, err := os.Create(assetPath) + if err != nil { + return fmt.Errorf("creating file %q: %w", assetPath, err) + } + defer func() { _ = f.Close() }() + + if err := assetStore.Get(id, f); err != nil { + return fmt.Errorf("writing asset to %q: %w", assetPath, err) + } + + return nil +} + +func exportAssets( + ctx context.Context, + logger *mlog.Logger, + assetStore asset.Store, + exportDirPath string, +) error { + var ( + assetsDirPath = filepath.Join(exportDirPath, "assets") + ) + + if err := os.MkdirAll(assetsDirPath, 0755); err != nil { + return fmt.Errorf("creating asset dir %q: %w", assetsDirPath, err) + } + + logger.Info(ctx, "Listing assets") + assets, err := assetStore.List() + if err != nil { + return fmt.Errorf("listing assets: %w", err) + } + + for _, id := range assets { + logger.Info(mctx.Annotate(ctx, "assetID", id), "Writing asset") + if err := writeAsset(assetStore, assetsDirPath, id); err != nil { + return fmt.Errorf("writing asset %q: %w", id, err) + } + } + + return nil +} diff --git a/src/cmd/export/main.go b/src/cmd/export/main.go index 1d39bc7..23fa643 100644 --- a/src/cmd/export/main.go +++ b/src/cmd/export/main.go @@ -7,6 +7,7 @@ import ( "dev.mediocregopher.com/mediocre-blog.git/src/gmi" "dev.mediocregopher.com/mediocre-blog.git/src/http" "dev.mediocregopher.com/mediocre-blog.git/src/post" + "dev.mediocregopher.com/mediocre-blog.git/src/post/asset" "dev.mediocregopher.com/mediocre-blog.git/src/render" "dev.mediocregopher.com/mediocre-go-lib.git/mlog" ) @@ -57,8 +58,8 @@ func main() { httpParams.PublicURL, gmiParams.PublicURL, ) - postStore = post.NewStore(postSQLDB) - //postAssetStore = asset.NewStore(postSQLDB) + postStore = post.NewStore(postSQLDB) + postAssetStore = asset.NewStore(postSQLDB) //postDraftStore = post.NewDraftStore(postSQLDB) ) @@ -72,4 +73,14 @@ func main() { if err != nil { logger.Fatal(ctx, "Failed to export post data", err) } + + err = exportAssets( + ctx, + logger.WithNamespace("assets"), + postAssetStore, + *exportDirPath, + ) + if err != nil { + logger.Fatal(ctx, "Failed to export asset data", err) + } } diff --git a/src/cmd/export/posts.go b/src/cmd/export/posts.go index f54fc2c..569ff6b 100644 --- a/src/cmd/export/posts.go +++ b/src/cmd/export/posts.go @@ -31,7 +31,7 @@ func postTargetFormat(p post.StoredPost) post.Format { return post.FormatGemtext } -func writePostBody(post post.StoredPost, path, body string) error { +func writePostBody(p post.StoredPost, path, body string) error { f, err := os.Create(path) if err != nil { return fmt.Errorf("opening file: %w", err) @@ -45,11 +45,18 @@ func writePostBody(post post.StoredPost, path, body string) error { _, err = f.WriteString(str) } - writeString(fmt.Sprintf("# %s\n\n", post.Title)) - if post.Description != "" { - writeString(fmt.Sprintf("> %s\n\n", post.Description)) + if f := postTargetFormat(p); f == post.FormatGemtext { + writeString("=> ../posts/ Back to All Posts\n\n") + } else if f == post.FormatMarkdown { + writeString(fmt.Sprintf("---\nTitle: %q\n---\n", p.Title)) + writeString("[Back to All Posts](../posts/)\n\n") + } + writeString(fmt.Sprintf("# %s\n\n", p.Title)) + if p.Description != "" { + writeString(fmt.Sprintf("> %s\n\n", p.Description)) } writeString(body) + writeString(fmt.Sprintf("\n-----\n\nPublished %s\n", p.PublishedAt.Format("2006-01-02"))) return err } |