Created
December 27, 2023 02:58
-
-
Save kazu634/e7e51c8dad92d09335d457567db4a1de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--- | |
title: "{{.Title}}" | |
date: {{.Date}} | |
summary: "{{.Summary}}" | |
topics: "{{.Topic}}" | |
authors: | |
- "{{.Author}}" | |
--- | |
{{.Body}} | |
This file contains hidden or 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
package activities | |
import ( | |
"embed" | |
"fmt" | |
"io" | |
"net/http" | |
"path" | |
"path/filepath" | |
"text/template" | |
"gitea.kazu634.com/kazu634/microcms/internal/mc" | |
) | |
//go:embed templates/* | |
var files embed.FS | |
type Activity struct { | |
ID string | |
Date string | |
Title string | |
Summary string | |
Topic string | |
Author string | |
Body string | |
EyeCatchURL string | |
} | |
func (a Activity) Upload() error { | |
err := a.uploadPost() | |
if err != nil { | |
return err | |
} | |
err = a.uploadEyecatch() | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func (a Activity) uploadPost() error { | |
// minioクライアントの作成 | |
mc, err := mc.Setup() | |
if err != nil { | |
return err | |
} | |
// Pipeの作成 | |
ar, aw := io.Pipe() | |
// 活動の記録を生成 | |
go func() { | |
err := a.GenActivity(aw) | |
if err != nil { | |
panic(err) | |
} | |
defer aw.Close() | |
}() | |
// 活動の記録をアップロード | |
err = mc.Upload(ar, "AppData", fmt.Sprintf("everun/contents/%s-%s/index.md", a.Date, a.ID)) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func (a Activity) getExtention() string { | |
_, file := path.Split(a.EyeCatchURL) | |
return filepath.Ext(file) | |
} | |
func (a Activity) uploadEyecatch() error { | |
if a.EyeCatchURL == "" { | |
return nil | |
} | |
// minioクライアントの作成 | |
mc, err := mc.Setup() | |
if err != nil { | |
return err | |
} | |
// Pipeの作成 | |
er, ew := io.Pipe() | |
// 活動の記録を生成 | |
go func() { | |
err := a.DownloadEyecatch(ew) | |
if err != nil { | |
panic(err) | |
} | |
defer ew.Close() | |
}() | |
// 活動の記録をアップロード | |
err = mc.Upload(er, "AppData", fmt.Sprintf("everun/contents/%s-%s/featured%s", a.Date, a.ID, a.getExtention())) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func (a Activity) GenActivity(target io.Writer) error { | |
t, err := template.New("activity.tmpl").ParseFS(files, "templates/activity.tmpl") | |
if err != nil { | |
return err | |
} | |
if err := t.Execute(target, a); err != nil { | |
return err | |
} | |
return nil | |
} | |
func (a Activity) DownloadEyecatch(target io.Writer) error { | |
resp, err := http.Get(a.EyeCatchURL) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
_, err = io.Copy(target, resp.Body) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment