Skip to content

Commit 5b5fb10

Browse files
authored
Merge pull request #1391 from 0chain/fix/upload-thumbnail
process thumbnail before save
2 parents c8f4be5 + 249501b commit 5b5fb10

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

code/go/0chain.net/blobbercore/handler/file_command_update.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ func (cmd *UpdateFileCommand) ProcessContent(allocationObj *allocation.Allocatio
154154
allocation.UpdateConnectionObjSize(connID, -cmd.existingFileRef.Size)
155155
}
156156

157+
if cmd.thumbFile != nil {
158+
err := cmd.ProcessThumbnail(allocationObj)
159+
if err != nil {
160+
return result, err
161+
}
162+
}
163+
157164
saveChange, err := allocation.SaveFileChange(connID, cmd.fileChanger.PathHash, cmd.fileChanger.Filename, cmd, cmd.fileChanger.IsFinal, cmd.fileChanger.Size, cmd.fileChanger.UploadOffset, fileOutputData.Size)
158165
if err != nil {
159166
return result, err
@@ -162,18 +169,17 @@ func (cmd *UpdateFileCommand) ProcessContent(allocationObj *allocation.Allocatio
162169
allocation.UpdateConnectionObjSize(connID, cmd.fileChanger.Size)
163170
result.UpdateChange = false
164171
}
165-
166-
if allocationObj.BlobberSizeUsed+(allocationSize-cmd.existingFileRef.Size) > allocationObj.BlobberSize {
167-
return result, common.NewError("max_allocation_size", "Max size reached for the allocation with this blobber")
168-
}
169-
170-
if cmd.thumbFile != nil {
171-
err := cmd.ProcessThumbnail(allocationObj)
172+
if cmd.thumbHeader != nil {
173+
err = allocation.SaveFileChanger(connID, &cmd.fileChanger.BaseFileChanger)
172174
if err != nil {
173175
return result, err
174176
}
175177
}
176178

179+
if allocationObj.BlobberSizeUsed+(allocationSize-cmd.existingFileRef.Size) > allocationObj.BlobberSize {
180+
return result, common.NewError("max_allocation_size", "Max size reached for the allocation with this blobber")
181+
}
182+
177183
return result, nil
178184
}
179185

@@ -182,7 +188,6 @@ func (cmd *UpdateFileCommand) ProcessThumbnail(allocationObj *allocation.Allocat
182188
connectionID := cmd.fileChanger.ConnectionID
183189
if cmd.thumbHeader != nil {
184190
defer cmd.thumbFile.Close()
185-
186191
thumbInputData := &filestore.FileInputData{Name: cmd.thumbHeader.Filename, Path: cmd.fileChanger.Path, IsThumbnail: true, FilePathHash: cmd.fileChanger.PathHash}
187192
thumbOutputData, err := filestore.GetFileStore().WriteFile(allocationObj.ID, connectionID, thumbInputData, cmd.thumbFile)
188193
if err != nil {
@@ -191,15 +196,14 @@ func (cmd *UpdateFileCommand) ProcessThumbnail(allocationObj *allocation.Allocat
191196

192197
cmd.fileChanger.ThumbnailSize = thumbOutputData.Size
193198
cmd.fileChanger.ThumbnailFilename = thumbInputData.Name
194-
err = allocation.SaveFileChanger(connectionID, &cmd.fileChanger.BaseFileChanger)
195-
return err
199+
return nil
196200
}
197201
return common.ErrNoThumbnail
198202
}
199203

200204
func (cmd *UpdateFileCommand) reloadChange() {
201205
changer := allocation.GetFileChanger(cmd.fileChanger.ConnectionID, cmd.fileChanger.PathHash)
202-
if changer != nil {
206+
if changer != nil && changer.ThumbnailHash != "" {
203207
cmd.fileChanger.ThumbnailFilename = changer.ThumbnailFilename
204208
cmd.fileChanger.ThumbnailSize = changer.ThumbnailSize
205209
cmd.fileChanger.ThumbnailHash = changer.ThumbnailHash

code/go/0chain.net/blobbercore/handler/file_command_upload.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ func (cmd *UploadFileCommand) ProcessContent(allocationObj *allocation.Allocatio
168168
}
169169
}
170170

171+
if cmd.thumbFile != nil {
172+
err := cmd.ProcessThumbnail(allocationObj)
173+
if err != nil {
174+
logging.Logger.Error("UploadFileCommand.ProcessContent", zap.Error(err))
175+
return result, err
176+
}
177+
}
178+
171179
saveChange, err := allocation.SaveFileChange(connectionID, cmd.fileChanger.PathHash, cmd.fileChanger.Filename, cmd, cmd.fileChanger.IsFinal, cmd.fileChanger.Size, cmd.fileChanger.UploadOffset, fileOutputData.Size)
172180
if err != nil {
173181
logging.Logger.Error("UploadFileCommand.ProcessContent", zap.Error(err))
@@ -177,19 +185,17 @@ func (cmd *UploadFileCommand) ProcessContent(allocationObj *allocation.Allocatio
177185
allocation.UpdateConnectionObjSize(connectionID, cmd.fileChanger.Size)
178186
result.UpdateChange = false
179187
}
180-
181-
if allocationObj.BlobberSizeUsed+allocationSize > allocationObj.BlobberSize {
182-
return result, common.NewError("max_allocation_size", "Max size reached for the allocation with this blobber")
183-
}
184-
185-
if cmd.thumbFile != nil {
186-
err := cmd.ProcessThumbnail(allocationObj)
188+
if cmd.thumbHeader != nil {
189+
err = allocation.SaveFileChanger(connectionID, &cmd.fileChanger.BaseFileChanger)
187190
if err != nil {
188-
logging.Logger.Error("UploadFileCommand.ProcessContent", zap.Error(err))
189191
return result, err
190192
}
191193
}
192194

195+
if allocationObj.BlobberSizeUsed+allocationSize > allocationObj.BlobberSize {
196+
return result, common.NewError("max_allocation_size", "Max size reached for the allocation with this blobber")
197+
}
198+
193199
return result, nil
194200
}
195201

@@ -207,14 +213,14 @@ func (cmd *UploadFileCommand) ProcessThumbnail(allocationObj *allocation.Allocat
207213

208214
cmd.fileChanger.ThumbnailSize = thumbOutputData.Size
209215
cmd.fileChanger.ThumbnailFilename = thumbInputData.Name
210-
return allocation.SaveFileChanger(connectionID, &cmd.fileChanger.BaseFileChanger)
216+
return nil
211217
}
212218
return common.ErrNoThumbnail
213219
}
214220

215221
func (cmd *UploadFileCommand) reloadChange() {
216222
changer := allocation.GetFileChanger(cmd.fileChanger.ConnectionID, cmd.fileChanger.PathHash)
217-
if changer != nil {
223+
if changer != nil && changer.ThumbnailHash != "" {
218224
cmd.fileChanger.ThumbnailFilename = changer.ThumbnailFilename
219225
cmd.fileChanger.ThumbnailSize = changer.ThumbnailSize
220226
cmd.fileChanger.ThumbnailHash = changer.ThumbnailHash

0 commit comments

Comments
 (0)