From 306a5083190003bd078598c2607cba6237440bf3 Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:16:03 +0530 Subject: [PATCH] refactor: db params --- cmd/run.go | 12 ++++---- config.sample.toml | 6 ++-- go.mod | 4 +++ go.sum | 16 ++++++++++ internal/config/config.go | 12 ++++---- internal/database/database.go | 32 ++++++++++++-------- internal/tgc/tgc.go | 9 ++++++ pkg/cron/cron.go | 48 ++++++------------------------ pkg/mapper/mapper.go | 15 +++++----- pkg/models/file.go | 56 +++++++++++++---------------------- pkg/schemas/file.go | 2 +- pkg/services/file.go | 31 +++++-------------- pkg/services/upload.go | 6 +++- 13 files changed, 110 insertions(+), 139 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 4b8d9508..fe51213c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -62,7 +62,8 @@ func NewRun() *cobra.Command { runCmd.Flags().StringVar(&config.DB.DataSource, "db-data-source", "", "Database connection string") runCmd.Flags().IntVar(&config.DB.LogLevel, "db-log-level", 1, "Database log level") - runCmd.Flags().BoolVar(&config.DB.Migrate.Enable, "db-migrate-enable", true, "Enable database migration") + runCmd.Flags().BoolVar(&config.DB.PrepareStmt, "db-prepare-stmt", true, "Enable prepared statements") + runCmd.Flags().BoolVar(&config.DB.Pool.Enable, "db-pool-enable", true, "Enable database pool") runCmd.Flags().IntVar(&config.DB.Pool.MaxIdleConnections, "db-pool-max-open-connections", 25, "Database max open connections") runCmd.Flags().IntVar(&config.DB.Pool.MaxIdleConnections, "db-pool-max-idle-connections", 25, "Database max idle connections") duration.DurationVar(runCmd.Flags(), &config.DB.Pool.MaxLifetime, "db-pool-max-lifetime", 10*time.Minute, "Database max connection lifetime") @@ -83,6 +84,7 @@ func NewRun() *cobra.Command { runCmd.Flags().StringVar(&config.TG.Proxy, "tg-proxy", "", "HTTP OR SOCKS5 proxy URL") runCmd.Flags().IntVar(&config.TG.BgBotsLimit, "tg-bg-bots-limit", 5, "Background bots limit") runCmd.Flags().BoolVar(&config.TG.DisableStreamBots, "tg-disable-stream-bots", false, "Disable stream bots") + runCmd.Flags().BoolVar(&config.TG.EnableLogging, "tg-enable-logging", false, "Enable telegram client logging") runCmd.Flags().StringVar(&config.TG.Uploads.EncryptionKey, "tg-uploads-encryption-key", "", "Uploads encryption key") runCmd.Flags().IntVar(&config.TG.Uploads.Threads, "tg-uploads-threads", 8, "Uploads threads") runCmd.Flags().IntVar(&config.TG.Uploads.MaxRetries, "tg-uploads-max-retries", 10, "Uploads Retries") @@ -133,12 +135,6 @@ func runApplication(conf *config.Config) { ), ) - defer func() { - if r := recover(); r != nil { - logging.FromContext(context.TODO()).Errorf("Recovered from panic: %v", r) - } - }() - app.Run() } @@ -245,7 +241,9 @@ func initApp(lc fx.Lifecycle, cfg *config.Config, c *controller.Controller) *gin lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { logging.FromContext(ctx).Infof("Started server http://localhost:%d", cfg.Server.Port) + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { logging.DefaultLogger().Errorw("failed to close http server", "err", err) } diff --git a/config.sample.toml b/config.sample.toml index 23ce29f4..7cd0a298 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -1,11 +1,9 @@ [db] data-source = "" + prepare-stmt = true log-level = 1 - - [db.migrate] - enable = true - [db.pool] + enable = true max-idle-connections = 25 max-lifetime = "10m" max-open-connections = 25 diff --git a/go.mod b/go.mod index 42578955..3c770ff8 100644 --- a/go.mod +++ b/go.mod @@ -26,17 +26,20 @@ require ( go.uber.org/zap v1.27.0 golang.org/x/time v0.5.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 + gorm.io/datatypes v1.2.0 gorm.io/driver/postgres v1.5.7 gorm.io/gorm v1.25.10 ) require ( + filippo.io/edwards25519 v1.1.0 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -57,6 +60,7 @@ require ( golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect google.golang.org/appengine v1.6.8 // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gorm.io/driver/mysql v1.5.6 // indirect ) diff --git a/go.sum b/go.sum index 546ee8fc..824e6a84 100644 --- a/go.sum +++ b/go.sum @@ -66,10 +66,15 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= 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/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA= +github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= +github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -136,8 +141,12 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/microsoft/go-mssqldb v1.7.0 h1:sgMPW0HA6Ihd37Yx0MzHyKD726C2kY/8KJsQtXHNaAs= +github.com/microsoft/go-mssqldb v1.7.0/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -309,10 +318,17 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco= +gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04= gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8= gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA= +gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= +gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= +gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0= +gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHDnJLig= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s= gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= diff --git a/internal/config/config.go b/internal/config/config.go index 0d12c1a3..a7f40bd7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -35,6 +35,7 @@ type TGConfig struct { Proxy string ReconnectTimeout time.Duration PoolSize int64 + EnableLogging bool Uploads struct { EncryptionKey string Threads int @@ -56,12 +57,11 @@ type JWTConfig struct { } type DBConfig struct { - DataSource string - LogLevel int - Migrate struct { - Enable bool - } - Pool struct { + DataSource string + PrepareStmt bool + LogLevel int + Pool struct { + Enable bool MaxOpenConnections int MaxIdleConnections int MaxLifetime time.Duration diff --git a/internal/database/database.go b/internal/database/database.go index 1a7e6c3c..f7f1fd6a 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -20,9 +20,11 @@ func NewDatabase(cfg *config.Config) (*gorm.DB, error) { err error logger = NewLogger(time.Second, true, zapcore.Level(cfg.DB.LogLevel)) ) - - for i := 0; i <= 10; i++ { - db, err = gorm.Open(postgres.Open(cfg.DB.DataSource), &gorm.Config{ + for i := 0; i <= 5; i++ { + db, err = gorm.Open(postgres.New(postgres.Config{ + DSN: cfg.DB.DataSource, + PreferSimpleProtocol: !cfg.DB.PrepareStmt, + }), &gorm.Config{ Logger: logger, NamingStrategy: schema.NamingStrategy{ TablePrefix: "teldrive.", @@ -38,24 +40,28 @@ func NewDatabase(cfg *config.Config) (*gorm.DB, error) { logging.DefaultLogger().Warnf("failed to open database: %v", err) time.Sleep(500 * time.Millisecond) } - db.Use(extraClausePlugin.New()) + if err != nil { logging.DefaultLogger().Fatalf("database: %v", err) } - rawDB, err := db.DB() - if err != nil { - return nil, err - } - rawDB.SetMaxOpenConns(cfg.DB.Pool.MaxOpenConnections) - rawDB.SetMaxIdleConns(cfg.DB.Pool.MaxIdleConnections) - rawDB.SetConnMaxLifetime(cfg.DB.Pool.MaxLifetime) + db.Use(extraClausePlugin.New()) - if cfg.DB.Migrate.Enable { - err := migrateDB(rawDB) + if cfg.DB.Pool.Enable { + rawDB, err := db.DB() if err != nil { return nil, err } + rawDB.SetMaxOpenConns(cfg.DB.Pool.MaxOpenConnections) + rawDB.SetMaxIdleConns(cfg.DB.Pool.MaxIdleConnections) + rawDB.SetConnMaxLifetime(cfg.DB.Pool.MaxLifetime) + } + + sqlDb, _ := db.DB() + err = migrateDB(sqlDb) + if err != nil { + return nil, err } + return db, nil } diff --git a/internal/tgc/tgc.go b/internal/tgc/tgc.go index 092ebe3e..266d7c8d 100644 --- a/internal/tgc/tgc.go +++ b/internal/tgc/tgc.go @@ -7,6 +7,7 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/divyam234/teldrive/internal/config" "github.com/divyam234/teldrive/internal/kv" + "github.com/divyam234/teldrive/internal/logging" "github.com/divyam234/teldrive/internal/recovery" "github.com/divyam234/teldrive/internal/retry" "github.com/divyam234/teldrive/internal/utils" @@ -16,6 +17,7 @@ import ( "github.com/gotd/td/telegram" "github.com/gotd/td/telegram/dcs" "github.com/pkg/errors" + "go.uber.org/zap" "golang.org/x/net/proxy" "golang.org/x/time/rate" ) @@ -31,6 +33,12 @@ func New(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHa dialer = d.DialContext } + var logger *zap.Logger + if config.EnableLogging { + logger = logging.FromContext(ctx).Desugar().Named("td") + + } + opts := telegram.Options{ Resolver: dcs.Plain(dcs.PlainOptions{ Dial: dialer, @@ -52,6 +60,7 @@ func New(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHa DialTimeout: 10 * time.Second, Middlewares: middlewares, UpdateHandler: handler, + Logger: logger, } return telegram.NewClient(config.AppId, config.AppHash, opts), nil diff --git a/pkg/cron/cron.go b/pkg/cron/cron.go index 35fd982d..2bf9d7e6 100644 --- a/pkg/cron/cron.go +++ b/pkg/cron/cron.go @@ -2,58 +2,33 @@ package cron import ( "context" - "database/sql/driver" - "encoding/json" "time" "github.com/divyam234/teldrive/internal/config" "github.com/divyam234/teldrive/internal/logging" "github.com/divyam234/teldrive/pkg/models" + "github.com/divyam234/teldrive/pkg/schemas" "github.com/divyam234/teldrive/pkg/services" "github.com/go-co-op/gocron" "go.uber.org/zap" + "gorm.io/datatypes" "gorm.io/gorm" ) -type Files []File type File struct { - ID string `json:"id"` - Parts []models.Part `json:"parts"` -} - -func (a Files) Value() (driver.Value, error) { - return json.Marshal(a) -} - -func (a *Files) Scan(value interface{}) error { - if err := json.Unmarshal(value.([]byte), &a); err != nil { - return err - } - return nil -} - -type UpParts []int - -func (a UpParts) Value() (driver.Value, error) { - return json.Marshal(a) -} - -func (a *UpParts) Scan(value interface{}) error { - if err := json.Unmarshal(value.([]byte), &a); err != nil { - return err - } - return nil + ID string `json:"id"` + Parts []schemas.Part `json:"parts"` } type Result struct { - Files Files + Files datatypes.JSONSlice[File] Session string UserId int64 ChannelId int64 } type UploadResult struct { - Parts UpParts + Parts datatypes.JSONSlice[int] Session string UserId int64 ChannelId int64 @@ -137,20 +112,15 @@ func (c *CronService) CleanUploads(ctx context.Context) { for _, result := range upResults { - parts := []int{} - for _, id := range result.Parts { - parts = append(parts, id) - } - - if result.Session == "" && len(parts) > 0 { - c.db.Where("part_id = any($1)", parts).Delete(&models.Upload{}) + if result.Session == "" && len(result.Parts) > 0 { + c.db.Where("part_id = any($1)", result.Parts).Delete(&models.Upload{}) break } err := services.DeleteTGMessages(ctx, &c.cnf.TG, result.Session, result.ChannelId, result.UserId, result.Parts) c.logger.Errorw("failed to delete messages", err) if err == nil { - c.db.Where("part_id = any($1)", parts).Delete(&models.Upload{}) + c.db.Where("part_id = any($1)", result.Parts).Delete(&models.Upload{}) } } } diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index ec30b122..0e234c34 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -26,18 +26,17 @@ func ToFileOut(file models.File) *schemas.FileOut { } func ToFileOutFull(file models.File) *schemas.FileOutFull { - parts := []schemas.Part{} - for _, part := range *file.Parts { - parts = append(parts, schemas.Part{ - ID: part.ID, - Salt: part.Salt, - }) + + var channelId int64 + + if file.ChannelID != nil { + channelId = *file.ChannelID } return &schemas.FileOutFull{ FileOut: ToFileOut(file), - Parts: parts, - ChannelID: *file.ChannelID, + Parts: file.Parts, + ChannelID: channelId, Encrypted: file.Encrypted, } } diff --git a/pkg/models/file.go b/pkg/models/file.go index 1e4d5f29..26269772 100644 --- a/pkg/models/file.go +++ b/pkg/models/file.go @@ -1,44 +1,28 @@ package models import ( - "database/sql/driver" - "encoding/json" "time" + + "github.com/divyam234/teldrive/pkg/schemas" + "gorm.io/datatypes" ) type File struct { - Id string `gorm:"type:text;primaryKey;default:generate_uid(16)"` - Name string `gorm:"type:text;not null"` - Type string `gorm:"type:text;not null"` - MimeType string `gorm:"type:text;not null"` - Path string `gorm:"type:text;index"` - Size *int64 `gorm:"type:bigint"` - Starred bool `gorm:"default:false"` - Depth *int `gorm:"type:integer"` - Category string `gorm:"type:text"` - Encrypted bool `gorm:"default:false"` - UserID int64 `gorm:"type:bigint;not null"` - Status string `gorm:"type:text"` - ParentID string `gorm:"type:text;index"` - Parts *Parts `gorm:"type:jsonb"` - ChannelID *int64 `gorm:"type:bigint"` - CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"` - UpdatedAt time.Time `gorm:"default:timezone('utc'::text, now())"` -} - -type Parts []Part -type Part struct { - ID int64 `json:"id"` - Salt string `json:"salt,omitempty"` -} - -func (a Parts) Value() (driver.Value, error) { - return json.Marshal(a) -} - -func (a *Parts) Scan(value interface{}) error { - if err := json.Unmarshal(value.([]byte), &a); err != nil { - return err - } - return nil + Id string `gorm:"type:text;primaryKey;default:generate_uid(16)"` + Name string `gorm:"type:text;not null"` + Type string `gorm:"type:text;not null"` + MimeType string `gorm:"type:text;not null"` + Path string `gorm:"type:text;index"` + Size *int64 `gorm:"type:bigint"` + Starred bool `gorm:"default:false"` + Depth *int `gorm:"type:integer"` + Category string `gorm:"type:text"` + Encrypted bool `gorm:"default:false"` + UserID int64 `gorm:"type:bigint;not null"` + Status string `gorm:"type:text"` + ParentID string `gorm:"type:text;index"` + Parts datatypes.JSONSlice[schemas.Part] `gorm:"type:jsonb"` + ChannelID *int64 `gorm:"type:bigint"` + CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"` + UpdatedAt time.Time `gorm:"default:timezone('utc'::text, now())"` } diff --git a/pkg/schemas/file.go b/pkg/schemas/file.go index 5644c84e..58440d34 100644 --- a/pkg/schemas/file.go +++ b/pkg/schemas/file.go @@ -56,7 +56,7 @@ type FileOut struct { type FileOutFull struct { *FileOut Parts []Part `json:"parts,omitempty"` - ChannelID int64 `json:"channelId"` + ChannelID int64 `json:"channelId,omitempty"` Encrypted bool `json:"encrypted"` } diff --git a/pkg/services/file.go b/pkg/services/file.go index cc223ea5..9d6a2ce5 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -31,6 +31,7 @@ import ( "go.uber.org/zap" "github.com/jackc/pgx/v5/pgtype" + "gorm.io/datatypes" "gorm.io/gorm" "gorm.io/gorm/clause" ) @@ -82,15 +83,7 @@ func (fs *FileService) CreateFile(c *gin.Context, userId int64, fileIn *schemas. fileDB.ChannelID = &channelId fileDB.MimeType = fileIn.MimeType fileDB.Category = string(category.GetCategory(fileIn.Name)) - parts := models.Parts{} - for _, part := range fileIn.Parts { - parts = append(parts, models.Part{ - ID: part.ID, - Salt: part.Salt, - }) - - } - fileDB.Parts = &parts + fileDB.Parts = datatypes.NewJSONSlice(fileIn.Parts) fileDB.Starred = false fileDB.Size = &fileIn.Size } @@ -134,17 +127,7 @@ func (fs *FileService) UpdateFile(id string, userId int64, update *schemas.FileU } if len(update.Parts) > 0 { - parts := models.Parts{} - - for _, part := range update.Parts { - parts = append(parts, models.Part{ - ID: part.ID, - Salt: part.Salt, - }) - - } - - updateDb.Parts = &parts + updateDb.Parts = datatypes.NewJSONSlice(update.Parts) } chain = fs.db.Model(&files).Clauses(clause.Returning{}).Where("id = ?", id).Updates(updateDb) @@ -360,7 +343,7 @@ func (fs *FileService) DeleteFileParts(c *gin.Context, id string) (*schemas.Mess ids := []int{} - for _, part := range *file.Parts { + for _, part := range file.Parts { ids = append(ids, int(part.ID)) } @@ -416,7 +399,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr file := mapper.ToFileOutFull(res[0]) - newIds := models.Parts{} + newIds := []schemas.Part{} channelId, err := GetDefaultChannel(c, fs.db, userId) if err != nil { @@ -466,7 +449,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr } } - newIds = append(newIds, models.Part{ID: int64(msg.ID), Salt: file.Parts[i].Salt}) + newIds = append(newIds, schemas.Part{ID: int64(msg.ID), Salt: file.Parts[i].Salt}) } return nil @@ -490,7 +473,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr dbFile.Size = &file.Size dbFile.Type = file.Type dbFile.MimeType = file.MimeType - dbFile.Parts = &newIds + dbFile.Parts = datatypes.NewJSONSlice(newIds) dbFile.UserID = userId dbFile.Starred = false dbFile.Status = "active" diff --git a/pkg/services/upload.go b/pkg/services/upload.go index 680dc3c6..d9727ec3 100644 --- a/pkg/services/upload.go +++ b/pkg/services/upload.go @@ -162,7 +162,11 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty uploadPool := pool.NewPool(client, int64(us.cnf.PoolSize), middlewares...) - defer uploadPool.Close() + defer func() { + if uploadPool != nil { + uploadPool.Close() + } + }() logger := logging.FromContext(c)