Skip to content

Commit

Permalink
fix: cache invalidation in file update
Browse files Browse the repository at this point in the history
  • Loading branch information
bhunter234 committed Jan 8, 2025
1 parent 126a2c4 commit ffa9a27
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions pkg/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
return nil, &apiError{err: errors.New("file not found"), code: 404}
}

file := mapper.ToFileOut(res[0], true)
file := res[0]

newIds := []api.Part{}

Expand All @@ -138,7 +138,7 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
for _, part := range file.Parts {
ids = append(ids, int(part.ID))
}
messages, err := tgc.GetMessages(ctx, client.API(), ids, file.ChannelId.Value)
messages, err := tgc.GetMessages(ctx, client.API(), ids, *file.ChannelID)

if err != nil {
return err
Expand Down Expand Up @@ -179,7 +179,11 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
}

}
newIds = append(newIds, api.Part{ID: msg.ID, Salt: file.Parts[i].Salt})
p := api.Part{ID: msg.ID}
if file.Parts[i].Salt.Value != "" {
p.Salt = file.Parts[i].Salt
}
newIds = append(newIds, p)

}
return nil
Expand All @@ -189,6 +193,10 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
return nil, &apiError{err: err}
}

if len(newIds) != len(file.Parts) {
return nil, &apiError{err: errors.New("failed to copy all file parts")}
}

var parentId string
if !isUUID(req.Destination) {
var destRes []models.File
Expand All @@ -204,19 +212,28 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
dbFile := models.File{}

dbFile.Name = req.NewName.Or(file.Name)
dbFile.Size = utils.Ptr(file.Size.Value)
dbFile.Size = file.Size
dbFile.Type = string(file.Type)
dbFile.MimeType = file.MimeType.Or(defaultContentType)
dbFile.Parts = datatypes.NewJSONSlice(newIds)
dbFile.MimeType = file.MimeType
if len(newIds) > 0 {
dbFile.Parts = datatypes.NewJSONSlice(newIds)
}
dbFile.UserID = userId
dbFile.Status = "active"
dbFile.ParentID = sql.NullString{
String: parentId,
Valid: true,
}
dbFile.ChannelID = &channelId
dbFile.Encrypted = file.Encrypted.Value
dbFile.Category = string(file.Category.Value)
dbFile.Encrypted = file.Encrypted
dbFile.Category = string(file.Category)
if req.UpdatedAt.IsSet() && !req.UpdatedAt.Value.IsZero() {
dbFile.UpdatedAt = req.UpdatedAt.Value
dbFile.CreatedAt = req.UpdatedAt.Value
} else {
dbFile.UpdatedAt = time.Now().UTC()
dbFile.CreatedAt = time.Now().UTC()
}

if err := a.db.Create(&dbFile).Error; err != nil {
return nil, &apiError{err: err}
Expand All @@ -236,8 +253,12 @@ func (a *apiService) FilesCreate(ctx context.Context, fileIn *api.File) (*api.Fi
channelId int64
)

if fileIn.Path.Value == "" && fileIn.ParentId.Value == "" {
return nil, &apiError{err: errors.New("parent id or path is required"), code: 409}
}

if fileIn.Path.Value != "" {
path = strings.ReplaceAll(path, "//", "/")
path = strings.ReplaceAll(fileIn.Path.Value, "//", "/")
if path != "/" {
path = strings.TrimSuffix(path, "/")
}
Expand All @@ -258,8 +279,6 @@ func (a *apiService) FilesCreate(ctx context.Context, fileIn *api.File) (*api.Fi
Valid: true,
}

} else {
return nil, &apiError{err: errors.New("parent id or path is required"), code: 409}
}

if fileIn.Type == "folder" {
Expand Down Expand Up @@ -295,6 +314,13 @@ func (a *apiService) FilesCreate(ctx context.Context, fileIn *api.File) (*api.Fi
fileDB.UserID = userId
fileDB.Status = "active"
fileDB.Encrypted = fileIn.Encrypted.Value
if fileIn.UpdatedAt.IsSet() && !fileIn.UpdatedAt.Value.IsZero() {
fileDB.UpdatedAt = fileIn.UpdatedAt.Value
fileDB.CreatedAt = fileIn.UpdatedAt.Value
} else {
fileDB.UpdatedAt = time.Now().UTC()
fileDB.CreatedAt = time.Now().UTC()
}
if err := a.db.Create(&fileDB).Error; err != nil {
if database.IsKeyConflictErr(err) {
return nil, &apiError{err: errors.New("file already exists"), code: 409}
Expand Down Expand Up @@ -491,9 +517,9 @@ func (a *apiService) FilesUpdate(ctx context.Context, req *api.FileUpdate, param
if req.Size.Value != 0 {
updateDb.Size = utils.Ptr(req.Size.Value)
}
if req.UpdatedAt.IsSet() {
if req.UpdatedAt.IsSet() && !req.UpdatedAt.Value.IsZero() {
updateDb.UpdatedAt = req.UpdatedAt.Value
} else {
} else if !req.UpdatedAt.IsSet() && params.Skiputs.Value == "0" {
updateDb.UpdatedAt = time.Now().UTC()
}

Expand Down Expand Up @@ -578,9 +604,9 @@ func (a *apiService) FilesUpdateParts(ctx context.Context, req *api.FilePartsUpd
}
client, _ := tgc.AuthClient(ctx, &a.cnf.TG, session, a.middlewares...)
tgc.DeleteMessages(ctx, client, *file.ChannelID, ids)
keys := []string{fmt.Sprintf("files:%s", params.ID), fmt.Sprintf("files:messages:%s:%d", params.ID, userId)}
keys := []string{fmt.Sprintf("files:%s", params.ID), fmt.Sprintf("files:messages:%s", params.ID)}
for _, part := range file.Parts {
keys = append(keys, fmt.Sprintf("files:location:%d:%s:%d", userId, params.ID, part.ID))
keys = append(keys, fmt.Sprintf("files:location:%s:%d", params.ID, part.ID))

}
a.cache.Delete(keys...)
Expand Down

0 comments on commit ffa9a27

Please sign in to comment.