Skip to content

Commit 519de90

Browse files
committed
feat(chesed): impl UploadImage
1 parent 53c6bb9 commit 519de90

53 files changed

Lines changed: 8901 additions & 118 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/sephirah/cmd/sephirah/wire_gen.go

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/sephirah/internal/biz/biz.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package biz
33
import (
44
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizangela"
55
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizbinah"
6+
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizchesed"
67
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizgebura"
78
"github.com/tuihub/librarian/app/sephirah/internal/biz/biznetzach"
89
"github.com/tuihub/librarian/app/sephirah/internal/biz/biztiphereth"
@@ -17,7 +18,8 @@ var ProviderSet = wire.NewSet(
1718
biztiphereth.NewTiphereth,
1819
bizgebura.NewGebura,
1920
bizbinah.NewBinah,
20-
bizbinah.NewCallbackControl,
21+
bizbinah.NewControlBlock,
2122
bizyesod.NewYesod,
2223
biznetzach.NewNetzach,
24+
bizchesed.ProviderSet,
2325
)

app/sephirah/internal/biz/bizbinah/binah.go

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,87 @@ import (
44
"context"
55
"io"
66
"os"
7+
"strconv"
78

9+
"github.com/tuihub/librarian/app/sephirah/internal/model/modelbinah"
810
"github.com/tuihub/librarian/internal/lib/libauth"
11+
"github.com/tuihub/librarian/internal/model"
912
mapper "github.com/tuihub/protos/pkg/librarian/mapper/v1"
1013
porter "github.com/tuihub/protos/pkg/librarian/porter/v1"
1114
searcher "github.com/tuihub/protos/pkg/librarian/searcher/v1"
1215
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
1316

1417
"github.com/go-kratos/kratos/v2/errors"
18+
"github.com/google/uuid"
1519
)
1620

1721
type UploadFile struct {
18-
callback UploadCallbackFunc
22+
id model.InternalID
23+
porter porter.LibrarianPorterServiceClient
24+
callback modelbinah.UploadCallbackFunc
1925
file *os.File
2026
Writer io.Writer
2127
}
2228

23-
func (f *UploadFile) Finish() error {
24-
return f.callback(f)
29+
func (f *UploadFile) Finish(ctx context.Context) error {
30+
cli, err := f.porter.PushData(ctx)
31+
if err != nil {
32+
return err
33+
}
34+
err = cli.Send(&porter.PushDataRequest{
35+
Content: &porter.PushDataRequest_Metadata{
36+
Metadata: &porter.PushDataRequest_DataMeta{
37+
Source: porter.DataSource_DATA_SOURCE_INTERNAL_DEFAULT,
38+
ContentId: strconv.FormatInt(int64(f.id), 10),
39+
},
40+
},
41+
})
42+
if err != nil {
43+
return err
44+
}
45+
buf := make([]byte, 16<<10) //nolint:gomnd //TODO
46+
for {
47+
_, err = f.file.Read(buf)
48+
if errors.Is(err, io.EOF) {
49+
_, err = cli.CloseAndRecv()
50+
if err != nil {
51+
return err
52+
}
53+
break
54+
}
55+
if err != nil {
56+
return err
57+
}
58+
err = cli.Send(&porter.PushDataRequest{
59+
Content: &porter.PushDataRequest_Data{
60+
Data: buf,
61+
},
62+
})
63+
if err != nil {
64+
return err
65+
}
66+
}
67+
return f.callback()
2568
}
2669

2770
type BinahRepo interface {
2871
}
2972

3073
type Binah struct {
31-
callback CallbackControlBlock
74+
callback *modelbinah.ControlBlock
3275
auth *libauth.Auth
3376
mapper mapper.LibrarianMapperServiceClient
3477
porter porter.LibrarianPorterServiceClient
3578
searcher searcher.LibrarianSearcherServiceClient
3679
}
3780

38-
func NewBinah(callback CallbackControlBlock, auth *libauth.Auth, mClient mapper.LibrarianMapperServiceClient,
39-
pClient porter.LibrarianPorterServiceClient, sClient searcher.LibrarianSearcherServiceClient) *Binah {
81+
func NewBinah(
82+
callback *modelbinah.ControlBlock,
83+
auth *libauth.Auth,
84+
mClient mapper.LibrarianMapperServiceClient,
85+
pClient porter.LibrarianPorterServiceClient,
86+
sClient searcher.LibrarianSearcherServiceClient,
87+
) *Binah {
4088
return &Binah{
4189
callback: callback,
4290
auth: auth,
@@ -46,25 +94,31 @@ func NewBinah(callback CallbackControlBlock, auth *libauth.Auth, mClient mapper.
4694
}
4795
}
4896

49-
func (b *Binah) getUploadCallback(id UploadCallbackID) UploadCallbackFunc {
50-
f, exist := b.callback.uploadCallbackMap[id]
51-
if exist {
52-
return f
53-
}
54-
return emptyUploadCallback
97+
func NewControlBlock(a *libauth.Auth) *modelbinah.ControlBlock {
98+
return modelbinah.NewControlBlock(a)
5599
}
56100

57101
func (b *Binah) NewUploadFile(ctx context.Context) (*UploadFile, *errors.Error) {
58102
claims, exist := libauth.FromContext(ctx)
59103
if !exist || claims == nil || claims.TransferMetadata == nil {
60104
return nil, pb.ErrorErrorReasonUnauthorized("token required")
61105
}
62-
f, err := os.CreateTemp("", claims.TransferMetadata.Name)
106+
callback, err := b.callback.GetUploadCallback(ctx)
107+
if err != nil {
108+
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
109+
}
110+
meta, err := b.callback.GetUploadFileMetadata(ctx)
111+
if err != nil {
112+
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
113+
}
114+
f, err := os.CreateTemp("", uuid.NewString())
63115
if err != nil {
64116
return nil, pb.ErrorErrorReasonUnspecified("create temp file failed")
65117
}
66118
return &UploadFile{
67-
callback: b.getUploadCallback(UploadCallbackID(claims.TransferMetadata.CallBack)),
119+
id: meta.ID,
120+
porter: b.porter,
121+
callback: callback,
68122
file: f,
69123
Writer: f,
70124
}, nil

app/sephirah/internal/biz/bizbinah/callback.go

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package bizchesed
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/tuihub/librarian/app/sephirah/internal/model/converter"
8+
"github.com/tuihub/librarian/app/sephirah/internal/model/modelbinah"
9+
"github.com/tuihub/librarian/app/sephirah/internal/model/modelchesed"
10+
"github.com/tuihub/librarian/internal/lib/libauth"
11+
"github.com/tuihub/librarian/internal/lib/libcache"
12+
"github.com/tuihub/librarian/internal/lib/libtime"
13+
"github.com/tuihub/librarian/internal/model"
14+
mapper "github.com/tuihub/protos/pkg/librarian/mapper/v1"
15+
porter "github.com/tuihub/protos/pkg/librarian/porter/v1"
16+
searcher "github.com/tuihub/protos/pkg/librarian/searcher/v1"
17+
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
18+
19+
"github.com/go-kratos/kratos/v2/errors"
20+
"github.com/google/wire"
21+
)
22+
23+
var ProviderSet = wire.NewSet(
24+
NewChesed,
25+
NewImageCache,
26+
)
27+
28+
type ChesedRepo interface {
29+
}
30+
31+
type Chesed struct {
32+
// repo ChesedRepo
33+
mapper mapper.LibrarianMapperServiceClient
34+
searcher searcher.LibrarianSearcherServiceClient
35+
porter porter.LibrarianPorterServiceClient
36+
upload *modelbinah.UploadCallBack
37+
imageCache *libcache.Map[model.InternalID, modelchesed.Image]
38+
}
39+
40+
func NewChesed(
41+
// repo ChesedRepo,
42+
mClient mapper.LibrarianMapperServiceClient,
43+
pClient porter.LibrarianPorterServiceClient,
44+
sClient searcher.LibrarianSearcherServiceClient,
45+
block *modelbinah.ControlBlock,
46+
imageCache *libcache.Map[model.InternalID, modelchesed.Image],
47+
) (*Chesed, error) {
48+
upload := block.RegisterUploadCallback(
49+
modelbinah.UploadArtifacts,
50+
UploadImageCallback,
51+
)
52+
y := &Chesed{
53+
//repo: repo,
54+
mapper: mClient,
55+
porter: pClient,
56+
searcher: sClient,
57+
upload: upload,
58+
imageCache: imageCache,
59+
}
60+
return y, nil
61+
}
62+
63+
func NewImageCache(
64+
store libcache.Store,
65+
) *libcache.Map[model.InternalID, modelchesed.Image] {
66+
return libcache.NewMap[model.InternalID, modelchesed.Image](
67+
store,
68+
"Images",
69+
func(k model.InternalID) string {
70+
return strconv.FormatInt(int64(k), 10)
71+
},
72+
nil,
73+
libcache.WithExpiration(libtime.Day),
74+
)
75+
}
76+
77+
func (c *Chesed) UploadImage(ctx context.Context, image modelchesed.Image,
78+
metadata modelbinah.FileMetadata) (string, *errors.Error) {
79+
if !libauth.FromContextAssertUserType(ctx, libauth.UserTypeAdmin, libauth.UserTypeNormal) {
80+
return "", pb.ErrorErrorReasonForbidden("no permission")
81+
}
82+
if err := metadata.Check(); err != nil {
83+
return "", pb.ErrorErrorReasonBadRequest("invalid file metadata: %s", err.Error())
84+
}
85+
resp, err := c.searcher.NewID(ctx, &searcher.NewIDRequest{})
86+
if err != nil {
87+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
88+
}
89+
image.ID = converter.ToBizInternalID(resp.Id)
90+
metadata.ID = converter.ToBizInternalID(resp.Id)
91+
err = c.imageCache.Set(ctx, image.ID, &image)
92+
if err != nil {
93+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
94+
}
95+
token, err := c.upload.GenerateUploadToken(ctx, metadata, libtime.HalfDay)
96+
if err != nil {
97+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
98+
}
99+
return token, nil
100+
}
101+
102+
func UploadImageCallback() error {
103+
panic("not impl")
104+
}

app/sephirah/internal/biz/bizgebura/gebura.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bizgebura
33
import (
44
"context"
55

6-
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizbinah"
76
"github.com/tuihub/librarian/app/sephirah/internal/model/modelgebura"
87
"github.com/tuihub/librarian/internal/lib/libauth"
98
"github.com/tuihub/librarian/internal/model"
@@ -52,18 +51,9 @@ type Gebura struct {
5251
func NewGebura(
5352
repo GeburaRepo,
5453
auth *libauth.Auth,
55-
block bizbinah.CallbackControlBlock,
5654
mClient mapper.LibrarianMapperServiceClient,
5755
pClient porter.LibrarianPorterServiceClient,
5856
sClient searcher.LibrarianSearcherServiceClient,
5957
) *Gebura {
60-
block.RegisterUploadCallback(bizbinah.UploadCallback{
61-
ID: bizbinah.UploadArtifacts,
62-
Func: UploadArtifactsCallback,
63-
})
6458
return &Gebura{auth: auth, repo: repo, mapper: mClient, porter: pClient, searcher: sClient}
6559
}
66-
67-
func UploadArtifactsCallback(file *bizbinah.UploadFile) error {
68-
panic("not impl")
69-
}

0 commit comments

Comments
 (0)