Skip to content

Commit

Permalink
feat: Integrate MinIO for file caching and enhance upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Laisky committed Nov 15, 2024
1 parent ad588ef commit 29a1baf
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 32 deletions.
22 changes: 18 additions & 4 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
gconfig "github.com/Laisky/go-config/v2"
gcmd "github.com/Laisky/go-utils/v4/cmd"
"github.com/Laisky/zap"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -50,14 +52,26 @@ func runAPI() error {
ctx := context.Background()
logger := log.Logger.Named("api")

// arweave := arweave.NewAkrod(
// gconfig.Shared.GetStringSlice("settings.db.akord.apikeys"),
// )
arweave := arweave.NewArdrive(
gconfig.S.GetString("settings.arweave.wallet_file"),
gconfig.S.GetString("settings.arweave.folder_id"),
)

minioCli, err := minio.New(
gconfig.S.GetString("settings.arweave.s3.endpoint"),
&minio.Options{
Creds: credentials.NewStaticV4(
gconfig.S.GetString("settings.arweave.s3.access_key"),
gconfig.S.GetString("settings.arweave.s3.secret"),
"",
),
Secure: true,
},
)
if err != nil {
return errors.Wrap(err, "new minio client")
}

var args web.ResolverArgs

{ // setup telegram
Expand All @@ -79,7 +93,7 @@ func runAPI() error {
args.TelegramSvc, err = telegramSvc.New(ctx,
telegramDao.NewMonitor(monitorDB),
telegramDao.NewTelegram(telegramDB),
telegramDao.NewUpload(telegramDB, arweave),
telegramDao.NewUpload(telegramDB, arweave, minioCli),
botToken,
gconfig.Shared.GetString("settings.telegram.api"),
)
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/gomarkdown/markdown v0.0.0-20241105142532-d03b89096d81
github.com/jinzhu/copier v0.4.0
github.com/minio/minio-go/v7 v7.0.80
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -51,18 +52,20 @@ require (
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gammazero/deque v0.2.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -78,12 +81,13 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -98,6 +102,7 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rivo/duplo v0.0.0-20220703183130-751e882e6b83 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
Expand Down
23 changes: 17 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpO
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -207,6 +209,8 @@ github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-json-experiment/json v0.0.0-20231011163920-8aa127fd5801 h1:PRieymvnGuBZUnWVQPBOemqlIhRznqtSxs/1LqlWe20=
github.com/go-json-experiment/json v0.0.0-20231011163920-8aa127fd5801/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -242,8 +246,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -406,11 +410,12 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -449,6 +454,10 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.80 h1:2mdUHXEykRdY/BigLt3Iuu1otL0JTogT0Nmltg0wujk=
github.com/minio/minio-go/v7 v7.0.80/go.mod h1:84gmIilaX4zcvAWWzJ5Z1WI5axN+hAbM5w25xf8xvC0=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
Expand Down Expand Up @@ -529,6 +538,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.6.0/go.mod h1:U8+INwJo3nBv1m6A/8OBXAq7Jnpspk5AxSgDyEQcea8=
Expand Down
78 changes: 62 additions & 16 deletions internal/web/telegram/dao/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
package dao

import (
"bytes"
"context"
"fmt"
"strings"
"time"

"go.mongodb.org/mongo-driver/bson"
mongoLib "go.mongodb.org/mongo-driver/mongo"
"golang.org/x/sync/errgroup"

"github.com/Laisky/errors/v2"
gmw "github.com/Laisky/gin-middlewares/v5"
gconfig "github.com/Laisky/go-config/v2"
"github.com/Laisky/laisky-blog-graphql/library/db/arweave"
"github.com/Laisky/laisky-blog-graphql/library/db/mongo"
"github.com/Laisky/zap"
"github.com/minio/minio-go/v7"
)

const (
Expand All @@ -22,18 +28,21 @@ const (

// Upload db
type Upload struct {
db mongo.DB
ar *arweave.Ardrive
db mongo.DB
ar *arweave.Ardrive
minio *minio.Client
}

// NewUpload create new DB
func NewUpload(
db mongo.DB,
ar *arweave.Ardrive,
minio *minio.Client,
) *Upload {
return &Upload{
db: db,
ar: ar,
db: db,
ar: ar,
minio: minio,
}
}

Expand All @@ -44,24 +53,61 @@ func (d *Upload) GetFilesCol() *mongoLib.Collection {
return d.db.GetCol(colUploadFiles)
}

func (d *Upload) UploadFile(ctx context.Context, uid int64, cnt []byte, opts ...arweave.UploadOption) (fileID string, err error) {
func (d *Upload) UploadFile(ctx context.Context,
uid int64, cnt []byte, contentType string) (fileID string, err error) {
logger := gmw.GetLogger(ctx)

fileID, err = d.ar.Upload(ctx, cnt, opts...)
fileID, err = d.ar.Upload(ctx, cnt,
arweave.WithContentType(contentType),
)
if err != nil {
return "", errors.WithStack(err)
return fileID, errors.Wrap(err, "upload to arweave")
}

var pool errgroup.Group

// save file info
_, err = d.db.GetCol(colUploadFiles).
InsertOne(ctx, bson.M{
"created_at": time.Now(),
"file_id": fileID,
"file_size": len(cnt),
"telegram_uid": uid,
})
if err != nil {
logger.Error("save file info", zap.Error(err))
pool.Go(func() (err error) {
_, err = d.db.GetCol(colUploadFiles).
InsertOne(ctx, bson.M{
"created_at": time.Now(),
"file_id": fileID,
"file_size": len(cnt),
"telegram_uid": uid,
})
if err != nil {
logger.Error("save file info", zap.Error(err))
}

return nil
})

// also upload to minio as cache
pool.Go(func() (err error) {
objkey := fmt.Sprintf(
"%s/%s",
strings.TrimSuffix(gconfig.S.GetString("settings.arweave.s3.prefix"), "/"),
fileID,
)
_, err = d.minio.PutObject(ctx,
gconfig.S.GetString("settings.arweave.s3.bucket"),
objkey,
bytes.NewReader(cnt),
int64(len(cnt)),
minio.PutObjectOptions{
ContentType: contentType,
},
)
if err != nil {
logger.Error("upload to minio", zap.Error(err))
}

logger.Info("upload to minio", zap.String("objkey", objkey))
return nil // ignore error
})

if err = pool.Wait(); err != nil {
return fileID, errors.Wrap(err, "upload file")
}

return fileID, nil
Expand Down
4 changes: 1 addition & 3 deletions internal/web/telegram/service/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
gutils "github.com/Laisky/go-utils/v4"
glog "github.com/Laisky/go-utils/v4/log"
"github.com/Laisky/laisky-blog-graphql/internal/web/telegram/model"
"github.com/Laisky/laisky-blog-graphql/library/db/arweave"
"github.com/Laisky/zap"
tb "gopkg.in/telebot.v3"
)
Expand Down Expand Up @@ -128,8 +127,7 @@ func (s *Telegram) _handleUserUploadedFile(ctx context.Context,
}

fileID, err = s.uploadDap.UploadFile(ctx,
msg.Sender.ID, cnt,
arweave.WithContentType(contentType),
msg.Sender.ID, cnt, contentType,
)
if err != nil {
return fileID, errors.Wrap(err, "upload file")
Expand Down

0 comments on commit 29a1baf

Please sign in to comment.