Skip to content

Commit

Permalink
Merge pull request #11 from Baihhh/merge
Browse files Browse the repository at this point in the history
refactor: translate to article
  • Loading branch information
IRONICBo authored Jan 20, 2024
2 parents fffaf6f + e92ed03 commit 6e580e3
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 86 deletions.
53 changes: 36 additions & 17 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,53 @@ get "/edit", ctx => {
}
ctx.yap "edit", doc
}

get "/getTrans", ctx => {
id := ctx.param("id")
htmlUrl, _ := community.transHtmlUrl(todo, id)
ctx.json {
"data": htmlUrl,
}
}

// click "submit" button
post "/commit", ctx => {
uid := "12"
// Whether article has been translated or not
trans := ctx.param("trans") // if trans != "", add article
id := ctx.param("id")
mdData := ctx.param("content")
htmlData := ctx.param("html")
// TODO get user id
uid := "12"
// add article
article := &core.Article{
ArticleEntry: core.ArticleEntry{
ID: id,
Title: "Sample Title",
UId: "1",
Cover: "sample_cover",
Tags: "tag1",
Ctime: time.Now(),
Mtime: time.Now(),
ID: id,
Title: ctx.param("title"),
UId: uid,
Cover: ctx.param("cover"),
Tags: ctx.param("tags"),
},
// Status: 1,
Content: "Sample Markdown Content",
// HtmlUrl: "/sample-html-url",
Content: mdData,
HtmlData:htmlData,
}
// TODO check author exist
_, _ = community.putArticle(todo, uid, article)
id, _ = community.putArticle(todo, uid, trans, article)
article.ID = id
ctx.yap "edit", *article
}


// click "translate button"
post "/translate", ctx => {
mdData := ctx.param("id")
transData, _ := trans.TranslateMarkdownText(mdData, language.Chinese, language.English)
// TODO get user id
uid := "1"
mdData := ctx.param("content")
htmlData := ctx.param("html")
id, _ := community.saveHtml(todo, uid, htmlData, mdData)
// get translation markdown
transData, _ := trans.translateMarkdownText(mdData, language.Chinese, language.English)
ctx.json {
"data": transData,
"id": id, //article id
"data": transData, // translation markdown content
}
}

Expand Down
153 changes: 88 additions & 65 deletions cmd/gopcomm/gop_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"github.com/goplus/yap"
"context"
"time"
"log"
"io"
"net/http"
Expand Down Expand Up @@ -75,141 +74,165 @@ func (this *community) MainEntry() {
//line cmd/gopcomm/community_yap.gox:58:1
ctx.Yap__1("edit", doc)
})
//line cmd/gopcomm/community_yap.gox:60:1
this.Post("/commit", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:61:1
uid := "12"
this.Get("/getTrans", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:62:1
id := ctx.Param("id")
//line cmd/gopcomm/community_yap.gox:63:1
article := &core.Article{ArticleEntry: core.ArticleEntry{ID: id, Title: "Sample Title", UId: "1", Cover: "sample_cover", Tags: "tag1", Ctime: time.Now(), Mtime: time.Now()}, Content: "Sample Markdown Content"}
//line cmd/gopcomm/community_yap.gox:78:1
_, _ = this.community.PutArticle(todo, uid, article)
htmlUrl, _ := this.community.TransHtmlUrl(todo, id)
//line cmd/gopcomm/community_yap.gox:64:1
ctx.Json__1(map[string]string{"data": htmlUrl})
})
//line cmd/gopcomm/community_yap.gox:70:1
this.Post("/commit", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:72:1
trans := ctx.Param("trans")
//line cmd/gopcomm/community_yap.gox:73:1
id := ctx.Param("id")
//line cmd/gopcomm/community_yap.gox:74:1
mdData := ctx.Param("content")
//line cmd/gopcomm/community_yap.gox:75:1
htmlData := ctx.Param("html")
//line cmd/gopcomm/community_yap.gox:77:1
uid := "12"
//line cmd/gopcomm/community_yap.gox:79:1
article := &core.Article{ArticleEntry: core.ArticleEntry{ID: id, Title: ctx.Param("title"), UId: uid, Cover: ctx.Param("cover"), Tags: ctx.Param("tags")}, Content: mdData, HtmlData: htmlData}
//line cmd/gopcomm/community_yap.gox:90:1
id, _ = this.community.PutArticle(todo, uid, trans, article)
//line cmd/gopcomm/community_yap.gox:91:1
article.ID = id
//line cmd/gopcomm/community_yap.gox:92:1
ctx.Yap__1("edit", *article)
})
//line cmd/gopcomm/community_yap.gox:83:1
//line cmd/gopcomm/community_yap.gox:96:1
this.Post("/translate", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:84:1
mdData := ctx.Param("id")
//line cmd/gopcomm/community_yap.gox:85:1
//line cmd/gopcomm/community_yap.gox:98:1
uid := "1"
//line cmd/gopcomm/community_yap.gox:99:1
mdData := ctx.Param("content")
//line cmd/gopcomm/community_yap.gox:100:1
htmlData := ctx.Param("html")
//line cmd/gopcomm/community_yap.gox:101:1
id, _ := this.community.SaveHtml(todo, uid, htmlData, mdData)
//line cmd/gopcomm/community_yap.gox:103:1
transData, _ := this.trans.TranslateMarkdownText(mdData, language.Chinese, language.English)
//line cmd/gopcomm/community_yap.gox:86:1
ctx.Json__1(map[string]string{"data": transData})
//line cmd/gopcomm/community_yap.gox:104:1
ctx.Json__1(map[string]interface {
}{"id": id, "data": transData})
})
//line cmd/gopcomm/community_yap.gox:91:1
//line cmd/gopcomm/community_yap.gox:110:1
this.Get("/getMedia/:id", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:92:1
//line cmd/gopcomm/community_yap.gox:111:1
mediaId := ctx.Param("id")
//line cmd/gopcomm/community_yap.gox:94:1
//line cmd/gopcomm/community_yap.gox:113:1
fileKey, _ := this.community.GetMediaUrl(context.Background(), mediaId)
//line cmd/gopcomm/community_yap.gox:96:1
//line cmd/gopcomm/community_yap.gox:115:1
http.Redirect(ctx.ResponseWriter, ctx.Request, "qiniu demain"+fileKey, http.StatusTemporaryRedirect)
})
//line cmd/gopcomm/community_yap.gox:98:1
//line cmd/gopcomm/community_yap.gox:117:1
this.Post("/upload", func(ctx *yap.Context) {
//line cmd/gopcomm/community_yap.gox:99:1
//line cmd/gopcomm/community_yap.gox:118:1
file, header, err := ctx.FormFile("file")
//line cmd/gopcomm/community_yap.gox:100:1
//line cmd/gopcomm/community_yap.gox:119:1
filename := header.Filename
//line cmd/gopcomm/community_yap.gox:102:1
//line cmd/gopcomm/community_yap.gox:121:1
ctx.ParseMultipartForm(10 << 20)
//line cmd/gopcomm/community_yap.gox:104:1
//line cmd/gopcomm/community_yap.gox:123:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:105:1
//line cmd/gopcomm/community_yap.gox:124:1
log.Fatalln("upload file error:", filename)
//line cmd/gopcomm/community_yap.gox:106:1
//line cmd/gopcomm/community_yap.gox:125:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:107:1
//line cmd/gopcomm/community_yap.gox:126:1
return
}
//line cmd/gopcomm/community_yap.gox:111:1
//line cmd/gopcomm/community_yap.gox:130:1
dst, err := os.Create(filename)
//line cmd/gopcomm/community_yap.gox:112:1
//line cmd/gopcomm/community_yap.gox:131:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:113:1
//line cmd/gopcomm/community_yap.gox:132:1
log.Fatalln("create file error:", file)
//line cmd/gopcomm/community_yap.gox:114:1
//line cmd/gopcomm/community_yap.gox:133:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:115:1
//line cmd/gopcomm/community_yap.gox:134:1
return
}
//line cmd/gopcomm/community_yap.gox:117:1
//line cmd/gopcomm/community_yap.gox:136:1
defer func() {
//line cmd/gopcomm/community_yap.gox:118:1
//line cmd/gopcomm/community_yap.gox:137:1
file.Close()
//line cmd/gopcomm/community_yap.gox:119:1
//line cmd/gopcomm/community_yap.gox:138:1
dst.Close()
//line cmd/gopcomm/community_yap.gox:120:1
//line cmd/gopcomm/community_yap.gox:139:1
err = os.Remove(filename)
//line cmd/gopcomm/community_yap.gox:121:1
//line cmd/gopcomm/community_yap.gox:140:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:122:1
//line cmd/gopcomm/community_yap.gox:141:1
log.Fatalln("delete file error:", filename)
//line cmd/gopcomm/community_yap.gox:123:1
//line cmd/gopcomm/community_yap.gox:142:1
return
}
}()
//line cmd/gopcomm/community_yap.gox:128:1
//line cmd/gopcomm/community_yap.gox:147:1
_, err = io.Copy(dst, file)
//line cmd/gopcomm/community_yap.gox:129:1
//line cmd/gopcomm/community_yap.gox:148:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:130:1
//line cmd/gopcomm/community_yap.gox:149:1
log.Fatalln("copy file errer:", filename)
//line cmd/gopcomm/community_yap.gox:131:1
//line cmd/gopcomm/community_yap.gox:150:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:132:1
//line cmd/gopcomm/community_yap.gox:151:1
return
}
//line cmd/gopcomm/community_yap.gox:134:1
//line cmd/gopcomm/community_yap.gox:153:1
bytes, err := os.ReadFile(filename)
//line cmd/gopcomm/community_yap.gox:135:1
//line cmd/gopcomm/community_yap.gox:154:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:136:1
//line cmd/gopcomm/community_yap.gox:155:1
log.Fatalln("read file errer:", filename)
//line cmd/gopcomm/community_yap.gox:137:1
//line cmd/gopcomm/community_yap.gox:156:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:138:1
//line cmd/gopcomm/community_yap.gox:157:1
return
}
//line cmd/gopcomm/community_yap.gox:140:1
//line cmd/gopcomm/community_yap.gox:159:1
cookie, err := ctx.Request.Cookie("user_id")
//line cmd/gopcomm/community_yap.gox:141:1
//line cmd/gopcomm/community_yap.gox:160:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:142:1
//line cmd/gopcomm/community_yap.gox:161:1
log.Fatalln("token不存在")
//line cmd/gopcomm/community_yap.gox:143:1
//line cmd/gopcomm/community_yap.gox:162:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:144:1
//line cmd/gopcomm/community_yap.gox:163:1
return
}
//line cmd/gopcomm/community_yap.gox:147:1
//line cmd/gopcomm/community_yap.gox:166:1
id, err := this.community.SaveMedia(context.Background(), cookie.Value, bytes)
//line cmd/gopcomm/community_yap.gox:148:1
//line cmd/gopcomm/community_yap.gox:167:1
if err != nil {
//line cmd/gopcomm/community_yap.gox:150:1
//line cmd/gopcomm/community_yap.gox:168:1
log.Fatalln("save file", err.Error())
//line cmd/gopcomm/community_yap.gox:151:1
//line cmd/gopcomm/community_yap.gox:169:1
ctx.JSON(500, err.Error())
//line cmd/gopcomm/community_yap.gox:152:1
//line cmd/gopcomm/community_yap.gox:170:1
return
}
//line cmd/gopcomm/community_yap.gox:156:1
//line cmd/gopcomm/community_yap.gox:174:1
ctx.JSON(200, id)
})
//line cmd/gopcomm/community_yap.gox:159:1
//line cmd/gopcomm/community_yap.gox:177:1
conf := &core.Config{}
//line cmd/gopcomm/community_yap.gox:160:1
//line cmd/gopcomm/community_yap.gox:178:1
this.community, _ = core.New(todo, conf)
//line cmd/gopcomm/community_yap.gox:161:1
//line cmd/gopcomm/community_yap.gox:179:1
this.trans = translation.New(os.Getenv("NIUTRANS_API_KEY"), "", "")
//line cmd/gopcomm/community_yap.gox:163:1
//line cmd/gopcomm/community_yap.gox:181:1
s3 := os.Getenv("S3_BUCKET")
//line cmd/gopcomm/community_yap.gox:164:1
//line cmd/gopcomm/community_yap.gox:182:1
fmt.Println(s3)
//line cmd/gopcomm/community_yap.gox:165:1
//line cmd/gopcomm/community_yap.gox:183:1
fmt.Println("start")
//line cmd/gopcomm/community_yap.gox:166:1
//line cmd/gopcomm/community_yap.gox:184:1
this.Run__1(":8080")
}
func main() {
Expand Down
50 changes: 46 additions & 4 deletions internal/core/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package core
import (
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -55,7 +56,8 @@ type Article struct {
ArticleEntry
Content string // in markdown
// Status int // published or draft
HtmlUrl string // parsed html file url
HtmlUrl string // parsed html file url
HtmlData string
}

type Community struct {
Expand Down Expand Up @@ -124,6 +126,26 @@ func (p *Community) Article(ctx context.Context, id string) (article *Article, e
return
}

// TransContent get translation html url
func (p *Community) TransHtmlUrl(ctx context.Context, id string) (htmlUrl string, err error) {
var htmlId string
sqlStr := "select trans_html_id from article where id=?"
err = p.db.QueryRow(sqlStr, id).Scan(&htmlId)
if err == sql.ErrNoRows {
log.Println("not found the translation html")
return "", ErrNotExist
}

// get html url
fileKey, err := p.GetMediaUrl(ctx, htmlId)
htmlUrl = fmt.Sprintf("%s%s", p.domain, fileKey)
if err != nil {
log.Println("have no html media")
htmlUrl = ""
}
return
}

// CanEditable determine whether the user has the permission to operate.
func (p *Community) CanEditable(ctx context.Context, uid, id string) (editable bool, err error) {
sqlStr := "select id from article where id=? and user_id = ?"
Expand All @@ -134,15 +156,28 @@ func (p *Community) CanEditable(ctx context.Context, uid, id string) (editable b
return true, nil
}

// htmlStrToUrl upload html(string) to media for html url
// SaveHtml upload origin html(string) to media for html id and save id to database
func (p *Community) SaveHtml(ctx context.Context, uid, htmlStr, mdData string) (articleId int64, err error) {
htmlId, err := p.SaveMedia(ctx, uid, []byte(htmlStr))
// save to database
sqlStr := "insert into article (user_id, html_id, content) values (?, ?, ?)"
res, err := p.db.Exec(sqlStr, uid, htmlId, mdData)
if err != nil {
return 0, err
}
articleId, err = res.LastInsertId()
return
}

// uploadHtml upload html(string) to media for html id
func (p *Community) uploadHtml(ctx context.Context, uid, htmlStr string) (htmlId int64, err error) {
htmlId, err = p.SaveMedia(ctx, uid, []byte(htmlStr))
return
}

// PutArticle adds new article (ID == "") or edits an existing article (ID != "").
func (p *Community) PutArticle(ctx context.Context, uid string, article *Article) (id string, err error) {
htmlId, err := p.uploadHtml(ctx, uid, article.Content)
func (p *Community) PutArticle(ctx context.Context, uid string, trans string, article *Article) (id string, err error) {
htmlId, err := p.uploadHtml(ctx, uid, article.HtmlData)
if err != nil {
htmlId = 0
}
Expand All @@ -156,6 +191,13 @@ func (p *Community) PutArticle(ctx context.Context, uid string, article *Article
idInt, err := res.LastInsertId()
return strconv.FormatInt(idInt, 10), nil
}
if trans != "" {
// add article except html_id, content (trans)
sqlStr := "update article set title=?, mtime=?, ctime=?, tags=?, cover=?, trans_content=?, trans_html_id=? where id=?"
_, err = p.db.Exec(sqlStr, &article.Title, time.Now(), time.Now(), &article.Tags, &article.Cover, &article.Content, htmlId, &article.ID)
return article.ID, err
}

// edit article
sqlStr := "update article set title=?, mtime=?, tags=?, cover=?, content=?, html_id=? where id=?"
_, err = p.db.Exec(sqlStr, &article.Title, time.Now(), &article.Tags, &article.Cover, &article.Content, htmlId, &article.ID)
Expand Down

0 comments on commit 6e580e3

Please sign in to comment.