Skip to content

Commit

Permalink
feat: provide utils func for data rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxsen committed Aug 27, 2024
1 parent d95f2c2 commit 74c7919
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
34 changes: 14 additions & 20 deletions processor/handler/image_transcode_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,22 @@ func (p *imageTranscodeHandler) transcode(ctx context.Context, name string, f *m
return nil
}
logger = logger.With(zap.String("key", f.Key))
data, err := store.GetData(ctx, f.Key)
if err != nil {
logger.Debug("read key data failed", zap.Error(err))
return f //不丢弃, 后续处理的时候报错, 方便发现问题
}
raw, err := image.TranscodeToJpeg(data)
if err != nil && strings.Contains(err.Error(), "luma/chroma subsampling ratio") && ffmpeg.IsFFMpegEnabled() {
data, err = ffmpeg.ConvertToYuv420pJpegFromBytes(ctx, data)
if err != nil {
logger.Error("use ffmpeg to correct invalid image data failed", zap.Error(err))
return nil

key, err := store.AnonymousDataRewrite(ctx, f.Key, func(ctx context.Context, data []byte) ([]byte, error) {
raw, err := image.TranscodeToJpeg(data)
if err != nil && strings.Contains(err.Error(), "luma/chroma subsampling ratio") && ffmpeg.IsFFMpegEnabled() {
data, err = ffmpeg.ConvertToYuv420pJpegFromBytes(ctx, data)
if err != nil {
logger.Error("use ffmpeg to correct invalid image data failed", zap.Error(err))
return nil, err
}
raw, err = image.TranscodeToJpeg(data)
}
raw, err = image.TranscodeToJpeg(data)
}
if err != nil {
logger.Error("unable to convert image to jpeg format", zap.Error(err))
return nil
}
key, err := store.AnonymousPutData(ctx, raw)
return raw, err
})
if err != nil {
logger.Error("store transcoded image data failed", zap.Error(err))
return f //
logger.Error("transcoded image data failed", zap.Error(err))
return nil //
}
logger.Debug("transcode image succ", zap.String("new_key", key))
f.Key = key
Expand Down
12 changes: 3 additions & 9 deletions processor/handler/poster_crop_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ func (c *posterCropHandler) Handle(ctx context.Context, fc *model.FileContext) e
if fc.Number.GetIsUncensorMovie() && face.IsFaceRecognizeEnabled() { //如果为步兵, 则使用人脸识别(当然, 只有该特性能用的情况下才启用)
cutter = c.wrapCutImageWithFaceRec(ctx, image.CutCensoredImageFromBytes)
}
data, err := store.GetData(ctx, fc.Meta.Cover.Key)
if err != nil {
return fmt.Errorf("get cover data failed, err:%w, key:%s", err, fc.Meta.Cover.Key)
}
res, err := cutter(data)
if err != nil {
return fmt.Errorf("cut poster image failed, err:%w", err)
}
key, err := store.AnonymousPutData(ctx, res)
key, err := store.AnonymousDataRewrite(ctx, fc.Meta.Cover.Key, func(ctx context.Context, data []byte) ([]byte, error) {
return cutter(data)
})
if err != nil {
return fmt.Errorf("save cutted poster data failed, err:%w", err)
}
Expand Down
12 changes: 3 additions & 9 deletions processor/handler/watermark_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func (h *watermark) Handle(ctx context.Context, fc *model.FileContext) error {
if fc.Meta.Poster == nil || len(fc.Meta.Poster.Key) == 0 {
return nil
}
data, err := store.GetData(ctx, fc.Meta.Poster.Key)
if err != nil {
return fmt.Errorf("load poster key failed, key:%s", fc.Meta.Poster.Key)
}
tags := make([]image.Watermark, 0, 5)
if fc.Number.GetIs4K() {
tags = append(tags, image.WM4K)
Expand All @@ -38,11 +34,9 @@ func (h *watermark) Handle(ctx context.Context, fc *model.FileContext) error {
logutil.GetLogger(ctx).Debug("no watermark tag found, skip watermark proc")
return nil
}
newData, err := image.AddWatermarkFromBytes(data, tags)
if err != nil {
return fmt.Errorf("add watermark failed, err:%w", err)
}
key, err := store.AnonymousPutData(ctx, newData)
key, err := store.AnonymousDataRewrite(ctx, fc.Meta.Poster.Key, func(ctx context.Context, data []byte) ([]byte, error) {
return image.AddWatermarkFromBytes(data, tags)
})
if err != nil {
return fmt.Errorf("save watermarked image failed, err:%w", err)
}
Expand Down
18 changes: 18 additions & 0 deletions store/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/google/uuid"
)

type DataRewriteFunc func(ctx context.Context, data []byte) ([]byte, error)

type IStorage interface {
GetData(ctx context.Context, key string) ([]byte, error)
PutData(ctx context.Context, key string, value []byte) error
Expand Down Expand Up @@ -41,3 +43,19 @@ func GetData(ctx context.Context, key string) ([]byte, error) {
func IsDataExist(ctx context.Context, key string) (bool, error) {
return getDefaultInst().IsDataExist(ctx, key)
}

func AnonymousDataRewrite(ctx context.Context, key string, fn DataRewriteFunc) (string, error) {
raw, err := GetData(ctx, key)
if err != nil {
return key, err
}
newData, err := fn(ctx, raw)
if err != nil {
return key, err
}
newKey, err := AnonymousPutData(ctx, newData)
if err != nil {
return key, err
}
return newKey, nil
}

0 comments on commit 74c7919

Please sign in to comment.