Skip to content

Commit 1db1fc3

Browse files
committed
internxt: Refactor Update method to extract original file name and type for improved rollback handling
1 parent 977d4e2 commit 1db1fc3

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

backend/internxt/internxt.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,10 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
735735
isEmptyFile := false
736736
remote := o.remote
737737

738+
origBaseName := filepath.Base(remote)
739+
origName := strings.TrimSuffix(origBaseName, filepath.Ext(origBaseName))
740+
origType := strings.TrimPrefix(filepath.Ext(origBaseName), ".")
741+
738742
// Handle empty file simulation
739743
if src.Size() == 0 {
740744
if !o.f.opt.SimulateEmptyFiles {
@@ -761,8 +765,8 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
761765
return err
762766
}
763767

764-
// === RENAME-BASED ROLLBACK PATTERN ===
765-
// This ensures data safety: old file is preserved until new upload succeeds
768+
// rename based rollback pattern
769+
// old file is preserved until new upload succeeds
766770

767771
var backupUUID string
768772
var backupName, backupType string
@@ -802,11 +806,6 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
802806
if err != nil {
803807
// Upload failed - restore backup if it exists
804808
if backupUUID != "" {
805-
// Extract original name from remote
806-
origBaseName := filepath.Base(remote)
807-
origName := strings.TrimSuffix(origBaseName, filepath.Ext(origBaseName))
808-
origType := strings.TrimPrefix(filepath.Ext(origBaseName), ".")
809-
810809
fs.Debugf(o.f, "Upload failed, attempting to restore backup %s.%s to %s", backupName, backupType, remote)
811810

812811
restoreErr := files.RenameFile(ctx, o.f.cfg, backupUUID,
@@ -826,8 +825,6 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
826825
fs.Debugf(o.f, "Upload succeeded, deleting backup %s.%s (UUID: %s)", backupName, backupType, backupUUID)
827826

828827
if err := files.DeleteFile(ctx, o.f.cfg, backupUUID); err != nil {
829-
// Log warning but don't fail - new file is uploaded successfully
830-
// Backup file becomes orphaned but data integrity is maintained
831828
if !strings.Contains(err.Error(), "404") {
832829
fs.Logf(o.f, "Warning: uploaded new version but failed to delete backup %s.%s (UUID: %s): %v. You may need to manually delete this orphaned file.",
833830
backupName, backupType, backupUUID, err)
@@ -841,7 +838,6 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
841838
o.uuid = meta.UUID
842839
o.size = src.Size()
843840
o.remote = remote
844-
// If this is a simulated empty file, set size to 0 for user-facing operations
845841
if isEmptyFile {
846842
o.size = 0
847843
}

0 commit comments

Comments
 (0)