Skip to content

Commit

Permalink
feat: import from hexo jekyll hugo
Browse files Browse the repository at this point in the history
style: format code
  • Loading branch information
1379 authored and 1379 committed Dec 18, 2022
1 parent 4338ad7 commit f44f1fd
Show file tree
Hide file tree
Showing 45 changed files with 2,614 additions and 40 deletions.
7 changes: 6 additions & 1 deletion consts/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func (c *CommentStatus) Scan(src interface{}) error {
}
return nil
}

func CommentStatusFromString(str string) (CommentStatus, error) {
if str == "PUBLISHED" {
return CommentStatusPublished, nil
Expand Down Expand Up @@ -421,10 +422,14 @@ const (
type EditorType int32

const (
EditorTypeMarkdown = iota
EditorTypeMarkdown EditorType = iota
EditorTypeRichText
)

func (e EditorType) Ptr() *EditorType {
return &e
}

func (e *EditorType) Scan(src interface{}) error {
if src == nil {
return xerr.BadParam.New("").WithMsg("field nil")
Expand Down
1 change: 1 addition & 0 deletions dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func Transaction(ctx context.Context, fn func(txCtx context.Context) error) erro
return fn(txCtx)
})
}

func GetDB() *gorm.DB {
return DB
}
2 changes: 2 additions & 0 deletions event/listener/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func NewCommentListener(
bus.Subscribe(event.CommentNewEventName, c.HandleCommentNew)
bus.Subscribe(event.CommentReplyEventName, c.HandleCommentReply)
}

func (c *CommentListener) HandleCommentNew(ctx context.Context, ce event.Event) error {
newCommentNotice, err := c.OptionService.GetOrByDefaultWithErr(ctx, property.CommentNewNotice, property.CommentNewNotice.DefaultValue)
if err != nil {
Expand Down Expand Up @@ -142,6 +143,7 @@ func (c *CommentListener) HandleCommentNew(ctx context.Context, ce event.Event)
}
return c.EmailService.SendTemplateEmail(ctx, users[0].Email, subject, content.String())
}

func (c *CommentListener) HandleCommentReply(ctx context.Context, ce event.Event) error {
commentReplyNotice, err := c.OptionService.GetOrByDefaultWithErr(ctx, property.CommentReplyNotice, property.CommentNewNotice.DefaultValue)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion event/listener/post_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func NewPostUpdateListener(bus event.Bus,
}

func (p *PostUpdateListener) HandlePostUpdateEvent(ctx context.Context, postUpdateEvent event.Event) error {

postID := postUpdateEvent.(*event.PostUpdateEvent).PostID

categories, err := p.PostCategoryService.ListCategoryByPostID(ctx, postID)
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/aliyun/aliyun-oss-go-sdk v2.2.6+incompatible
github.com/clbanning/mxj/v2 v2.5.7
github.com/disintegration/imaging v1.6.2
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-contrib/cors v1.4.0
Expand All @@ -18,10 +19,14 @@ require (
github.com/minio/minio-go/v7 v7.0.45
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml/v2 v2.0.5
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.4.0
github.com/spf13/afero v1.9.2
github.com/spf13/cast v1.5.0
github.com/spf13/viper v1.14.0
github.com/yeqown/go-qrcode v1.5.10
github.com/yuin/goldmark v1.5.3
go.uber.org/dig v1.15.0
go.uber.org/fx v1.18.2
go.uber.org/zap v1.24.0
Expand Down Expand Up @@ -64,12 +69,9 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/clbanning/mxj/v2 v2.5.7 h1:7q5lvUpaPF/WOkqgIDiwjBJaznaLCCBd78pi8ZyAnE0=
github.com/clbanning/mxj/v2 v2.5.7/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -392,6 +394,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
Expand Down
5 changes: 5 additions & 0 deletions handler/admin/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package admin

import (
"net/http"
"path"
"path/filepath"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -116,6 +117,10 @@ func (b *BackupHandler) ImportMarkdown(ctx *gin.Context) (interface{}, error) {
if err != nil {
return nil, xerr.WithMsg(err, "上传文件错误").WithStatus(xerr.StatusBadRequest)
}
filenameExt := path.Ext(fileHeader.Filename)
if filenameExt != ".md" && filenameExt != ".markdown" && filenameExt != ".mdown" {
return nil, xerr.WithMsg(err, "Unsupported format").WithStatus(xerr.StatusBadRequest)
}
return nil, b.BackupService.ImportMarkdown(ctx, fileHeader)
}

Expand Down
1 change: 1 addition & 0 deletions handler/admin/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewEmailHandler(emailService service.EmailService) *EmailHandler {
EmailService: emailService,
}
}

func (e *EmailHandler) Test(ctx *gin.Context) (interface{}, error) {
p := &param.TestEmail{}
if err := ctx.ShouldBindJSON(p); err != nil {
Expand Down
1 change: 0 additions & 1 deletion handler/content/api/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (j *JournalHandler) GetJournal(ctx *gin.Context) (interface{}, error) {
}

func (j *JournalHandler) ListTopComment(ctx *gin.Context) (interface{}, error) {

journalID, err := util.ParamInt32(ctx, "journalID")
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions handler/content/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewArchiveHandler(
func (a *ArchiveHandler) Archives(ctx *gin.Context, model template.Model) (string, error) {
return a.PostModel.Archives(ctx, 0, model)
}

func (a *ArchiveHandler) ArchivesPage(ctx *gin.Context, model template.Model) (string, error) {
page, err := util.ParamInt32(ctx, "page")
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion handler/content/authentication/post_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (p *PostAuthentication) Authenticate(ctx context.Context, token string, id
}
}
return "", xerr.WithMsg(nil, "密码不正确").WithStatus(http.StatusUnauthorized)

}

func (p *PostAuthentication) IsAuthenticated(ctx context.Context, tokenStr string, id int32) (bool, error) {
Expand Down
1 change: 1 addition & 0 deletions handler/content/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewCategoryHandler(
func (c *CategoryHandler) Categories(ctx *gin.Context, model template.Model) (string, error) {
return c.CategoryModel.ListCategories(ctx, model)
}

func (c *CategoryHandler) CategoryDetail(ctx *gin.Context, model template.Model) (string, error) {
slug, err := util.ParamString(ctx, "slug")
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion handler/content/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func NewJournalHandler(
optionService service.OptionService,
journalService service.JournalService,
journalModel *model.JournalModel,

) *JournalHandler {
return &JournalHandler{
OptionService: optionService,
Expand Down
1 change: 0 additions & 1 deletion handler/content/model/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type CategoryModel struct {
}

func (c *CategoryModel) ListCategories(ctx context.Context, model template.Model) (string, error) {

seoKeyWords := c.OptionService.GetOrByDefault(ctx, property.SeoKeywords)
seoDescription := c.OptionService.GetOrByDefault(ctx, property.SeoDescription)

Expand Down
1 change: 0 additions & 1 deletion handler/content/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func NewPhotoHandler(
optionService service.OptionService,
photoService service.PhotoService,
photoModel *model.PhotoModel,

) *PhotoHandler {
return &PhotoHandler{
OptionService: optionService,
Expand Down
1 change: 1 addition & 0 deletions handler/content/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewSearchHandler(
ThemeService: themeService,
}
}

func (s *SearchHandler) Search(ctx *gin.Context, model template.Model) (string, error) {
return s.search(ctx, 0, model)
}
Expand Down
1 change: 1 addition & 0 deletions handler/content/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (v *ViewHandler) authenticateCategory(ctx *gin.Context, slug, password, tok
ctx.Redirect(http.StatusFound, categoryDTO.FullPath)
return token, nil
}

func (v *ViewHandler) authenticatePost(ctx *gin.Context, slug, password, token string) (string, error) {
post, err := v.PostService.GetBySlug(ctx, slug)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions handler/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *Server) RegisterRouters() {
backupRouter.DELETE("/data", s.wrapHandler(s.BackupHandler.DeleteDataFile))
backupRouter.GET("/data/*path", s.BackupHandler.HandleData)
backupRouter.POST("/markdown/export", s.wrapHandler(s.BackupHandler.ExportMarkdown))
backupRouter.POST("/mark-down/import", s.wrapHandler(s.BackupHandler.ImportMarkdown))
backupRouter.POST("/markdown/import", s.wrapHandler(s.BackupHandler.ImportMarkdown))
backupRouter.GET("/markdown/fetch", s.wrapHandler(s.BackupHandler.GetMarkDownBackup))
backupRouter.GET("/markdown/export", s.wrapHandler(s.BackupHandler.ListMarkdowns))
backupRouter.DELETE("/markdown/export", s.wrapHandler(s.BackupHandler.DeleteMarkdowns))
Expand Down Expand Up @@ -345,7 +345,6 @@ func (s *Server) RegisterRouters() {
contentAPIRouter.GET("/options/comment", s.wrapHandler(s.ContentAPIOptionHander.Comment))
}
}

}

func (s *Server) registerDynamicRouters(contentRouter *gin.RouterGroup) error {
Expand Down
1 change: 0 additions & 1 deletion model/entity/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func (m *Post) BeforeCreate(tx *gorm.DB) (err error) {
if m.CreateTime == (time.Time{}) {
m.CreateTime = time.Now()
}
m.CreateTime = time.Now()
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions model/param/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Post struct {
CategoryIDs []int32 `json:"categoryIds" form:"categoryIds"`
MetaParam []Meta `json:"metas" form:"metas"`
Content string `json:"content" form:"content"`
EditTime *int64 `json:"editTime" form:"editTime"`
UpdateTime *int64 `json:"updateTime" form:"updateTime"`
}

type PostContent struct {
Expand Down
2 changes: 2 additions & 0 deletions model/property/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ var AliOssSource = Property{
KeyValue: "oss_ali_source",
Kind: reflect.String,
}

var AliOssThumbnailStyleRule = Property{
DefaultValue: "",
KeyValue: "oss_ali_thumbnail_style_rule",
Kind: reflect.String,
}

var AliOssStyleRule = Property{
DefaultValue: "",
KeyValue: "oss_ali_style_rule",
Expand Down
1 change: 1 addition & 0 deletions service/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type CategoryService interface {
GetByID(ctx context.Context, id int32) (*entity.Category, error)
GetBySlug(ctx context.Context, slug string) (*entity.Category, error)
GetByName(ctx context.Context, name string) (*entity.Category, error)
ListCategoryWithPostCountDTO(ctx context.Context, sort *param.Sort) ([]*dto.CategoryWithPostCount, error)
ListAll(ctx context.Context, sort *param.Sort) ([]*entity.Category, error)
ConvertToCategoryDTO(ctx context.Context, e *entity.Category) (*dto.CategoryDTO, error)
Expand Down
13 changes: 13 additions & 0 deletions service/export_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package service

import (
"context"
"io"

"github.com/go-sonic/sonic/model/entity"
)

type ExportImport interface {
CreateByMarkdown(ctx context.Context, filename string, reader io.Reader) (*entity.Post, error)
ExportMarkdown(ctx context.Context, needFrontMatter bool) (string, error)
}
1 change: 1 addition & 0 deletions service/file_storage/file_storage_impl/file_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (f *localFileDescriptor) getExtensionName() string {
func (f *localFileDescriptor) getFileName() string {
return f.Name
}

func (f *localFileDescriptor) getShouldRename() shouldRename {
return f.ShouldRename
}
Expand Down
4 changes: 4 additions & 0 deletions service/file_storage/file_storage_impl/url_file_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (f *urlFileDescriptor) getFileName() string {
func (f *urlFileDescriptor) setFileName(name string) {
f.Name = name
}

func (f *urlFileDescriptor) getShouldRename() shouldRename {
return f.ShouldRename
}
Expand All @@ -96,16 +97,19 @@ func withBaseURL(baseURL string) urlOption {
f.BasePath = baseURL
}
}

func withSubURLPath(subURL string) urlOption {
return func(f *urlFileDescriptor) {
f.SubPath = subURL
}
}

func withOriginalNameURLOption(originalName string) urlOption {
return func(f *urlFileDescriptor) {
f.OriginalName = originalName
}
}

func withShouldRenameURLOption(fn func(relativePath string) (bool, error)) urlOption {
return func(f *urlFileDescriptor) {
f.ShouldRename = fn
Expand Down
1 change: 0 additions & 1 deletion service/impl/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (a *attachmentServiceImpl) GetAttachment(ctx context.Context, attachmentID
}

func (a *attachmentServiceImpl) Upload(ctx context.Context, fileHeader *multipart.FileHeader) (attachmentDTO *dto.AttachmentDTO, err error) {

attachmentType := a.OptionService.GetAttachmentType(ctx)

fileStorage := a.FileStorageComposite.GetFileStorage(attachmentType)
Expand Down
23 changes: 10 additions & 13 deletions service/impl/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package impl
import (
"context"
"encoding/json"
"io"
"io/fs"
"mime/multipart"
"os"
Expand All @@ -15,7 +14,6 @@ import (
"github.com/go-sonic/sonic/config"
"github.com/go-sonic/sonic/consts"
"github.com/go-sonic/sonic/dal"
"github.com/go-sonic/sonic/log"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/util"
Expand All @@ -26,13 +24,15 @@ type backupServiceImpl struct {
Config *config.Config
OptionService service.OptionService
OneTimeTokenService service.OneTimeTokenService
ExportImportService service.ExportImport
}

func NewBackUpService(config *config.Config, optionService service.OptionService, oneTimeTokenService service.OneTimeTokenService) service.BackupService {
func NewBackUpService(config *config.Config, optionService service.OptionService, oneTimeTokenService service.OneTimeTokenService, exportImportService service.ExportImport) service.BackupService {
return &backupServiceImpl{
Config: config,
OptionService: optionService,
OneTimeTokenService: oneTimeTokenService,
ExportImportService: exportImportService,
}
}

Expand Down Expand Up @@ -144,14 +144,8 @@ func (b *backupServiceImpl) ImportMarkdown(ctx context.Context, fileHeader *mult
if err != nil {
return xerr.NoType.Wrap(err).WithMsg("upload file error")
}
bContent, err := io.ReadAll(file)
if err != nil {
return xerr.NoType.Wrap(err).WithMsg("read file error")
}
content := string(bContent)
log.Info(content)
// TODO 导入markdown
return nil
_, err = b.ExportImportService.CreateByMarkdown(ctx, fileHeader.Filename, file)
return err
}

func (b *backupServiceImpl) ExportData(ctx context.Context) (*dto.BackupDTO, error) {
Expand Down Expand Up @@ -208,8 +202,11 @@ func (b *backupServiceImpl) ExportData(ctx context.Context) (*dto.BackupDTO, err
}

func (b *backupServiceImpl) ExportMarkdown(ctx context.Context, needFrontMatter bool) (*dto.BackupDTO, error) {
// TODO
return nil, nil
fileName, err := b.ExportImportService.ExportMarkdown(ctx, needFrontMatter)
if err != nil {
return nil, err
}
return b.buildBackupDTO(ctx, string(service.Markdown), fileName)
}

func (b *backupServiceImpl) buildBackupDTO(ctx context.Context, baseBackupURL string, backupFilePath string) (*dto.BackupDTO, error) {
Expand Down
Loading

0 comments on commit f44f1fd

Please sign in to comment.