Skip to content

Commit

Permalink
Merge pull request #17 from IRONICBo/hotfix/fix-url
Browse files Browse the repository at this point in the history
Hotfix/fix url
  • Loading branch information
Baihhh committed Feb 7, 2024
2 parents da1f754 + be85e76 commit 93154ac
Show file tree
Hide file tree
Showing 25 changed files with 3,250 additions and 469 deletions.
5 changes: 4 additions & 1 deletion cmd/gopcomm/.env_temp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ QIU_TRANSLATION_KEY=
NIUTRANS_API_KEY=

# Casdoor config
GOP_CASDOOR_CERTIFICATE=
GOP_CASDOOR_CERTIFICATE=

# enabled yap debug model
YAP_DEBUG="true"
124 changes: 89 additions & 35 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"net/url"

"github.com/goplus/community/internal/core"
"github.com/goplus/community/markdown"
"github.com/goplus/community/translation"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
language "golang.org/x/text/language"
_ "github.com/joho/godotenv/autoload"
"github.com/qiniu/x/xlog"
language "golang.org/x/text/language"
)

var (
Expand All @@ -25,7 +21,7 @@ var (
)

const (
layoutUS = "January 2, 2006"
layoutUS = "January 2, 2006"
limitConst = 10
)

Expand Down Expand Up @@ -54,6 +50,10 @@ get "/demo", ctx => {
ctx.yap "demo", {}
}

get "/signin", ctx => {
ctx.yap "signin", {}
}

get "/p/:id", ctx => {
// todo middleware
// Get User Info
Expand Down Expand Up @@ -120,6 +120,24 @@ get "/user/:id", ctx => {
}
}

get "/userEdit", ctx => {
var user *core.User
token, err := core.GetToken(ctx)
if err == nil {
user, err = community.GetUser(token.Value)
if err != nil {
xLog.Error("get user error:", err)
}
}

if err != nil {
http.Redirect(ctx.ResponseWriter, ctx.Request, "/error", http.StatusTemporaryRedirect)
}
ctx.yap "user_edit", {
"User": user,
}
}

get "/add", ctx => {
ctx.yap "edit", {}
}
Expand Down Expand Up @@ -152,21 +170,21 @@ get "/delete", ctx => {
get "/medias", ctx => {
format := ctx.param("format")
uid := ctx.param("uid")
files, err :=community.ListMediaByUserId(todo, uid, format)
files, err := community.ListMediaByUserId(todo, uid, format)
if err != nil {
ctx.json {
"code": 0,
"err": "get media failed",
}
} else {
ctx.json {
"code": 200,
"code": 200,
"items": files,
}
}
}

get "/delMedia", ctx =>{
get "/delMedia", ctx => {
id := ctx.param("id")
token, err := core.GetToken(ctx)
uid, err := community.ParseJwtToken(token.Value)
Expand Down Expand Up @@ -200,37 +218,37 @@ get "/", ctx => {
user, err = community.GetUser(token.Value)
if err != nil {
xLog.Error("get user error:", err)
}else{
} else {
userId = user.Id
}
}
// Get Article Info
articles, next, _ := community.ListArticle(todo, core.MarkBegin, limitConst, "")
articlesJson, _ := json.Marshal(&articles)
ctx.yap "home", {
"UserId": userId,
"User": user,
"Items": strings.Replace(string(articlesJson), `\"`, `"`, -1),
"Next": next,
"UserId": userId,
"User": user,
"Items": strings.Replace(string(articlesJson), `\"`, `"`, -1),
"Next": next,
}
}

get "/get", ctx => {
from := ctx.param("from")
limit := ctx.param("limit")
searchValue := ctx.param("value")

limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = limitConst
}
// Get Article Info
articles, next, _ := community.ListArticle(todo, from, limitInt, searchValue)
ctx.json {
"code": 200,
"items": articles,
"next": next,
"value": searchValue,
"code": 200,
"items": articles,
"next": next,
"value": searchValue,
}
}

Expand All @@ -251,19 +269,19 @@ get "/search", ctx => {
user, err = community.GetUser(token.Value)
if err != nil {
xLog.Error("get user error:", err)
}else{
} else {
userId = user.Id
}
}

articles, next, _ := community.ListArticle(todo, core.MarkBegin, limitConst, searchValue)
articlesJson, _ := json.Marshal(&articles)
ctx.yap "home", {
"UserId": userId,
"User": user,
"Items": strings.Replace(string(articlesJson), `\"`, `"`, -1),
"Value": searchValue,
"Next": next,
"UserId": userId,
"User": user,
"Items": strings.Replace(string(articlesJson), `\"`, `"`, -1),
"Value": searchValue,
"Next": next,
}
}

Expand Down Expand Up @@ -376,7 +394,7 @@ post "/translate", ctx => {
htmlData := ctx.param("html")
id := ctx.param("id")
// get translation markdown
transData, err := trans.translateMarkdownText(mdData, language.Chinese, language.English)
transData, err := community.TranslateMarkdownText(todo, mdData, language.Chinese, language.English)
if err != nil {
ctx.json {
"code": 500,
Expand Down Expand Up @@ -415,14 +433,52 @@ get "/getMediaUrl/:id", ctx => {
}
}

get "/getVideoAndSubtitle/:id", ctx => {
id := ctx.param("id")
fileKey, err := community.GetMediaUrl(todo, id)
m:=make(map[string]string,2)
format,err:=community.GetMediaType(todo,id)
if err!=nil {
ctx.json {
"code": 500,
"err": err.Error(),
}
}
if format == "video/mp4" {
subtitle,err:=community.GetVideoSubtitle(todo,id)
if err!=nil{
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
}
}
return
}
m["subtitle"] = domain+subtitle
}
htmlUrl := fmt.Sprintf("%s%s", domain, fileKey)
if err != nil {
ctx.json {
"code": 500,
"err": "have no html media",
}
}
m["fileKey"] = htmlUrl
ctx.json {
"code": 200,
"url": m,
}
}

post "/upload", ctx => {
core.UploadFile(ctx, community)
community.UploadFile(ctx)
}

get "/login", ctx => {
// Get URL from query string
// redirectURL := ctx.URL.Query().Get("redirect_url")
// Get current request page URL from
// Get current request page URL from
// Concatenate the current request page URL from refer
refererURL, err := url.Parse(ctx.Request.Referer())
if err != nil {
Expand All @@ -432,16 +488,15 @@ get "/login", ctx => {

refererPath := refererURL.Path
if refererURL.RawQuery != "" {
refererPath = fmt.Sprintf("%s?%s",refererURL.Path, refererURL.RawQuery)
refererPath = fmt.Sprintf("%s?%s", refererURL.Path, refererURL.RawQuery)
}

redirectURL := fmt.Sprintf("%s://%s/%s?origin_path=%s",refererURL.Scheme, refererURL.Host, "callback", url.QueryEscape(refererPath))
redirectURL := fmt.Sprintf("%s://%s/%s?origin_path=%s", refererURL.Scheme, refererURL.Host, "callback", url.QueryEscape(refererPath))

loginURL := community.RedirectToCasdoor(redirectURL)
ctx.Redirect loginURL, http.StatusFound
}


get "/logout", ctx => {
err := core.RemoveToken(ctx)
if err != nil {
Expand All @@ -456,7 +511,7 @@ get "/logout", ctx => {

refererPath := refererURL.Path
if refererURL.RawQuery != "" {
refererPath = fmt.Sprintf("%s?%s",refererURL.Path, refererURL.RawQuery)
refererPath = fmt.Sprintf("%s?%s", refererURL.Path, refererURL.RawQuery)
}

http.Redirect(ctx.ResponseWriter, ctx.Request, refererPath, http.StatusFound)
Expand All @@ -479,11 +534,10 @@ get "/callback", ctx => {

conf := &core.Config{}
community, _ = core.New(todo, conf)
trans = translation.New(os.Getenv("NIUTRANS_API_KEY"), "", "")
core.CasdoorConfigInit()

// 404
handle "/",ctx => {
handle "/", ctx => {
ctx.yap "4xx", {}
}

Expand Down
Loading

0 comments on commit 93154ac

Please sign in to comment.