Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/xhyqaq/community into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyqaq committed Jan 19, 2024
2 parents 2fe7204 + 3d16579 commit 0fa789b
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
59 changes: 56 additions & 3 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import (
"context"
"net/http"

"github.com/goplus/community/internal/core"
)
Expand Down Expand Up @@ -46,14 +47,66 @@ post "/commit", ctx => {
// ...
}

get "/getMedia",ctx => {
mediaId:=ctx.param("id")
get "/getMedia", ctx => {
mediaId := ctx.param("id")
// todo the err not empty return 404 for image
fileKey,_:=community.GetMediaUrl(ctx,mediaId)
fileKey, _ := community.GetMediaUrl(ctx, mediaId)

http.Redirect(ctx.ResponseWriter, ctx.Request, "qiniu demain"+fileKey, http.StatusTemporaryRedirect)
}

post "/upload", ctx => {
file, header, err := ctx.FormFile("file")
filename := header.Filename

ctx.ParseMultipartForm(10 << 20)

if err != nil {
log.Fatalln("upload file error:", filename)
ctx.JSON(500, err.Error())
return
}


dst, err := os.Create(filename)
if err != nil {
log.Fatalln("create file error:", file)
ctx.JSON(500, err.Error())
return
}
defer func() {
file.Close()
dst.Close()
err = os.Remove(filename)
if err != nil {
log.Fatalln("delete file error:", filename)
return
}
}()


_, err = io.Copy(dst, file)
if err != nil {
log.Fatalln("copy file errer:", filename)
ctx.JSON(500, err.Error())
return
}
bytes, err := os.ReadFile(filename)
if err != nil {
log.Fatalln("read file errer:", filename)
ctx.JSON(500, err.Error())
return
}
cookie, err := ctx.Request.Cookie("user_id")
if err != nil {
log.Fatalln("token不存在")
ctx.JSON(500, err.Error())
return
}

community.SaveMedia(context.Background(),cookie.Value,bytes)
}

conf := &core.Config{}
community, _ = core.New(todo, conf)

Expand Down
75 changes: 75 additions & 0 deletions internal/core/media_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package core_test

import (
"context"
"log"
"os"
"testing"

"github.com/goplus/community/internal/core"
)

var c *core.Community

func TestMain(t *testing.M) {
config := &core.Config{
Driver: "mysql",
DSN: "",
BlobUS: "",
}
ret, err := core.New(context.Background(), config)
if err != nil {
log.Fatalln(err.Error())
return
}
c = ret
t.Run()
}

func TestGetMediaUrl(t *testing.T) {
url, err := c.GetMediaUrl(context.Background(), "10")
if err != nil {
log.Fatalln(err.Error())
return
}
log.Println(url)
}

func TestSaveMedia(t *testing.T) {
data, err := readFileByte("xxx")
if err != nil {
log.Fatalln(err.Error())
return
}
id, err := c.SaveMedia(context.Background(), "1", data)
if err != nil {
log.Fatalln(err.Error())
return
}
log.Println("id:", id)

}

func TestDelMeida(t *testing.T) {
err := c.DelMedia(context.Background(), "1", "10")
if err != nil {
log.Fatalln(err.Error())
return
}
log.Println(err.Error())
}

func TestDelMedias(t *testing.T) {
err := c.DelMedias(context.Background(), "1", []string{"1"})
if err != nil {

log.Fatalln(err.Error())
return
}

}

func readFileByte(filePath string) ([]byte, error) {
data, err := os.ReadFile("sss")
return data, err
}

0 comments on commit 0fa789b

Please sign in to comment.