Skip to content

Commit

Permalink
Cleanup temporary file after sheet is pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Jun 18, 2024
1 parent 47f18c7 commit 2e70a40
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/GoogleSheetPusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public function __invoke(ExportableToGoogleSheet $export): void
}

$writer = app(Writer::class)->export($export, Excel::CSV);
$handle = fopen($writer->getLocalPath(), 'r');
$temporaryFilePath = $writer->getLocalPath();

$handle = fopen($temporaryFilePath, 'r');

$sheetName = $export->title();

Expand All @@ -42,16 +44,21 @@ public function __invoke(ExportableToGoogleSheet $export): void
values: $chunk->values()->all(),
);
});

if (is_resource($handle)) {
fclose($handle);
}
} catch (GoogleException $e) {
// Clear the complete sheet if (a chunk) fails.
// We don't want to end up with a half-filled sheet.
$this->googleSheetService->clearSheet($export->googleSpreadsheetId(), $sheetName);

throw $e;
} finally {
if (is_resource($handle)) {
fclose($handle);
}

// Clean up the local file
if (is_file($temporaryFilePath) && file_exists($temporaryFilePath)) {
unlink($temporaryFilePath);
}
}
}
}

0 comments on commit 2e70a40

Please sign in to comment.