Skip to content

Commit

Permalink
refactor: return full path in single file res
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Sep 20, 2024
1 parent 84d2241 commit f28a35b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 1 addition & 7 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/schemas/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions pkg/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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...)
Expand Down

0 comments on commit f28a35b

Please sign in to comment.