feat: add upload logics
This commit is contained in:
@@ -2,6 +2,10 @@ package internal
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"exporter/config"
|
"exporter/config"
|
||||||
"exporter/database/telegram_resource/public/model"
|
"exporter/database/telegram_resource/public/model"
|
||||||
@@ -23,13 +27,22 @@ func PublishCmd() *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type publishMsg struct {
|
||||||
|
model.ChannelMessages
|
||||||
|
model.Channels
|
||||||
|
}
|
||||||
|
|
||||||
func publishCmd(ctx context.Context) error {
|
func publishCmd(ctx context.Context) error {
|
||||||
var msg model.ChannelMessages
|
var msg publishMsg
|
||||||
|
|
||||||
tbl := table.ChannelMessages
|
tbl := table.ChannelMessages
|
||||||
err := tbl.SELECT(tbl.AllColumns).WHERE(tbl.Published.IS_TRUE()).LIMIT(1).QueryContext(ctx, db, &msg)
|
tblC := table.Channels
|
||||||
if err != nil {
|
stmt := tbl.SELECT(tbl.AllColumns, tblC.Title).
|
||||||
return err
|
WHERE(tbl.Published.IS_FALSE()).
|
||||||
|
LIMIT(1).
|
||||||
|
FROM(tbl.LEFT_JOIN(tblC, tbl.ChannelID.EQ(tblC.UUID)))
|
||||||
|
if err := stmt.QueryContext(context.Background(), db, &msg); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to get message")
|
||||||
}
|
}
|
||||||
|
|
||||||
// publish item
|
// publish item
|
||||||
@@ -37,7 +50,7 @@ func publishCmd(ctx context.Context) error {
|
|||||||
return errors.Wrap(err, "failed to publish")
|
return errors.Wrap(err, "failed to publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tbl.UPDATE().SET(tbl.Published.SET(Bool(true))).WHERE(tbl.ID.EQ(Int(msg.ID))).ExecContext(ctx, db)
|
_, err := tbl.UPDATE().SET(tbl.Published.SET(Bool(true))).WHERE(tbl.ID.EQ(Int64(msg.ChannelMessages.ID))).ExecContext(ctx, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -45,14 +58,48 @@ func publishCmd(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func publish(ctx context.Context, msg model.ChannelMessages) error {
|
func publish(ctx context.Context, msg publishMsg) error {
|
||||||
|
memos.InitClient(config.C.PublishHost, config.C.PublishToken)
|
||||||
|
|
||||||
data := memos.PostData{
|
data := memos.PostData{
|
||||||
Host: config.C.PublishHost,
|
Content: *msg.ChannelMessages.Content,
|
||||||
Token: config.C.PublishToken,
|
ChannelID: msg.ChannelMessages.ChannelID,
|
||||||
Content: *msg.Content,
|
ChannelTitle: msg.Channels.Title,
|
||||||
ChannelID: msg.ChannelID,
|
|
||||||
ChannelTitle: "",
|
|
||||||
Resources: []memos.Resource{},
|
Resources: []memos.Resource{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var medias []ChannelMessageMedia
|
||||||
|
if err := json.Unmarshal([]byte(msg.ChannelMessages.Media), &medias); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to unmarshal media")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, media := range medias {
|
||||||
|
res := memos.Resource{}
|
||||||
|
|
||||||
|
var file string
|
||||||
|
if media.Photo != nil {
|
||||||
|
res.Type = "image/jpg"
|
||||||
|
res.Name = *media.Photo
|
||||||
|
file = fmt.Sprintf("%d/%s", data.ChannelID, *media.Photo)
|
||||||
|
} else if media.Document != nil {
|
||||||
|
res.Type = media.Document.MimeType
|
||||||
|
res.Name = media.Document.Filename
|
||||||
|
file = fmt.Sprintf("%d/%d%s", data.ChannelID, media.AssetID, media.Document.Ext)
|
||||||
|
}
|
||||||
|
filepath := filepath.Join(config.C.Output, file)
|
||||||
|
b, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
res.Content = b
|
||||||
|
|
||||||
|
resource, err := memos.Upload(res)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
data.Resources = append(data.Resources, resource)
|
||||||
|
}
|
||||||
|
|
||||||
return memos.Post(data)
|
return memos.Post(data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"exporter/database/telegram_resource/public/model"
|
||||||
"exporter/database/telegram_resource/public/table"
|
"exporter/database/telegram_resource/public/table"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
@@ -56,12 +57,23 @@ func Test_Join(t *testing.T) {
|
|||||||
if err := InitDB(dsn); err != nil {
|
if err := InitDB(dsn); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
// db.Exec(`truncate channel_messages`)
|
||||||
|
|
||||||
|
var msg struct {
|
||||||
|
model.ChannelMessages
|
||||||
|
model.Channels
|
||||||
|
}
|
||||||
tblC := table.Channels
|
tblC := table.Channels
|
||||||
tbl := table.ChannelMessages
|
tbl := table.ChannelMessages
|
||||||
stmt := tbl.SELECT(tbl.AllColumns, tblC.Title).
|
stmt := tbl.SELECT(tbl.AllColumns, tblC.Title).
|
||||||
WHERE(tbl.Published.IS_TRUE()).
|
WHERE(tbl.Published.IS_FALSE()).
|
||||||
LIMIT(1).
|
LIMIT(1).
|
||||||
FROM(tbl.LEFT_JOIN(tblC, tbl.ChannelID.EQ(tblC.UUID)))
|
FROM(tbl.LEFT_JOIN(tblC, tbl.ChannelID.EQ(tblC.UUID)))
|
||||||
t.Log(stmt.DebugSql())
|
t.Log(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(context.Background(), db, &msg); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("%+v", msg)
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -42,6 +42,8 @@ func main() {
|
|||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
internal.LoginCmd(),
|
internal.LoginCmd(),
|
||||||
internal.ExportCmd(),
|
internal.ExportCmd(),
|
||||||
|
internal.PublishCmd(),
|
||||||
|
internal.WatchCmd(),
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd.SilenceErrors = true
|
// rootCmd.SilenceErrors = true
|
||||||
|
|||||||
@@ -22,35 +22,22 @@ type Memo struct {
|
|||||||
Uid string `json:"uid,omitempty"` // resp
|
Uid string `json:"uid,omitempty"` // resp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Host string
|
||||||
|
// Token string
|
||||||
type PostData struct {
|
type PostData struct {
|
||||||
Host string
|
|
||||||
Token string
|
|
||||||
Content string
|
Content string
|
||||||
ChannelID int64
|
ChannelID int64
|
||||||
ChannelTitle string
|
ChannelTitle string
|
||||||
Resources []Resource
|
Resources []Resource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var client *req.Client
|
||||||
|
|
||||||
|
func InitClient(host, token string) {
|
||||||
|
client = req.C().EnableInsecureSkipVerify().SetBaseURL(host).SetCommonBearerAuthToken(token)
|
||||||
|
}
|
||||||
|
|
||||||
func Post(pd PostData) error {
|
func Post(pd PostData) error {
|
||||||
client := req.C().EnableInsecureSkipVerify().SetBaseURL(pd.Host).SetCommonBearerAuthToken(pd.Token)
|
|
||||||
|
|
||||||
resources := []Resource{}
|
|
||||||
|
|
||||||
for _, res := range pd.Resources {
|
|
||||||
var result Resource
|
|
||||||
|
|
||||||
resp, err := client.R().SetSuccessResult(&result).SetBodyJsonMarshal(res).Post("/api/v1/resources")
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "post resource failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !resp.IsSuccessState() {
|
|
||||||
return errors.New("post memo failed, body: " + resp.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
resources = append(resources, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
var createMemoResp Memo
|
var createMemoResp Memo
|
||||||
|
|
||||||
resp, err := client.R().
|
resp, err := client.R().
|
||||||
@@ -73,7 +60,7 @@ func Post(pd PostData) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp, err = client.R().
|
resp, err = client.R().
|
||||||
SetBodyJsonMarshal(Resources{Resources: resources}).
|
SetBodyJsonMarshal(Resources{Resources: pd.Resources}).
|
||||||
Patch(fmt.Sprintf("/api/v1/%s/resources", createMemoResp.Name))
|
Patch(fmt.Sprintf("/api/v1/%s/resources", createMemoResp.Name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "patch resources failed")
|
return errors.Wrap(err, "patch resources failed")
|
||||||
@@ -85,3 +72,18 @@ func Post(pd PostData) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Upload(res Resource) (Resource, error) {
|
||||||
|
var result Resource
|
||||||
|
|
||||||
|
resp, err := client.R().SetSuccessResult(&result).SetBodyJsonMarshal(res).Post("/api/v1/resources")
|
||||||
|
if err != nil {
|
||||||
|
return Resource{}, errors.Wrap(err, "post resource failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resp.IsSuccessState() {
|
||||||
|
return Resource{}, errors.New("post memo failed, body: " + resp.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user