fix: uploads when using multiple channels

This commit is contained in:
divyam234 2024-05-31 20:28:32 +05:30
parent b7e6af058c
commit 450c7e0487
8 changed files with 24 additions and 16 deletions

View file

@ -0,0 +1,5 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE teldrive.uploads DROP CONSTRAINT IF EXISTS uploads_pk;
ALTER TABLE teldrive.uploads ADD CONSTRAINT uploads_pk PRIMARY KEY (part_id,channel_id);
-- +goose StatementEnd

View file

@ -129,23 +129,26 @@ func (c *CronService) CleanUploads(ctx context.Context) {
Select("JSONB_AGG(uploads.part_id) as parts", "uploads.channel_id", "uploads.user_id", "s.session").
Joins("left join teldrive.users as u on u.user_id = uploads.user_id").
Joins("left join (select * from teldrive.sessions order by created_at desc limit 1) as s on s.user_id = uploads.user_id").
Where("uploads.created_at > ?", time.Now().UTC().Add(c.cnf.TG.Uploads.Retention)).
Where("uploads.created_at < ?", time.Now().UTC().Add(-c.cnf.TG.Uploads.Retention)).
Group("uploads.channel_id").Group("uploads.user_id").Group("s.session").
Scan(&upResults).Error; err != nil {
return
}
for _, result := range upResults {
if result.Session == "" {
break
}
err := services.DeleteTGMessages(ctx, &c.cnf.TG, result.Session, result.ChannelId, result.UserId, result.Parts)
c.logger.Errorw("failed to delete messages", err)
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{})
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{})
}

View file

@ -11,7 +11,7 @@ func ToFileOut(file models.File) *schemas.FileOut {
size = *file.Size
}
return &schemas.FileOut{
ID: file.ID,
Id: file.Id,
Name: file.Name,
Type: file.Type,
MimeType: file.MimeType,

View file

@ -7,7 +7,7 @@ import (
)
type File struct {
ID string `gorm:"type:text;primaryKey;default:generate_uid(16)"`
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"`

View file

@ -38,7 +38,7 @@ type FileIn struct {
}
type FileOut struct {
ID string `json:"id"`
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
MimeType string `json:"mimeType"`

View file

@ -211,7 +211,7 @@ func getParts(ctx context.Context, client *telegram.Client, file *schemas.FileOu
cache := cache.FromContext(ctx)
parts := []types.Part{}
key := fmt.Sprintf("messages:%s:%s", file.ID, userID)
key := fmt.Sprintf("messages:%s:%s", file.Id, userID)
err := cache.Get(key, &parts)

View file

@ -255,7 +255,7 @@ func (fs *FileService) getPathId(path string, userId int64) (string, error) {
return "", database.ErrNotFound
}
return file.ID, nil
return file.Id, nil
}
func (fs *FileService) MakeDirectory(userId int64, payload *schemas.MkDir) (*schemas.FileOut, *types.AppError) {
@ -442,7 +442,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr
dbFile.UserID = userId
dbFile.Starred = false
dbFile.Status = "active"
dbFile.ParentID = dest.ID
dbFile.ParentID = dest.Id
dbFile.ChannelID = &channelId
dbFile.Encrypted = file.Encrypted
dbFile.Category = file.Category
@ -537,7 +537,7 @@ func (fs *FileService) GetFileStream(c *gin.Context) {
c.Header("Content-Type", mimeType)
c.Header("Content-Length", strconv.FormatInt(contentLength, 10))
c.Header("E-Tag", fmt.Sprintf("\"%s\"", md5.FromString(file.ID+strconv.FormatInt(file.Size, 10))))
c.Header("E-Tag", fmt.Sprintf("\"%s\"", md5.FromString(file.Id+strconv.FormatInt(file.Size, 10))))
c.Header("Last-Modified", file.UpdatedAt.UTC().Format(http.TimeFormat))
disposition := "inline"

View file

@ -58,9 +58,9 @@ func TestSuite(t *testing.T) {
func (s *FileServiceSuite) TestSave() {
res, err := s.srv.CreateFile(&gin.Context{}, 123456, s.entry("file.jpeg"))
s.NoError(err.Error)
find, err := s.srv.GetFileByID(res.ID)
find, err := s.srv.GetFileByID(res.Id)
s.NoError(err.Error)
s.Equal(find.ID, res.ID)
s.Equal(find.Id, res.Id)
s.Equal(find.MimeType, res.MimeType)
}
@ -83,7 +83,7 @@ func (s *FileServiceSuite) Test_Update() {
Path: "/dwkd",
Type: "file",
}
r, err := s.srv.UpdateFile(res.ID, 123456, data, nil)
r, err := s.srv.UpdateFile(res.Id, 123456, data, nil)
s.NoError(err.Error)
s.Equal(r.Name, data.Name)
s.Equal(r.Path, data.Path)