Skip to content

Commit f6e508f

Browse files
committed
feat(chesed): impl ListImages DownloadImage
1 parent 4b80aa9 commit f6e508f

23 files changed

Lines changed: 321 additions & 56 deletions

File tree

app/searcher/internal/biz/searcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type SearcherRepo interface {
1010
NewID(context.Context) (int64, error)
1111
DescribeID(context.Context, model.InternalID, string) error
12-
SearchID(context.Context, string) ([]*SearchResult, error)
12+
SearchID(context.Context, model.Paging, string) ([]*SearchResult, error)
1313
}
1414

1515
type Searcher struct {
@@ -45,6 +45,6 @@ func (g *Searcher) DescribeID(ctx context.Context, id model.InternalID, descript
4545
return g.repo.DescribeID(ctx, id, description)
4646
}
4747

48-
func (g *Searcher) SearchID(ctx context.Context, keyword string) ([]*SearchResult, error) {
49-
return g.repo.SearchID(ctx, keyword)
48+
func (g *Searcher) SearchID(ctx context.Context, paging model.Paging, keyword string) ([]*SearchResult, error) {
49+
return g.repo.SearchID(ctx, paging, keyword)
5050
}

app/searcher/internal/data/bleve.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ func (r *bleveSearcherRepo) DescribeID(ctx context.Context, id model.InternalID,
5858
return nil
5959
}
6060

61-
func (r *bleveSearcherRepo) SearchID(ctx context.Context, keyword string) ([]*biz.SearchResult, error) {
61+
func (r *bleveSearcherRepo) SearchID(ctx context.Context, paging model.Paging, keyword string) (
62+
[]*biz.SearchResult, error) {
6263
query := bleve.NewFuzzyQuery(keyword)
6364
search := bleve.NewSearchRequest(query)
65+
search.From = paging.ToOffset()
66+
search.Size = paging.ToLimit()
6467
result, err := r.search.Search(search)
6568
if err != nil {
6669
return nil, err

app/searcher/internal/data/bleve_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ func Test_bleveSearcherRepo_SearchID(t *testing.T) {
7171
t.Errorf("DescribeID() error = %v", err)
7272
}
7373
}
74-
ids, err := r.SearchID(context.Background(), "your")
74+
ids, err := r.SearchID(context.Background(), model.Paging{
75+
PageSize: 10,
76+
PageNum: 1,
77+
}, "your")
7578
if err != nil {
7679
t.Errorf("SearchID() error = %v", err)
7780
}

app/searcher/internal/data/meili.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,25 @@ func (m *meiliSearcherRepo) DescribeID(ctx context.Context, id model.InternalID,
6262
return nil
6363
}
6464

65-
func (m *meiliSearcherRepo) SearchID(ctx context.Context, keyword string) ([]*biz.SearchResult, error) {
65+
func (m *meiliSearcherRepo) SearchID(ctx context.Context, paging model.Paging, keyword string) (
66+
[]*biz.SearchResult, error) {
6667
request := &meilisearch.SearchRequest{ //nolint:exhaustruct //TODO
67-
Limit: 20, //nolint:gomnd // TODO
68+
Limit: int64(paging.ToLimit()),
69+
Offset: int64(paging.ToOffset()),
6870
}
69-
result, err := m.search.Index(IndexName).Search(keyword, request)
71+
// https://github.com/meilisearch/meilisearch-go/issues/406
72+
resultRaw, err := m.search.Index(IndexName).SearchRaw(keyword, request)
73+
if err != nil {
74+
return nil, err
75+
}
76+
resultStr, err := resultRaw.MarshalJSON()
77+
if err != nil {
78+
return nil, err
79+
}
80+
result := struct {
81+
Hits []document
82+
}{}
83+
err = libcodec.Unmarshal(libcodec.JSON, resultStr, &result)
7084
if err != nil {
7185
return nil, err
7286
}

app/searcher/internal/service/librariansearcherservice.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ func (s *LibrarianSearcherServiceService) DescribeID(ctx context.Context, req *p
5656
}
5757
func (s *LibrarianSearcherServiceService) SearchID(ctx context.Context, req *pb.SearchIDRequest) (
5858
*pb.SearchIDResponse, error) {
59-
res, err := s.uc.SearchID(ctx, req.GetKeyword())
59+
res, err := s.uc.SearchID(ctx, model.Paging{
60+
PageSize: int(req.GetPaging().GetPageSize()),
61+
PageNum: int(req.GetPaging().GetPageNum()),
62+
}, req.GetKeyword())
6063
if err != nil {
6164
return nil, err
6265
}
@@ -67,5 +70,8 @@ func (s *LibrarianSearcherServiceService) SearchID(ctx context.Context, req *pb.
6770
Rank: res[i].Rank,
6871
}
6972
}
70-
return &pb.SearchIDResponse{Result: result}, nil
73+
return &pb.SearchIDResponse{
74+
Paging: nil,
75+
Result: result,
76+
}, nil
7177
}

app/searcher/internal/service/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (m *mockedSearcherRepo) DescribeID(ctx context.Context, id model.InternalID
2828
panic("implement me")
2929
}
3030

31-
func (m *mockedSearcherRepo) SearchID(ctx context.Context, s string) ([]*biz.SearchResult, error) {
31+
func (m *mockedSearcherRepo) SearchID(ctx context.Context, p model.Paging, s string) ([]*biz.SearchResult, error) {
3232
// TODO implement me
3333
panic("implement me")
3434
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
type UploadFile struct {
2424
id model.InternalID
2525
porter porter.LibrarianPorterServiceClient
26-
callback modelbinah.UploadCallbackFunc
26+
callback modelbinah.CallbackFunc
2727
file *os.File
2828
Writer io.Writer
2929
}
@@ -111,7 +111,7 @@ func NewControlBlock(a *libauth.Auth) *modelbinah.ControlBlock {
111111

112112
func (b *Binah) NewUploadFile(ctx context.Context) (*UploadFile, *errors.Error) {
113113
claims, exist := libauth.FromContext(ctx)
114-
if !exist || claims == nil || claims.TransferMetadata == nil {
114+
if !exist || claims == nil {
115115
return nil, pb.ErrorErrorReasonUnauthorized("token required")
116116
}
117117
callback, err := b.callback.GetUploadCallback(ctx)
@@ -137,18 +137,16 @@ func (b *Binah) NewUploadFile(ctx context.Context) (*UploadFile, *errors.Error)
137137

138138
func (b *Binah) PresignedDownloadFile(ctx context.Context) (string, *errors.Error) {
139139
claims, exist := libauth.FromContext(ctx)
140-
if !exist || claims == nil || claims.TransferMetadata == nil {
140+
if !exist || claims == nil {
141141
return "", pb.ErrorErrorReasonUnauthorized("token required")
142142
}
143-
var id model.InternalID
144-
if meta, met := claims.TransferMetadata.(modelbinah.FileMetadata); !met {
145-
return "", pb.ErrorErrorReasonBadRequest("bad token info")
146-
} else {
147-
id = meta.ID
143+
callback, err := b.callback.GetDownloadFileMetadata(ctx)
144+
if err != nil {
145+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
148146
}
149147
res, err := b.porter.PresignedPullData(ctx, &porter.PresignedPullDataRequest{
150148
Source: porter.DataSource_DATA_SOURCE_INTERNAL_DEFAULT,
151-
ContentId: strconv.FormatInt(int64(id), 10),
149+
ContentId: strconv.FormatInt(int64(callback.ID), 10),
152150
ExpireTime: durationpb.New(libtime.Day),
153151
})
154152
if err != nil {

app/sephirah/internal/biz/bizchesed/chesed.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
porter "github.com/tuihub/protos/pkg/librarian/porter/v1"
2323
searcher "github.com/tuihub/protos/pkg/librarian/searcher/v1"
2424
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
25+
librarian "github.com/tuihub/protos/pkg/librarian/v1"
2526

2627
"github.com/go-kratos/kratos/v2/errors"
2728
"github.com/google/wire"
@@ -35,8 +36,10 @@ var ProviderSet = wire.NewSet(
3536

3637
type ChesedRepo interface {
3738
CreateImage(context.Context, model.InternalID, *modelchesed.Image) error
39+
ListImages(context.Context, model.InternalID, model.Paging) ([]*modelchesed.Image, int64, error)
3840
ListImageNeedScan(context.Context) ([]*modelchesed.Image, error)
3941
SetImageStatus(context.Context, model.InternalID, modelchesed.ImageStatus) error
42+
GetImage(context.Context, model.InternalID, model.InternalID) (*modelchesed.Image, error)
4043
}
4144

4245
type Chesed struct {
@@ -45,6 +48,7 @@ type Chesed struct {
4548
searcher searcher.LibrarianSearcherServiceClient
4649
porter porter.LibrarianPorterServiceClient
4750
upload *modelbinah.UploadCallBack
51+
download *modelbinah.DownloadCallBack
4852
imageCache *libcache.Map[model.InternalID, modelchesed.Image]
4953
muScanImage sync.Mutex
5054
}
@@ -64,13 +68,18 @@ func NewChesed(
6468
porter: pClient,
6569
searcher: sClient,
6670
upload: nil,
71+
download: nil,
6772
imageCache: imageCache,
6873
muScanImage: sync.Mutex{},
6974
}
7075
c.upload = block.RegisterUploadCallback(
71-
modelbinah.UploadArtifacts,
76+
modelbinah.UploadChesedImage,
7277
c.UploadImageCallback,
7378
)
79+
c.download = block.RegisterDownloadCallback(
80+
modelbinah.DownloadEmpty,
81+
nil,
82+
)
7483
err := cron.BySeconds(60, c.ScanImage, context.Background()) //nolint:gomnd //TODO
7584
if err != nil {
7685
return nil, err
@@ -228,11 +237,39 @@ func (c *Chesed) ScanImage(ctx context.Context) { //nolint:funlen,gocognit //TOD
228237
}
229238
}
230239

231-
func (c *Chesed) SearchImages(ctx context.Context, keywords string) ([]model.InternalID, *errors.Error) {
240+
func (c *Chesed) ListImages(ctx context.Context, paging model.Paging) ([]model.InternalID, int64, *errors.Error) {
241+
if !libauth.FromContextAssertUserType(ctx, libauth.UserTypeAdmin, libauth.UserTypeNormal) {
242+
return nil, 0, pb.ErrorErrorReasonForbidden("no permission")
243+
}
244+
claims, exist := libauth.FromContext(ctx)
245+
if !exist {
246+
return nil, 0, pb.ErrorErrorReasonForbidden("no permission")
247+
}
248+
images, total, err := c.repo.ListImages(ctx, claims.InternalID, paging)
249+
if err != nil {
250+
return nil, 0, pb.ErrorErrorReasonUnspecified("%s", err.Error())
251+
}
252+
res := make([]model.InternalID, 0, len(images))
253+
for _, image := range images {
254+
res = append(res, image.ID)
255+
}
256+
return res, total, nil
257+
}
258+
259+
func (c *Chesed) SearchImages(ctx context.Context, paging model.Paging, keywords string) (
260+
[]model.InternalID, *errors.Error) {
232261
if !libauth.FromContextAssertUserType(ctx, libauth.UserTypeAdmin, libauth.UserTypeNormal) {
233262
return nil, pb.ErrorErrorReasonForbidden("no permission")
234263
}
235-
resp, err := c.searcher.SearchID(ctx, &searcher.SearchIDRequest{Keyword: keywords})
264+
resp, err := c.searcher.SearchID(ctx,
265+
&searcher.SearchIDRequest{
266+
Paging: &librarian.PagingRequest{
267+
PageNum: int32(paging.PageNum),
268+
PageSize: int32(paging.PageSize),
269+
},
270+
Keyword: keywords,
271+
},
272+
)
236273
if err != nil {
237274
return nil, pb.ErrorErrorReasonUnspecified("%s", err.Error())
238275
}
@@ -242,3 +279,28 @@ func (c *Chesed) SearchImages(ctx context.Context, keywords string) ([]model.Int
242279
}
243280
return res, nil
244281
}
282+
283+
func (c *Chesed) DownloadImage(ctx context.Context, id model.InternalID) (string, *errors.Error) {
284+
if !libauth.FromContextAssertUserType(ctx, libauth.UserTypeAdmin, libauth.UserTypeNormal) {
285+
return "", pb.ErrorErrorReasonForbidden("no permission")
286+
}
287+
claims, exist := libauth.FromContext(ctx)
288+
if !exist {
289+
return "", pb.ErrorErrorReasonUnauthorized("empty token")
290+
}
291+
image, err := c.repo.GetImage(ctx, claims.InternalID, id)
292+
if err != nil {
293+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
294+
}
295+
token, err := c.download.GenerateDownloadToken(ctx, modelbinah.FileMetadata{
296+
ID: id,
297+
Name: image.Name,
298+
Size: 0,
299+
Type: 0,
300+
Sha256: nil,
301+
}, libtime.HalfDay)
302+
if err != nil {
303+
return "", pb.ErrorErrorReasonUnspecified("%s", err.Error())
304+
}
305+
return token, nil
306+
}

app/sephirah/internal/data/chesed.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizchesed"
77
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/converter"
88
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/image"
9+
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/user"
910
"github.com/tuihub/librarian/app/sephirah/internal/model/modelchesed"
1011
"github.com/tuihub/librarian/internal/model"
1112
)
@@ -32,6 +33,26 @@ func (c chesedRepo) CreateImage(ctx context.Context, userID model.InternalID, im
3233
Exec(ctx)
3334
}
3435

36+
func (c chesedRepo) ListImages(ctx context.Context, userID model.InternalID, paging model.Paging) (
37+
[]*modelchesed.Image, int64, error) {
38+
q := c.data.db.Image.Query().
39+
Where(
40+
image.HasOwnerWith(user.IDEQ(userID)),
41+
)
42+
total, err := q.Count(ctx)
43+
if err != nil {
44+
return nil, 0, err
45+
}
46+
res, err := q.
47+
Limit(paging.ToLimit()).
48+
Offset(paging.ToOffset()).
49+
All(ctx)
50+
if err != nil {
51+
return nil, 0, err
52+
}
53+
return converter.ToBizImageList(res), int64(total), nil
54+
}
55+
3556
func (c chesedRepo) ListImageNeedScan(ctx context.Context) ([]*modelchesed.Image, error) {
3657
res, err := c.data.db.Image.Query().
3758
Where(image.StatusEQ(image.StatusUploaded)).
@@ -48,3 +69,17 @@ func (c chesedRepo) SetImageStatus(ctx context.Context, id model.InternalID, sta
4869
SetStatus(converter.ToEntImageStatus(status)).
4970
Exec(ctx)
5071
}
72+
73+
func (c chesedRepo) GetImage(ctx context.Context, userID model.InternalID, id model.InternalID) (
74+
*modelchesed.Image, error) {
75+
res, err := c.data.db.Image.Query().
76+
Where(
77+
image.IDEQ(id),
78+
image.HasOwnerWith(user.IDEQ(userID)),
79+
).
80+
Only(ctx)
81+
if err != nil {
82+
return nil, err
83+
}
84+
return converter.ToBizImage(res), nil
85+
}

app/sephirah/internal/data/gebura.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func (g geburaRepo) ListApps(
142142
return err
143143
}
144144
al, err = q.
145-
Limit(paging.PageSize).
146-
Offset((paging.PageNum - 1) * paging.PageSize).
145+
Limit(paging.ToLimit()).
146+
Offset(paging.ToOffset()).
147147
All(ctx)
148148
if err != nil {
149149
return err
@@ -223,8 +223,8 @@ func (g geburaRepo) SearchApps(ctx context.Context, paging model.Paging, keyword
223223
return nil, 0, err
224224
}
225225
apps, err := q.
226-
Limit(paging.PageSize).
227-
Offset((paging.PageNum - 1) * paging.PageSize).
226+
Limit(paging.ToLimit()).
227+
Offset(paging.ToOffset()).
228228
All(ctx)
229229
if err != nil {
230230
return nil, 0, err
@@ -339,8 +339,8 @@ func (g geburaRepo) ListAppPackages(
339339
return nil, 0, err
340340
}
341341
ap, err := q.
342-
Limit(paging.PageSize).
343-
Offset((paging.PageNum - 1) * paging.PageSize).
342+
Limit(paging.ToLimit()).
343+
Offset(paging.ToOffset()).
344344
All(ctx)
345345
if err != nil {
346346
return nil, 0, err

0 commit comments

Comments
 (0)