Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat(artrans): Run on frontend client.
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Oct 28, 2021
1 parent 364a251 commit fbce496
Show file tree
Hide file tree
Showing 18 changed files with 393 additions and 313 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"akismet",
"artalk",
"artran",
"Artrans",
"artrans",
"artransfer",
"gomail",
"Goreleaser",
"gorm",
Expand Down
15 changes: 2 additions & 13 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/ArtalkJS/ArtalkGo/lib"
"github.com/ArtalkJS/ArtalkGo/model"
"github.com/ArtalkJS/ArtalkGo/lib/artransfer"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -21,20 +20,10 @@ var exportCmd = &cobra.Command{
- 文档:https://artalk.js.org/guide/transfer.html
`,
Run: func(cmd *cobra.Command, args []string) {
comments := []model.Comment{}
lib.DB.Find(&comments)

artrans := []model.Artran{}
for _, c := range comments {
ct := c.ToArtran()
artrans = append(artrans, ct)
}

jsonByte, err := json.Marshal(artrans)
jsonStr, err := artransfer.ExportArtransString()
if err != nil {
logrus.Fatal(err)
}
jsonStr := string(jsonByte)

outputFile := cmd.Flag("output").Value.String()
if outputFile == "" {
Expand Down
17 changes: 10 additions & 7 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io/ioutil"
"os"

"github.com/ArtalkJS/ArtalkGo/lib/importer"
"github.com/ArtalkJS/ArtalkGo/lib/artransfer"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -30,29 +30,32 @@ var importCmd = &cobra.Command{
}

content := string(buf)
basic := importer.GetBasicParamsFrom(args[1:])
basic := artransfer.GetBasicParamsFrom(args[1:])
basic.UrlResolver = false // 默认关闭 URL 解析器:因为 pageKey 是完整,且站点隔离开
importer.ImportArtransByStr(basic, content)
artransfer.ImportArtransByStr(basic, content)

logrus.Info("导入结束")
},
Args: cobra.MinimumNArgs(1),
ArgAliases: importer.GetSupportNames(),
ArgAliases: artransfer.GetSupportNames(),
}

func init() {
rootCmd.AddCommand(importCmd)

for _, item := range importer.Supports {
imp := importer.GetImporterInfo(item)
for _, item := range artransfer.Supports {
imp := artransfer.GetImporterInfo(item)
subCmd := &cobra.Command{
Use: imp.Name + " [参数]",
Short: imp.Desc,
Run: func(cmd *cobra.Command, args []string) {
importer.RunByName(imp.Name, args)
artransfer.Assumeyes, _ = cmd.Flags().GetBool("assumeyes")
artransfer.RunByName(imp.Name, args)
},
}

importCmd.AddCommand(subCmd)
}

flagPV(importCmd, "assumeyes", "y", false, "Automatically answer yes for all questions.")
}
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"
"strings"
"time"
Expand Down Expand Up @@ -62,6 +63,16 @@ func init() {

rootCmd.SetVersionTemplate("Artalk-GO {{printf \"version %s\" .Version}}\n")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "配置文件路径 (defaults are './artalk-go.yml')")

versionCmd := &cobra.Command{
Use: "version",
Short: "输出 ArtalkGo 版本信息",
Version: Version,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ArtalkGo (" + lib.Version + "/" + lib.CommitHash + ")")
},
}
rootCmd.AddCommand(versionCmd)
}

// 1. 初始化配置
Expand Down
2 changes: 1 addition & 1 deletion http/a_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func InitRoute(e *echo.Echo) {
admin.POST("/site-del", ActionAdminSiteDel)
admin.POST("/setting-get", ActionAdminSettingGet)
admin.POST("/setting-save", ActionAdminSettingSave)
admin.POST("/importer", ActionAdminImporter)
admin.POST("/artransfer", ActionAdminArtransfer)
// admin.POST("/vote-sync", ActionAdminVoteSync) // 数据导入功能未关注 vote 部分,暂时注释

admin.POST("/send-mail", ActionAdminSendMail)
Expand Down
45 changes: 45 additions & 0 deletions http/api_admin_artransfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package http

import (
"encoding/json"
"net/http"

"github.com/ArtalkJS/ArtalkGo/lib/artransfer"
"github.com/labstack/echo/v4"
)

type ParamsAdminArtransfer struct {
Type string `mapstructure:"type" param:"required"`
Payload string `mapstructure:"payload"`
}

func ActionAdminArtransfer(c echo.Context) error {
if isOK, resp := AdminOnly(c); !isOK {
return resp
}

var p ParamsAdminArtransfer
if isOK, resp := ParamsDecode(c, ParamsAdminArtransfer{}, &p); !isOK {
return resp
}

var payload []string
err := json.Unmarshal([]byte(p.Payload), &payload)
if err != nil {
return RespError(c, "payload 解析错误", Map{
"error": err,
})
}

c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)

artransfer.Assumeyes = true
artransfer.HttpOutput = func(continueRun bool, text string) {
c.Response().Write([]byte(text))
c.Response().Flush()
}
artransfer.RunByName(p.Type, payload)

return nil
}
148 changes: 0 additions & 148 deletions http/api_admin_importer.go

This file was deleted.

18 changes: 13 additions & 5 deletions lib/importer/artalk_v1.go → lib/artransfer/artalk_v1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package importer
package artransfer

import (
"github.com/ArtalkJS/ArtalkGo/model"
Expand All @@ -17,12 +17,20 @@ type _ArtalkV1Importer struct {
}

func (imp *_ArtalkV1Importer) Run(basic *BasicParams, payload []string) {
RequiredBasicTargetSite(basic)
err := RequiredBasicTargetSite(basic)
if err != nil {
logFatal(err)
return
}

// 读取文件
jsonStr := JsonFileReady(payload)
jsonStr, jErr := JsonFileReady(payload)
if jErr != nil {
logFatal(jErr)
return
}

var aComments []ArtalkV1AFS
var aComments []ArtalkV1CommentAFS
JsonDecodeFAS(jsonStr, &aComments)

tp := []model.Artran{}
Expand All @@ -49,7 +57,7 @@ func (imp *_ArtalkV1Importer) Run(basic *BasicParams, payload []string) {
ImportArtrans(basic, tp)
}

type ArtalkV1AFS struct {
type ArtalkV1CommentAFS struct {
ID string `json:"id"`
Content string `json:"content"`
Nick string `json:"nick"`
Expand Down
Loading

0 comments on commit fbce496

Please sign in to comment.