@@ -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
3637type 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
4245type 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+ }
0 commit comments