fix: close source file handle when duplicating attachments

Save the source file handle before calling NewAttachment (which
overwrites attachment.File) and use defer to ensure it gets closed.
This prevents file descriptor leaks on both success and error paths.
This commit is contained in:
kolaente
2026-03-04 16:52:35 +01:00
parent 692357a648
commit 7aad96b199

View File

@@ -119,7 +119,7 @@ func (td *TaskDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
attachment.File = &files.File{ID: attachment.FileID}
if err := attachment.File.LoadFileMetaByID(); err != nil {
if files.IsErrFileDoesNotExist(err) {
continue
continue
}
return err
}
@@ -127,13 +127,12 @@ func (td *TaskDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
return err
}
err := attachment.NewAttachment(s, attachment.File.File, attachment.File.Name, attachment.File.Size, doer)
sourceFile := attachment.File.File
defer sourceFile.Close()
err := attachment.NewAttachment(s, sourceFile, attachment.File.Name, attachment.File.Size, doer)
if err != nil {
return err
}
if attachment.File.File != nil {
_ = attachment.File.File.Close()
}
}
// Create "copied from/to" relation