From f28a35be1c7c654135d846cdfb281a9c5bac0c8a Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:51:47 +0530 Subject: [PATCH] refactor: return full path in single file res --- internal/reader/reader.go | 2 +- pkg/mapper/mapper.go | 8 +------- pkg/schemas/file.go | 9 ++++++--- pkg/services/common.go | 2 +- pkg/services/file.go | 21 +++++++++++---------- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/internal/reader/reader.go b/internal/reader/reader.go index 2b21f50e..e12d456e 100644 --- a/internal/reader/reader.go +++ b/internal/reader/reader.go @@ -128,7 +128,7 @@ func (r *LinearReader) getPartReader() (io.ReadCloser, error) { partID := r.parts[currentRange.PartNo].ID chunkSrc := &chunkSource{ - channelID: r.file.ChannelID, + channelID: *r.file.ChannelID, partID: partID, client: r.client, concurrency: r.concurrency, diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 366f8f27..9e110fd8 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -25,16 +25,10 @@ func ToFileOut(file models.File) *schemas.FileOut { func ToFileOutFull(file models.File) *schemas.FileOutFull { - var channelId int64 - - if file.ChannelID != nil { - channelId = *file.ChannelID - } - return &schemas.FileOutFull{ FileOut: ToFileOut(file), Parts: file.Parts, - ChannelID: channelId, + ChannelID: file.ChannelID, } } diff --git a/pkg/schemas/file.go b/pkg/schemas/file.go index e136f656..9dd861bd 100644 --- a/pkg/schemas/file.go +++ b/pkg/schemas/file.go @@ -2,11 +2,13 @@ package schemas import ( "time" + + "gorm.io/datatypes" ) type Part struct { ID int64 `json:"id"` - Salt string `json:"salt"` + Salt string `json:"salt,omitempty"` } type FileQuery struct { @@ -54,8 +56,9 @@ type FileOut struct { type FileOutFull struct { *FileOut - Parts []Part `json:"parts,omitempty"` - ChannelID int64 `json:"channelId,omitempty"` + Parts datatypes.JSONSlice[Part] `json:"parts,omitempty"` + ChannelID *int64 `json:"channelId,omitempty"` + Path string `json:"path,omitempty"` } type FileUpdate struct { diff --git a/pkg/services/common.go b/pkg/services/common.go index ce424524..19ca06e3 100644 --- a/pkg/services/common.go +++ b/pkg/services/common.go @@ -33,7 +33,7 @@ func getParts(ctx context.Context, client *telegram.Client, cache cache.Cacher, for _, part := range file.Parts { ids = append(ids, int(part.ID)) } - messages, err := tgc.GetMessages(ctx, client.API(), ids, file.ChannelID) + messages, err := tgc.GetMessages(ctx, client.API(), ids, *file.ChannelID) if err != nil { return nil, err diff --git a/pkg/services/file.go b/pkg/services/file.go index 9423a769..b62e9950 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -193,15 +193,16 @@ func (fs *FileService) UpdateFile(id string, userId int64, update *schemas.FileU } func (fs *FileService) GetFileByID(id string) (*schemas.FileOutFull, *types.AppError) { - var file models.File - if err := fs.db.Where("id = ?", id).First(&file).Error; err != nil { - if database.IsRecordNotFoundErr(err) { - return nil, &types.AppError{Error: database.ErrNotFound, Code: http.StatusNotFound} - } + var result []schemas.FileOutFull + if err := fs.db.Model(&models.File{}).Select("*", "(select get_path_from_file_id as path from teldrive.get_path_from_file_id(id))"). + Where("id = ?", id).Scan(&result).Error; err != nil { return nil, &types.AppError{Error: err} } + if len(result) == 0 { + return nil, &types.AppError{Error: database.ErrNotFound, Code: http.StatusNotFound} + } - return mapper.ToFileOutFull(file), nil + return &result[0], nil } func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*schemas.FileResponse, *types.AppError) { @@ -600,7 +601,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr for _, part := range file.Parts { ids = append(ids, int(part.ID)) } - messages, err := tgc.GetMessages(c, client.API(), ids, file.ChannelID) + messages, err := tgc.GetMessages(c, client.API(), ids, *file.ChannelID) if err != nil { return err @@ -806,7 +807,7 @@ func (fs *FileService) GetFileStream(c *gin.Context, download bool, sharedFile * c.Header("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": file.Name})) - tokens, err := getBotsToken(fs.db, fs.cache, session.UserId, file.ChannelID) + tokens, err := getBotsToken(fs.db, fs.cache, session.UserId, *file.ChannelID) if err != nil { fs.handleError(fmt.Errorf("failed to get bots: %w", err), w) @@ -831,9 +832,9 @@ func (fs *FileService) GetFileStream(c *gin.Context, download bool, sharedFile * multiThreads = 0 } else { - fs.botWorker.Set(tokens, file.ChannelID) + fs.botWorker.Set(tokens, *file.ChannelID) - token, _ = fs.botWorker.Next(file.ChannelID) + token, _ = fs.botWorker.Next(*file.ChannelID) middlewares := tgc.Middlewares(&fs.cnf.TG, 5) client, err = tgc.BotClient(c, fs.kv, &fs.cnf.TG, token, middlewares...)