Skip to content

Commit

Permalink
fix: excel file handling when there's CSV data within the xlsx
Browse files Browse the repository at this point in the history
closes #196
  • Loading branch information
g105b committed Jan 16, 2025
1 parent a179ceb commit 5ae4736
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions class/Upload/UploadRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public function create(User $user, FileUpload...$uploadList):array {
$extension = pathinfo($targetPath, PATHINFO_EXTENSION);
if($this->isCsv($targetPath) && $extension !== "csv") {
rename($targetPath, "$targetPath.csv");
$targetPath = "$targetPath.csv";
}
elseif($this->isTsv($targetPath) && $extension !== "tsv") {
rename($targetPath, "$targetPath.tsv");
$targetPath = "$targetPath.tsv";
}

Log::debug("Detected upload type: $uploadType");
Expand Down Expand Up @@ -200,6 +202,11 @@ private function detectUploadType(string $uploadedFilePath):string {
}

private function isCsv(string $filePath, string $separator = ","):bool {
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if($extension === "xlsx") {
return false;
}

$fh = fopen($filePath, "r");
$firstLine = fgetcsv($fh, separator: $separator);
$secondLine = fgetcsv($fh, separator: $separator);
Expand Down

0 comments on commit 5ae4736

Please sign in to comment.