Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/michaelhu #281

Merged
merged 2 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ powerx

!internal/uc/powerx
!cmd/server/powerx.go
/plugins
/plugins
wechat
10 changes: 10 additions & 0 deletions api/admin/wechat/officialaccount/media.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ service PowerX {
@handler GetOAMedia
get /medias/:mediaId (GetOAMediaRequest) returns (GetOAMediaReply)

@doc "根据媒体key获取媒体"
@handler GetOAMediaByVideo
get /medias/video/:mediaId (GetOAMediaRequest) returns (GetOAMediaByVideoReply)


@doc "创建菜单"
@handler UploadOAMedia
Expand Down Expand Up @@ -79,6 +83,12 @@ type (
GetOAMediaReply struct {
OAMedia interface{} `json:"media"`
}

GetOAMediaByVideoReply struct {
Title string `json:"title"`
Description string `json:"description"`
DownUrl string `json:"down_url"`
}
)


Expand Down
28 changes: 28 additions & 0 deletions internal/handler/admin/crm/market/media/getmediabyvideohandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package media

import (
"net/http"

"PowerX/internal/logic/admin/crm/market/media"
"PowerX/internal/svc"
"PowerX/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

func GetMediaByVideoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetMediaRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := media.NewGetMediaByVideoLogic(r.Context(), svcCtx)
resp, err := l.GetMediaByVideo(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package media

import (
"net/http"

"PowerX/internal/logic/admin/wechat/officialaccount/media"
"PowerX/internal/svc"
"PowerX/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

func GetOAMediaByVideoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetOAMediaRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := media.NewGetOAMediaByVideoLogic(r.Context(), svcCtx)
resp, err := l.GetOAMediaByVideo(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
5 changes: 5 additions & 0 deletions internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions internal/logic/admin/crm/market/media/getmediabyvideologic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package media

import (
"context"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

type GetMediaByVideoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewGetMediaByVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMediaByVideoLogic {
return &GetMediaByVideoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *GetMediaByVideoLogic) GetMediaByVideo(req *types.GetMediaRequest) (resp *types.GetMediaReply, err error) {
// todo: add your logic here and delete this line

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package media

import (
"context"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

type GetOAMediaByVideoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewGetOAMediaByVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOAMediaByVideoLogic {
return &GetOAMediaByVideoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *GetOAMediaByVideoLogic) GetOAMediaByVideo(req *types.GetOAMediaRequest) (resp *types.GetOAMediaByVideoReply, err error) {
res, err := l.svcCtx.PowerX.WechatOA.App.Material.GetVideo(l.ctx, req.MediaId)

return &types.GetOAMediaByVideoReply{
Title: res.Title,
Description: res.Description,
DownUrl: res.DownUrl,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"PowerX/internal/svc"
"PowerX/internal/types"
"context"
"io"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -25,8 +26,15 @@ func NewGetOAMediaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOAM
func (l *GetOAMediaLogic) GetOAMedia(req *types.GetOAMediaRequest) (resp *types.GetOAMediaReply, err error) {

res, err := l.svcCtx.PowerX.WechatOA.App.Material.Get(l.ctx, req.MediaId)
defer res.Body.Close()

// 读取响应体
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

return &types.GetOAMediaReply{
OAMedia: res,
OAMedia: body,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ package media

import (
"PowerX/internal/logic/admin/mediaresource"
"PowerX/internal/svc"
"PowerX/internal/types"
"PowerX/internal/types/errorx"
fmt "PowerX/pkg/printx"
"PowerX/pkg/filex"
"context"
fmt2 "fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/material/response"
"github.com/zeromicro/go-zero/core/logx"
"io"
"net/http"
"os"
"time"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

type UploadOAMediaLogic struct {
Expand All @@ -43,7 +39,7 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO

paramValues := r.Form
mediaType := paramValues.Get("type")
fmt.Dump(mediaType)
//fmt.Dump(mediaType)
query := &object.StringMap{}
res := &response.ResponseMaterialAddMaterial{}
if mediaType == "video" {
Expand All @@ -60,7 +56,7 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO
}
}

file, _, err := r.FormFile("file")
file, handler, err := r.FormFile("file")
//fmt.Dump(handler.Filename)
if err != nil {
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error())
Expand All @@ -75,15 +71,12 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO

// 获取文件的临时目录和文件名
tempDir := os.TempDir()
tempFileName := fmt2.Sprintf("%d_*.jpg", time.Now().Unix())
if mediaType == "video" {
tempFileName = fmt2.Sprintf("%d_*.mp4", time.Now().Unix())
} else if mediaType == "voice" {
tempFileName = fmt2.Sprintf("%d_*.mp3", time.Now().Unix())
}
//extension := filex.GetFileExtension(handler.Filename)
//tempFileName := fmt2.Sprintf("%d_*.%s", time.Now().Unix(), extension)
tempFileName := handler.Filename

// 创建临时文件
tempFile, err := os.CreateTemp(tempDir, tempFileName)
tempFile, err := filex.CreateTempWithoutRandom(tempDir, tempFileName)
if err != nil {
return nil, errorx.WithCause(errorx.ErrBadRequest, "failed to create temporary file")
}
Expand Down
6 changes: 6 additions & 0 deletions internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions pkg/filex/filex.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,36 @@ func GetTempFilePath(fileName string) (string, error) {
filePath := filepath.Join(os.TempDir(), fileName)
return filePath, nil
}

func GetFileExtension(filename string) string {
ext := filepath.Ext(filename)
// 去掉后缀中的点号
return ext[1:]
}

// CreateTempWithoutRandom creates a new temporary file in the directory dir
// with the specified filename.
// If dir is the empty string, CreateTempWithoutRandom uses the default directory for temporary files, as returned by TempDir.
// The caller can use the file's Name method to find the pathname of the file.
// It is the caller's responsibility to remove the file when it is no longer needed.
func CreateTempWithoutRandom(dir, filename string) (*os.File, error) {
if dir == "" {
dir = os.TempDir()
}

path := filepath.Join(dir, filename)

// 先检查文件是否存在,存在则删除
if _, err := os.Stat(path); err == nil {
if err := os.Remove(path); err != nil {
return nil, err
}
}

f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return nil, &os.PathError{Op: "createtemp", Path: path, Err: err}
}

return f, nil
}
Loading