Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 2e70a40

Browse files
committed
Cleanup temporary file after sheet is pushed
1 parent 47f18c7 commit 2e70a40

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/GoogleSheetPusher.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function __invoke(ExportableToGoogleSheet $export): void
2222
}
2323

2424
$writer = app(Writer::class)->export($export, Excel::CSV);
25-
$handle = fopen($writer->getLocalPath(), 'r');
25+
$temporaryFilePath = $writer->getLocalPath();
26+
27+
$handle = fopen($temporaryFilePath, 'r');
2628

2729
$sheetName = $export->title();
2830

@@ -42,16 +44,21 @@ public function __invoke(ExportableToGoogleSheet $export): void
4244
values: $chunk->values()->all(),
4345
);
4446
});
45-
46-
if (is_resource($handle)) {
47-
fclose($handle);
48-
}
4947
} catch (GoogleException $e) {
5048
// Clear the complete sheet if (a chunk) fails.
5149
// We don't want to end up with a half-filled sheet.
5250
$this->googleSheetService->clearSheet($export->googleSpreadsheetId(), $sheetName);
5351

5452
throw $e;
53+
} finally {
54+
if (is_resource($handle)) {
55+
fclose($handle);
56+
}
57+
58+
// Clean up the local file
59+
if (is_file($temporaryFilePath) && file_exists($temporaryFilePath)) {
60+
unlink($temporaryFilePath);
61+
}
5562
}
5663
}
5764
}

0 commit comments

Comments
 (0)