refactor: return full path in single file res

This commit is contained in:
divyam234 2024-09-20 13:51:47 +05:30
parent 84d2241858
commit f28a35be1c
5 changed files with 20 additions and 22 deletions

View file

@ -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,

View file

@ -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,
}
}

View file

@ -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 {

View file

@ -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

View file

@ -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...)