From 535d3f9ccef91e8e4e06139729a022e76ffc6a12 Mon Sep 17 00:00:00 2001 From: Xhy <1416318023@qq.com> Date: Thu, 22 Feb 2024 21:33:39 +0800 Subject: [PATCH] feat: support user video list lazy load and page --- cmd/gopcomm/community_yap.gox | 41 ++-- cmd/gopcomm/gop_autogen.go | 413 +++++++++++++++++----------------- internal/core/media.go | 83 +++++-- 3 files changed, 294 insertions(+), 243 deletions(-) diff --git a/cmd/gopcomm/community_yap.gox b/cmd/gopcomm/community_yap.gox index bb0ceb8..c2f3174 100644 --- a/cmd/gopcomm/community_yap.gox +++ b/cmd/gopcomm/community_yap.gox @@ -198,34 +198,33 @@ get "/delete", ctx => { } get "/medias", ctx => { - format := ctx.param("format") - uid := ctx.param("uid") - from := ctx.param("from") - fromInt, err := strconv.Atoi(from) + format := ctx.param("format") + uid := ctx.param("uid") + page := ctx.param("page") + pageInt, err := strconv.Atoi(page) if err != nil { - fromInt = 0 + pageInt = 1 } limit := ctx.param("limit") limitInt, err := strconv.Atoi(limit) if err != nil { - limitInt = limitConst + limitInt = limitConst + } + files,count,err := community.ListMediaByUserId(todo, uid, format,pageInt,limitInt) + if err != nil { + ctx.json { + "code": 0, + "count": count, + "err": err.Error(), + } + } else { + ctx.json { + "code": 200, + "count": count, + "items": files, + } } - files,next,err := community.ListMediaByUserId(todo, uid, format,fromInt,limitInt) - if err != nil { - ctx.json { - "code": 0, - "next": next, - "err": err.Error(), - } - } else { - ctx.json { - "code": 200, - "next": next, - "items": files, - } - } } - get "/delMedia", ctx => { id := ctx.param("id") token, err := core.GetToken(ctx) diff --git a/cmd/gopcomm/gop_autogen.go b/cmd/gopcomm/gop_autogen.go index bff2b64..b0af2ca 100644 --- a/cmd/gopcomm/gop_autogen.go +++ b/cmd/gopcomm/gop_autogen.go @@ -1,21 +1,21 @@ package main import ( - "fmt" - "os" - "strconv" - "strings" - "github.com/goplus/yap" "context" "encoding/json" - "net/http" - "net/url" + "fmt" "github.com/goplus/community/internal/core" "github.com/goplus/community/translation" + "github.com/goplus/yap" _ "github.com/joho/godotenv/autoload" gopaccountsdk "github.com/liuscraft/gop-casdoor-account-sdk" "github.com/qiniu/x/xlog" "golang.org/x/text/language" + "net/http" + "net/url" + "os" + "strconv" + "strings" ) const ( @@ -23,13 +23,14 @@ const ( limitConst = 10 ) -type community struct { +type community_yap struct { yap.App community *core.Community trans *translation.Engine } + //line cmd/gopcomm/community_yap.gox:29 -func (this *community) MainEntry() { +func (this *community_yap) MainEntry() { //line cmd/gopcomm/community_yap.gox:29:1 todo := context.TODO() //line cmd/gopcomm/community_yap.gox:30:1 @@ -271,13 +272,13 @@ func (this *community) MainEntry() { //line cmd/gopcomm/community_yap.gox:202:1 uid := ctx.Param("uid") //line cmd/gopcomm/community_yap.gox:203:1 - from := ctx.Param("from") + page := ctx.Param("page") //line cmd/gopcomm/community_yap.gox:204:1 - fromInt, err := strconv.Atoi(from) + pageInt, err := strconv.Atoi(page) //line cmd/gopcomm/community_yap.gox:205:1 if err != nil { //line cmd/gopcomm/community_yap.gox:206:1 - fromInt = 0 + pageInt = 1 } //line cmd/gopcomm/community_yap.gox:208:1 limit := ctx.Param("limit") @@ -289,485 +290,485 @@ func (this *community) MainEntry() { limitInt = limitConst } //line cmd/gopcomm/community_yap.gox:213:1 - files, next, err := this.community.ListMediaByUserId(todo, uid, format, fromInt, limitInt) + files, count, err := this.community.ListMediaByUserId(todo, uid, format, pageInt, limitInt) //line cmd/gopcomm/community_yap.gox:214:1 if err != nil { //line cmd/gopcomm/community_yap.gox:215:1 ctx.Json__1(map[string]interface { - }{"code": 0, "next": next, "err": err.Error()}) + }{"code": 0, "count": count, "err": err.Error()}) } else { //line cmd/gopcomm/community_yap.gox:221:1 ctx.Json__1(map[string]interface { - }{"code": 200, "next": next, "items": files}) + }{"code": 200, "count": count, "items": files}) } }) -//line cmd/gopcomm/community_yap.gox:229:1 +//line cmd/gopcomm/community_yap.gox:228:1 this.Get("/delMedia", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:230:1 +//line cmd/gopcomm/community_yap.gox:229:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:231:1 +//line cmd/gopcomm/community_yap.gox:230:1 token, err := core.GetToken(ctx) -//line cmd/gopcomm/community_yap.gox:232:1 +//line cmd/gopcomm/community_yap.gox:231:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:233:1 +//line cmd/gopcomm/community_yap.gox:232:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:234:1 +//line cmd/gopcomm/community_yap.gox:233:1 xLog.Error("token parse error") -//line cmd/gopcomm/community_yap.gox:235:1 +//line cmd/gopcomm/community_yap.gox:234:1 ctx.Json__1(map[string]interface { }{"code": 0, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:240:1 +//line cmd/gopcomm/community_yap.gox:239:1 err = this.community.DelMedia(todo, uid, id) -//line cmd/gopcomm/community_yap.gox:241:1 +//line cmd/gopcomm/community_yap.gox:240:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:242:1 +//line cmd/gopcomm/community_yap.gox:241:1 ctx.Json__1(map[string]interface { }{"code": 0, "err": "delete failed"}) } else { -//line cmd/gopcomm/community_yap.gox:247:1 +//line cmd/gopcomm/community_yap.gox:246:1 ctx.Json__1(map[string]interface { }{"code": 200, "msg": "delete success"}) } }) -//line cmd/gopcomm/community_yap.gox:254:1 +//line cmd/gopcomm/community_yap.gox:253:1 this.Get("/", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:256:1 +//line cmd/gopcomm/community_yap.gox:255:1 // Get User Info var user *core.User -//line cmd/gopcomm/community_yap.gox:257:1 +//line cmd/gopcomm/community_yap.gox:256:1 userId := "" -//line cmd/gopcomm/community_yap.gox:258:1 +//line cmd/gopcomm/community_yap.gox:257:1 token, err := core.GetToken(ctx) -//line cmd/gopcomm/community_yap.gox:259:1 +//line cmd/gopcomm/community_yap.gox:258:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:260:1 +//line cmd/gopcomm/community_yap.gox:259:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:261:1 +//line cmd/gopcomm/community_yap.gox:260:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:262:1 +//line cmd/gopcomm/community_yap.gox:261:1 xLog.Error("get user error:", err) } else { -//line cmd/gopcomm/community_yap.gox:264:1 +//line cmd/gopcomm/community_yap.gox:263:1 userId = user.Id } } -//line cmd/gopcomm/community_yap.gox:268:1 +//line cmd/gopcomm/community_yap.gox:267:1 articles, next, _ := this.community.ListArticle(todo, core.MarkBegin, limitConst, "") -//line cmd/gopcomm/community_yap.gox:269:1 +//line cmd/gopcomm/community_yap.gox:268:1 articlesJson, _ := json.Marshal(&articles) -//line cmd/gopcomm/community_yap.gox:270:1 +//line cmd/gopcomm/community_yap.gox:269:1 ctx.Yap__1("home", map[string]interface { }{"UserId": userId, "User": user, "Items": strings.Replace(string(articlesJson), `\"`, `"`, -1), "Next": next}) }) -//line cmd/gopcomm/community_yap.gox:278:1 +//line cmd/gopcomm/community_yap.gox:277:1 this.Get("/get", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:279:1 +//line cmd/gopcomm/community_yap.gox:278:1 from := ctx.Param("from") -//line cmd/gopcomm/community_yap.gox:280:1 +//line cmd/gopcomm/community_yap.gox:279:1 limit := ctx.Param("limit") -//line cmd/gopcomm/community_yap.gox:281:1 +//line cmd/gopcomm/community_yap.gox:280:1 searchValue := ctx.Param("value") -//line cmd/gopcomm/community_yap.gox:283:1 +//line cmd/gopcomm/community_yap.gox:282:1 limitInt, err := strconv.Atoi(limit) -//line cmd/gopcomm/community_yap.gox:284:1 +//line cmd/gopcomm/community_yap.gox:283:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:285:1 +//line cmd/gopcomm/community_yap.gox:284:1 limitInt = limitConst } -//line cmd/gopcomm/community_yap.gox:288:1 +//line cmd/gopcomm/community_yap.gox:287:1 articles, next, _ := this.community.ListArticle(todo, from, limitInt, searchValue) -//line cmd/gopcomm/community_yap.gox:289:1 +//line cmd/gopcomm/community_yap.gox:288:1 ctx.Json__1(map[string]interface { }{"code": 200, "items": articles, "next": next, "value": searchValue}) }) -//line cmd/gopcomm/community_yap.gox:297:1 +//line cmd/gopcomm/community_yap.gox:296:1 this.Get("/search", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:298:1 +//line cmd/gopcomm/community_yap.gox:297:1 searchValue := ctx.Param("value") -//line cmd/gopcomm/community_yap.gox:307:1 +//line cmd/gopcomm/community_yap.gox:306:1 // todo middleware var user *core.User -//line cmd/gopcomm/community_yap.gox:308:1 +//line cmd/gopcomm/community_yap.gox:307:1 userId := "" -//line cmd/gopcomm/community_yap.gox:309:1 +//line cmd/gopcomm/community_yap.gox:308:1 token, err := core.GetToken(ctx) -//line cmd/gopcomm/community_yap.gox:310:1 +//line cmd/gopcomm/community_yap.gox:309:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:311:1 +//line cmd/gopcomm/community_yap.gox:310:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:312:1 +//line cmd/gopcomm/community_yap.gox:311:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:313:1 +//line cmd/gopcomm/community_yap.gox:312:1 xLog.Error("get user error:", err) } else { -//line cmd/gopcomm/community_yap.gox:315:1 +//line cmd/gopcomm/community_yap.gox:314:1 userId = user.Id } } -//line cmd/gopcomm/community_yap.gox:319:1 +//line cmd/gopcomm/community_yap.gox:318:1 articles, next, _ := this.community.ListArticle(todo, core.MarkBegin, limitConst, searchValue) -//line cmd/gopcomm/community_yap.gox:320:1 +//line cmd/gopcomm/community_yap.gox:319:1 articlesJson, _ := json.Marshal(&articles) -//line cmd/gopcomm/community_yap.gox:321:1 +//line cmd/gopcomm/community_yap.gox:320:1 ctx.Yap__1("home", map[string]interface { }{"UserId": userId, "User": user, "Items": strings.Replace(string(articlesJson), `\"`, `"`, -1), "Value": searchValue, "Next": next}) }) -//line cmd/gopcomm/community_yap.gox:330:1 +//line cmd/gopcomm/community_yap.gox:329:1 this.Get("/edit/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:331:1 +//line cmd/gopcomm/community_yap.gox:330:1 var user *core.User -//line cmd/gopcomm/community_yap.gox:332:1 +//line cmd/gopcomm/community_yap.gox:331:1 token, err := core.GetToken(ctx) -//line cmd/gopcomm/community_yap.gox:333:1 +//line cmd/gopcomm/community_yap.gox:332:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:334:1 +//line cmd/gopcomm/community_yap.gox:333:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:335:1 +//line cmd/gopcomm/community_yap.gox:334:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:336:1 +//line cmd/gopcomm/community_yap.gox:335:1 xLog.Error("get user error:", err) } } -//line cmd/gopcomm/community_yap.gox:340:1 +//line cmd/gopcomm/community_yap.gox:339:1 uid := user.Id -//line cmd/gopcomm/community_yap.gox:341:1 +//line cmd/gopcomm/community_yap.gox:340:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:342:1 +//line cmd/gopcomm/community_yap.gox:341:1 if id != "" { -//line cmd/gopcomm/community_yap.gox:343:1 +//line cmd/gopcomm/community_yap.gox:342:1 if -//line cmd/gopcomm/community_yap.gox:343:1 +//line cmd/gopcomm/community_yap.gox:342:1 editable, _ := this.community.CanEditable(todo, uid, id); !editable { -//line cmd/gopcomm/community_yap.gox:344:1 +//line cmd/gopcomm/community_yap.gox:343:1 xLog.Error("no permissions") -//line cmd/gopcomm/community_yap.gox:345:1 +//line cmd/gopcomm/community_yap.gox:344:1 http.Redirect(ctx.ResponseWriter, ctx.Request, "/error", http.StatusTemporaryRedirect) } -//line cmd/gopcomm/community_yap.gox:347:1 +//line cmd/gopcomm/community_yap.gox:346:1 article, _ := this.community.Article(todo, id) -//line cmd/gopcomm/community_yap.gox:348:1 +//line cmd/gopcomm/community_yap.gox:347:1 articleJson, _ := json.Marshal(&article) -//line cmd/gopcomm/community_yap.gox:349:1 +//line cmd/gopcomm/community_yap.gox:348:1 ctx.Yap__1("edit", map[string]interface { }{"User": user, "Article": strings.Replace(string(articleJson), `\"`, `"`, -1)}) } }) -//line cmd/gopcomm/community_yap.gox:356:1 +//line cmd/gopcomm/community_yap.gox:355:1 this.Get("/getTrans", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:357:1 +//line cmd/gopcomm/community_yap.gox:356:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:358:1 +//line cmd/gopcomm/community_yap.gox:357:1 htmlUrl, err := this.community.TransHtmlUrl(todo, id) -//line cmd/gopcomm/community_yap.gox:359:1 +//line cmd/gopcomm/community_yap.gox:358:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:360:1 +//line cmd/gopcomm/community_yap.gox:359:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:365:1 +//line cmd/gopcomm/community_yap.gox:364:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": htmlUrl}) }) -//line cmd/gopcomm/community_yap.gox:372:1 +//line cmd/gopcomm/community_yap.gox:371:1 this.Post("/commit", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:373:1 +//line cmd/gopcomm/community_yap.gox:372:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:374:1 +//line cmd/gopcomm/community_yap.gox:373:1 content := ctx.Param("content") -//line cmd/gopcomm/community_yap.gox:375:1 +//line cmd/gopcomm/community_yap.gox:374:1 title := ctx.Param("title") -//line cmd/gopcomm/community_yap.gox:376:1 +//line cmd/gopcomm/community_yap.gox:375:1 tags := ctx.Param("tags") -//line cmd/gopcomm/community_yap.gox:377:1 +//line cmd/gopcomm/community_yap.gox:376:1 abstract := ctx.Param("abstract") -//line cmd/gopcomm/community_yap.gox:378:1 +//line cmd/gopcomm/community_yap.gox:377:1 trans, _ := strconv.ParseBool(ctx.Param("trans")) -//line cmd/gopcomm/community_yap.gox:380:1 +//line cmd/gopcomm/community_yap.gox:379:1 token, err := core.GetToken(ctx) -//line cmd/gopcomm/community_yap.gox:381:1 +//line cmd/gopcomm/community_yap.gox:380:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:382:1 +//line cmd/gopcomm/community_yap.gox:381:1 xLog.Info("token", err) -//line cmd/gopcomm/community_yap.gox:383:1 +//line cmd/gopcomm/community_yap.gox:382:1 ctx.Json__1(map[string]interface { }{"code": 0, "err": "no token"}) } -//line cmd/gopcomm/community_yap.gox:388:1 +//line cmd/gopcomm/community_yap.gox:387:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:389:1 +//line cmd/gopcomm/community_yap.gox:388:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:390:1 +//line cmd/gopcomm/community_yap.gox:389:1 xLog.Info("uid", err) -//line cmd/gopcomm/community_yap.gox:391:1 +//line cmd/gopcomm/community_yap.gox:390:1 ctx.Json__1(map[string]interface { }{"code": 0, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:396:1 +//line cmd/gopcomm/community_yap.gox:395:1 article := &core.Article{ArticleEntry: core.ArticleEntry{ID: id, Title: title, UId: uid, Cover: ctx.Param("cover"), Tags: tags, Abstract: abstract}, Content: content, Trans: trans} -//line cmd/gopcomm/community_yap.gox:410:1 +//line cmd/gopcomm/community_yap.gox:409:1 if trans { -//line cmd/gopcomm/community_yap.gox:411:1 +//line cmd/gopcomm/community_yap.gox:410:1 article, _ = this.community.TranslateArticle(todo, article) } -//line cmd/gopcomm/community_yap.gox:414:1 +//line cmd/gopcomm/community_yap.gox:413:1 id, err = this.community.PutArticle(todo, uid, article) -//line cmd/gopcomm/community_yap.gox:415:1 +//line cmd/gopcomm/community_yap.gox:414:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:416:1 +//line cmd/gopcomm/community_yap.gox:415:1 ctx.Json__1(map[string]interface { }{"code": 0, "err": "add failed"}) } -//line cmd/gopcomm/community_yap.gox:421:1 +//line cmd/gopcomm/community_yap.gox:420:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": id}) }) -//line cmd/gopcomm/community_yap.gox:427:1 +//line cmd/gopcomm/community_yap.gox:426:1 this.Get("/tranArticle", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:428:1 +//line cmd/gopcomm/community_yap.gox:427:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:430:1 +//line cmd/gopcomm/community_yap.gox:429:1 article, err := this.community.GetTranslateArticle(todo, id) -//line cmd/gopcomm/community_yap.gox:431:1 +//line cmd/gopcomm/community_yap.gox:430:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:432:1 +//line cmd/gopcomm/community_yap.gox:431:1 ctx.Json__1(map[string]int{"code": 0}) } -//line cmd/gopcomm/community_yap.gox:436:1 +//line cmd/gopcomm/community_yap.gox:435:1 ctx.Json__1(map[string]interface { }{"code": 200, "content": article.Content, "tags": article.Tags, "title": article.Title}) }) -//line cmd/gopcomm/community_yap.gox:445:1 +//line cmd/gopcomm/community_yap.gox:444:1 this.Post("/translate", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:446:1 +//line cmd/gopcomm/community_yap.gox:445:1 mdData := ctx.Param("content") -//line cmd/gopcomm/community_yap.gox:447:1 +//line cmd/gopcomm/community_yap.gox:446:1 transData, err := this.community.TranslateMarkdownText(todo, mdData, "auto", language.English) -//line cmd/gopcomm/community_yap.gox:448:1 +//line cmd/gopcomm/community_yap.gox:447:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:449:1 +//line cmd/gopcomm/community_yap.gox:448:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:454:1 +//line cmd/gopcomm/community_yap.gox:453:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": transData}) }) -//line cmd/gopcomm/community_yap.gox:461:1 +//line cmd/gopcomm/community_yap.gox:460:1 this.Get("/getMedia/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:462:1 +//line cmd/gopcomm/community_yap.gox:461:1 mediaId := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:464:1 +//line cmd/gopcomm/community_yap.gox:463:1 fileKey, _ := this.community.GetMediaUrl(context.Background(), mediaId) -//line cmd/gopcomm/community_yap.gox:466:1 +//line cmd/gopcomm/community_yap.gox:465:1 http.Redirect(ctx.ResponseWriter, ctx.Request, domain+fileKey, http.StatusTemporaryRedirect) }) -//line cmd/gopcomm/community_yap.gox:469:1 +//line cmd/gopcomm/community_yap.gox:468:1 this.Get("/getMediaUrl/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:470:1 +//line cmd/gopcomm/community_yap.gox:469:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:471:1 +//line cmd/gopcomm/community_yap.gox:470:1 fileKey, err := this.community.GetMediaUrl(todo, id) -//line cmd/gopcomm/community_yap.gox:472:1 +//line cmd/gopcomm/community_yap.gox:471:1 htmlUrl := fmt.Sprintf("%s%s", domain, fileKey) -//line cmd/gopcomm/community_yap.gox:473:1 +//line cmd/gopcomm/community_yap.gox:472:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:474:1 +//line cmd/gopcomm/community_yap.gox:473:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "have no html media"}) } -//line cmd/gopcomm/community_yap.gox:479:1 +//line cmd/gopcomm/community_yap.gox:478:1 ctx.Json__1(map[string]interface { }{"code": 200, "url": htmlUrl}) }) -//line cmd/gopcomm/community_yap.gox:485:1 +//line cmd/gopcomm/community_yap.gox:484:1 this.Get("/getVideoAndSubtitle/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:486:1 +//line cmd/gopcomm/community_yap.gox:485:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:487:1 +//line cmd/gopcomm/community_yap.gox:486:1 fileKey, err := this.community.GetMediaUrl(todo, id) -//line cmd/gopcomm/community_yap.gox:488:1 +//line cmd/gopcomm/community_yap.gox:487:1 m := make(map[string]string, 2) -//line cmd/gopcomm/community_yap.gox:489:1 +//line cmd/gopcomm/community_yap.gox:488:1 format, err := this.community.GetMediaType(todo, id) -//line cmd/gopcomm/community_yap.gox:490:1 +//line cmd/gopcomm/community_yap.gox:489:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:491:1 +//line cmd/gopcomm/community_yap.gox:490:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:496:1 +//line cmd/gopcomm/community_yap.gox:495:1 if format == "video/mp4" { -//line cmd/gopcomm/community_yap.gox:497:1 +//line cmd/gopcomm/community_yap.gox:496:1 subtitle, err := this.community.GetVideoSubtitle(todo, id) -//line cmd/gopcomm/community_yap.gox:498:1 +//line cmd/gopcomm/community_yap.gox:497:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:499:1 +//line cmd/gopcomm/community_yap.gox:498:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:500:1 +//line cmd/gopcomm/community_yap.gox:499:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:505:1 +//line cmd/gopcomm/community_yap.gox:504:1 return } -//line cmd/gopcomm/community_yap.gox:507:1 +//line cmd/gopcomm/community_yap.gox:506:1 m["subtitle"] = domain + subtitle } -//line cmd/gopcomm/community_yap.gox:509:1 +//line cmd/gopcomm/community_yap.gox:508:1 htmlUrl := fmt.Sprintf("%s%s", domain, fileKey) -//line cmd/gopcomm/community_yap.gox:510:1 +//line cmd/gopcomm/community_yap.gox:509:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:511:1 +//line cmd/gopcomm/community_yap.gox:510:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "have no html media"}) } -//line cmd/gopcomm/community_yap.gox:516:1 +//line cmd/gopcomm/community_yap.gox:515:1 m["fileKey"] = htmlUrl -//line cmd/gopcomm/community_yap.gox:517:1 +//line cmd/gopcomm/community_yap.gox:516:1 ctx.Json__1(map[string]interface { }{"code": 200, "url": m}) }) -//line cmd/gopcomm/community_yap.gox:523:1 +//line cmd/gopcomm/community_yap.gox:522:1 this.Post("/upload", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:524:1 +//line cmd/gopcomm/community_yap.gox:523:1 this.community.UploadFile(ctx) }) -//line cmd/gopcomm/community_yap.gox:527:1 +//line cmd/gopcomm/community_yap.gox:526:1 this.Post("/caption/task", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:528:1 +//line cmd/gopcomm/community_yap.gox:527:1 uid := ctx.Param("uid") -//line cmd/gopcomm/community_yap.gox:529:1 +//line cmd/gopcomm/community_yap.gox:528:1 vid := ctx.Param("vid") -//line cmd/gopcomm/community_yap.gox:531:1 +//line cmd/gopcomm/community_yap.gox:530:1 if uid == "" || vid == "" { -//line cmd/gopcomm/community_yap.gox:532:1 +//line cmd/gopcomm/community_yap.gox:531:1 ctx.Json__1(map[string]interface { }{"code": 200, "msg": "Invalid param"}) } -//line cmd/gopcomm/community_yap.gox:538:1 +//line cmd/gopcomm/community_yap.gox:537:1 if -//line cmd/gopcomm/community_yap.gox:538:1 +//line cmd/gopcomm/community_yap.gox:537:1 err := this.community.RetryCaptionGenerate(todo, uid, vid); err != nil { -//line cmd/gopcomm/community_yap.gox:539:1 +//line cmd/gopcomm/community_yap.gox:538:1 ctx.Json__1(map[string]interface { }{"code": 200, "msg": "Request task error"}) } -//line cmd/gopcomm/community_yap.gox:545:1 +//line cmd/gopcomm/community_yap.gox:544:1 ctx.Json__1(map[string]interface { }{"code": 200, "msg": "Ok"}) }) -//line cmd/gopcomm/community_yap.gox:551:1 +//line cmd/gopcomm/community_yap.gox:550:1 this.Get("/login", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:556:1 +//line cmd/gopcomm/community_yap.gox:555:1 refererURL, err := url.Parse(ctx.Request.Referer()) -//line cmd/gopcomm/community_yap.gox:557:1 +//line cmd/gopcomm/community_yap.gox:556:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:558:1 +//line cmd/gopcomm/community_yap.gox:557:1 xLog.Info("Error parsing Referer: %v", err) -//line cmd/gopcomm/community_yap.gox:559:1 +//line cmd/gopcomm/community_yap.gox:558:1 return } -//line cmd/gopcomm/community_yap.gox:562:1 +//line cmd/gopcomm/community_yap.gox:561:1 refererPath := refererURL.Path -//line cmd/gopcomm/community_yap.gox:563:1 +//line cmd/gopcomm/community_yap.gox:562:1 if refererURL.RawQuery != "" { -//line cmd/gopcomm/community_yap.gox:564:1 +//line cmd/gopcomm/community_yap.gox:563:1 refererPath = fmt.Sprintf("%s?%s", refererURL.Path, refererURL.RawQuery) } -//line cmd/gopcomm/community_yap.gox:567:1 +//line cmd/gopcomm/community_yap.gox:566:1 redirectURL := fmt.Sprintf("%s://%s/%s?origin_path=%s", refererURL.Scheme, refererURL.Host, "callback", url.QueryEscape(refererPath)) -//line cmd/gopcomm/community_yap.gox:569:1 +//line cmd/gopcomm/community_yap.gox:568:1 loginURL := this.community.RedirectToCasdoor(redirectURL) -//line cmd/gopcomm/community_yap.gox:570:1 +//line cmd/gopcomm/community_yap.gox:569:1 ctx.Redirect(loginURL, http.StatusFound) }) -//line cmd/gopcomm/community_yap.gox:573:1 +//line cmd/gopcomm/community_yap.gox:572:1 this.Get("/logout", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:574:1 +//line cmd/gopcomm/community_yap.gox:573:1 err := core.RemoveToken(ctx) -//line cmd/gopcomm/community_yap.gox:575:1 +//line cmd/gopcomm/community_yap.gox:574:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:576:1 +//line cmd/gopcomm/community_yap.gox:575:1 xLog.Error("remove token error:", err) } -//line cmd/gopcomm/community_yap.gox:579:1 +//line cmd/gopcomm/community_yap.gox:578:1 refererURL, err := url.Parse(ctx.Request.Referer()) -//line cmd/gopcomm/community_yap.gox:580:1 +//line cmd/gopcomm/community_yap.gox:579:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:581:1 +//line cmd/gopcomm/community_yap.gox:580:1 xLog.Info("Error parsing Referer: %v", err) -//line cmd/gopcomm/community_yap.gox:582:1 +//line cmd/gopcomm/community_yap.gox:581:1 return } -//line cmd/gopcomm/community_yap.gox:585:1 +//line cmd/gopcomm/community_yap.gox:584:1 refererPath := refererURL.Path -//line cmd/gopcomm/community_yap.gox:586:1 +//line cmd/gopcomm/community_yap.gox:585:1 if refererURL.RawQuery != "" { -//line cmd/gopcomm/community_yap.gox:587:1 +//line cmd/gopcomm/community_yap.gox:586:1 refererPath = fmt.Sprintf("%s?%s", refererURL.Path, refererURL.RawQuery) } -//line cmd/gopcomm/community_yap.gox:590:1 +//line cmd/gopcomm/community_yap.gox:589:1 http.Redirect(ctx.ResponseWriter, ctx.Request, refererPath, http.StatusFound) }) -//line cmd/gopcomm/community_yap.gox:593:1 +//line cmd/gopcomm/community_yap.gox:592:1 this.Get("/callback", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:594:1 +//line cmd/gopcomm/community_yap.gox:593:1 err := core.SetToken(ctx) -//line cmd/gopcomm/community_yap.gox:595:1 +//line cmd/gopcomm/community_yap.gox:594:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:596:1 +//line cmd/gopcomm/community_yap.gox:595:1 xLog.Error("set token error:", err) } -//line cmd/gopcomm/community_yap.gox:598:1 +//line cmd/gopcomm/community_yap.gox:597:1 origin_path := ctx.URL.Query().Get("origin_path") -//line cmd/gopcomm/community_yap.gox:599:1 +//line cmd/gopcomm/community_yap.gox:598:1 unurl, err := url.QueryUnescape(origin_path) -//line cmd/gopcomm/community_yap.gox:600:1 +//line cmd/gopcomm/community_yap.gox:599:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:601:1 +//line cmd/gopcomm/community_yap.gox:600:1 xLog.Info("Unurl error", err) -//line cmd/gopcomm/community_yap.gox:602:1 +//line cmd/gopcomm/community_yap.gox:601:1 unurl = "/" } -//line cmd/gopcomm/community_yap.gox:605:1 +//line cmd/gopcomm/community_yap.gox:604:1 http.Redirect(ctx.ResponseWriter, ctx.Request, unurl, http.StatusFound) }) -//line cmd/gopcomm/community_yap.gox:608:1 +//line cmd/gopcomm/community_yap.gox:607:1 conf := &core.Config{} -//line cmd/gopcomm/community_yap.gox:609:1 +//line cmd/gopcomm/community_yap.gox:608:1 this.community, _ = core.New(todo, conf) -//line cmd/gopcomm/community_yap.gox:610:1 +//line cmd/gopcomm/community_yap.gox:609:1 core.CasdoorConfigInit() -//line cmd/gopcomm/community_yap.gox:613:1 +//line cmd/gopcomm/community_yap.gox:612:1 this.Handle("/", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:614:1 +//line cmd/gopcomm/community_yap.gox:613:1 ctx.Yap__1("4xx", map[string]interface { }{}) }) -//line cmd/gopcomm/community_yap.gox:617:1 +//line cmd/gopcomm/community_yap.gox:616:1 xLog.Info("Started in endpoint: ", endpoint) -//line cmd/gopcomm/community_yap.gox:620:1 +//line cmd/gopcomm/community_yap.gox:619:1 this.Run(endpoint, func(h http.Handler) http.Handler { -//line cmd/gopcomm/community_yap.gox:622:1 +//line cmd/gopcomm/community_yap.gox:621:1 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { -//line cmd/gopcomm/community_yap.gox:623:1 +//line cmd/gopcomm/community_yap.gox:622:1 defer func() { -//line cmd/gopcomm/community_yap.gox:624:1 +//line cmd/gopcomm/community_yap.gox:623:1 if -//line cmd/gopcomm/community_yap.gox:624:1 +//line cmd/gopcomm/community_yap.gox:623:1 err := recover(); err != nil { -//line cmd/gopcomm/community_yap.gox:625:1 +//line cmd/gopcomm/community_yap.gox:624:1 xLog.Error(err) -//line cmd/gopcomm/community_yap.gox:626:1 +//line cmd/gopcomm/community_yap.gox:625:1 http.Redirect(w, r, "/failed", http.StatusFound) } }() -//line cmd/gopcomm/community_yap.gox:630:1 +//line cmd/gopcomm/community_yap.gox:629:1 h.ServeHTTP(w, r) }) }) } func main() { - yap.Gopt_App_Main(new(community)) + yap.Gopt_App_Main(new(community_yap)) } diff --git a/internal/core/media.go b/internal/core/media.go index 47a2852..4d6406a 100644 --- a/internal/core/media.go +++ b/internal/core/media.go @@ -3,6 +3,7 @@ package core import ( "context" "database/sql" + "encoding/json" "fmt" "io" "net/http" @@ -33,6 +34,7 @@ type File struct { Size int64 CreateAt time.Time UpdateAt time.Time + Duration *string } func (c *Community) DelMedias(ctx context.Context, userId string, ids []string) error { @@ -130,12 +132,21 @@ func (c *Community) SaveMedia(ctx context.Context, userId string, data []byte) ( if err != nil { return 0, err } + + var duration string = "" + if fileInfo.Format == "video/mp4" { + duration, err = GetVideoDuration(c.domain + fileKey + "?avinfo") + if err != nil { + return 0, err + } + } + fmt.Println(duration) // save - stem, err := c.db.Prepare(`insert into file (file_key,format,size,user_id,create_at,update_at) VALUES (?,?,?,?,?,?)`) + stem, err := c.db.Prepare(`insert into file (file_key,format,size,user_id,create_at,update_at,duration) VALUES (?,?,?,?,?,?,?)`) if err != nil { return 0, err } - res, err := stem.Exec(fileKey, fileInfo.Format, fileInfo.Size, userId, time.Now(), time.Now()) + res, err := stem.Exec(fileKey, fileInfo.Format, fileInfo.Size, userId, time.Now(), time.Now(), duration) if err != nil { return 0, err @@ -169,6 +180,45 @@ func (c *Community) getMediaInfo(fileKey string) (*File, error) { } +func GetVideoDuration(url string) (duration string, err error) { + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + type Format struct { + Duration string `json:"duration"` + } + var Data struct { + Format Format `json:"format"` + } + + err = json.Unmarshal(body, &Data) + + if err != nil { + fmt.Println(err.Error()) + return + } + duration = Data.Format.Duration + + decimalIndex := -1 + for i := 0; i < len(duration); i++ { + if duration[i] == '.' { + decimalIndex = i + break + } + } + if decimalIndex != -1 { + duration = duration[:decimalIndex+3] + } + return duration, nil +} + func (c *Community) uploadMedia(fileKey string, data []byte) error { w, err := c.bucket.NewWriter(context.Background(), fileKey, nil) @@ -267,9 +317,9 @@ func (c *Community) RetryCaptionGenerate(ctx context.Context, userId, videoId st return nil } -// func (c *Community) ListMediaByUserId(ctx context.Context, userId string, format string) ([]File, error) { -// sqlStr := "select * from file where user_id = ?" -func (c *Community) ListMediaByUserId(ctx context.Context, userId string, format string, from, limitInt int) ([]File, string, error) { +// func (c *Community) ListMediaByUserId(ctx context.Context, userId string, format string) ([]File, error) { +// sqlStr := "select * from file where user_id = ?" +func (c *Community) ListMediaByUserId(ctx context.Context, userId string, format string, page, limitInt int) ([]File, int, error) { sqlStr := "select * from file where user_id = ? " var args []any args = append(args, userId) @@ -279,30 +329,31 @@ func (c *Community) ListMediaByUserId(ctx context.Context, userId string, format sqlStr += " and format like ?" args = append(args, "%"+format+"%") } - sqlStr = sqlStr + " limit ? offset ?" + sqlStr = sqlStr + " order by create_at desc limit ? offset ?" args = append(args, limitInt) - args = append(args, from) + args = append(args, (page-1)*limitInt) + rows, err = c.db.Query(sqlStr, args...) var files []File if err != nil { - return files, MarkEnd, err + return files, 0, err } - var rowLen int for rows.Next() { var file File - if err := rows.Scan(&file.Id, &file.CreateAt, &file.UpdateAt, &file.FileKey, &file.Format, &file.UserId, &file.Size); err != nil { - return files, MarkEnd, err + if err := rows.Scan(&file.Id, &file.CreateAt, &file.UpdateAt, &file.FileKey, &file.Format, &file.UserId, &file.Size, &file.Duration); err != nil { + return files, 0, err } file.FileKey = c.domain + file.FileKey files = append(files, file) - rowLen++ + } - if rowLen == 0 { - return []File{}, MarkEnd, io.EOF + var count int + err = c.db.QueryRow("select count(*) from file where user_id = ? and format like ?", userId, "%"+format+"%").Scan(&count) + if err != nil { + return files, 0, err } - next := strconv.Itoa(from + rowLen) - return files, next, nil + return files, count, nil } func (c *Community) ListSubtitleByVideoId(ctx context.Context, videoId int) ([]VideoSubtitle, error) {