From 20da66c4f519cb83719e113eab586d970bd5c5af Mon Sep 17 00:00:00 2001 From: zak39 Date: Tue, 30 Jul 2024 17:54:08 +0200 Subject: [PATCH] refactor(controller,files): Create a function to detect simicolon or pipe --- lib/Controller/FileCSVController.php | 12 +++++------ lib/Files/Csv/SeparatorDetector.php | 31 ++++++++++++++-------------- lib/Files/FileInterface.php | 1 + lib/Files/FileUploader.php | 5 +++++ lib/Files/NextcloudFile.php | 15 ++++++++++---- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/Controller/FileCSVController.php b/lib/Controller/FileCSVController.php index 01ad8f679..47d35d8ec 100644 --- a/lib/Controller/FileCSVController.php +++ b/lib/Controller/FileCSVController.php @@ -99,8 +99,8 @@ public function import(): JSONResponse { } $fileUploader = new FileUploader($file['tmp_name']); - - if (!SeparatorDetector::isComma($fileUploader)) { + + if (!SeparatorDetector::isComma($fileUploader) || !SeparatorDetector::isCommaForAllFile($fileUploader)) { throw new InvalidSeparatorCsvException( $this->translate->t('Invalid separator for CSV file'), $this->translate->t('Your CSV file must use a comma (",") as separator'), @@ -273,12 +273,10 @@ public function getFromFiles():JSONResponse { $this->translate->t('The file must be in CSV format.'), ); } + + $nextcloudFile = new NextcloudFile($file); - $fullPath = $file->getInternalPath(); - - $nextcloudFile = new NextcloudFile($fullPath, $file->getStorage()); - - if (!SeparatorDetector::isComma($nextcloudFile)) { + if (!SeparatorDetector::isComma($nextcloudFile) || !SeparatorDetector::isCommaForAllFile($nextcloudFile)) { throw new InvalidSeparatorCsvException( $this->translate->t('Invalid separator for CSV file'), $this->translate->t('Your CSV file must use a comma (",") as separator'), diff --git a/lib/Files/Csv/SeparatorDetector.php b/lib/Files/Csv/SeparatorDetector.php index e012e796c..bd5005e8a 100644 --- a/lib/Files/Csv/SeparatorDetector.php +++ b/lib/Files/Csv/SeparatorDetector.php @@ -12,23 +12,24 @@ public static function isComma(FileInterface $file): bool { $firstLine = fread($handle, self::SIZE); - $lines = file($file->getPath(), FILE_SKIP_EMPTY_LINES); - $totalCount = count($lines); - - $separatorsAsComma = array_filter( - $lines, - fn ($line) => str_contains($line, Separator::COMMA) - ); - - $commasCount = count($separatorsAsComma); - $file->close(); - $isComma = - (strpos($firstLine, Separator::COMMA) !== false) - && ($totalCount === $commasCount) - ; + return strpos($firstLine, Separator::COMMA) !== false; + } - return $isComma; + public static function isCommaForAllFile(FileInterface $file): bool { + $handle = $file->open(); + $lines = fread($handle, $file->getSize()); + $nbPipes = substr_count($lines, '|'); + $nbSemiColons = substr_count($lines, ';'); + + if ( + $nbPipes > 0 + || $nbSemiColons > 0 + ) { + return false; + } + + return true; } } diff --git a/lib/Files/FileInterface.php b/lib/Files/FileInterface.php index c11b35f9b..01230d7e7 100644 --- a/lib/Files/FileInterface.php +++ b/lib/Files/FileInterface.php @@ -4,4 +4,5 @@ interface FileInterface extends BasicStreamInterface { public function getPath(): string; + public function getSize(): false|int|float; } diff --git a/lib/Files/FileUploader.php b/lib/Files/FileUploader.php index 2859db061..c21ad1aae 100644 --- a/lib/Files/FileUploader.php +++ b/lib/Files/FileUploader.php @@ -29,4 +29,9 @@ public function close(): bool { public function getPath(): string { return $this->path; } + + public function getSize(): false|int|float + { + return filesize($this->path); + } } diff --git a/lib/Files/NextcloudFile.php b/lib/Files/NextcloudFile.php index ea70c30e8..44ced4f4d 100644 --- a/lib/Files/NextcloudFile.php +++ b/lib/Files/NextcloudFile.php @@ -2,12 +2,12 @@ namespace OCA\Workspace\Files; -use OCP\Files\Storage\IStorage; +use OCP\Files\Node; class NextcloudFile implements FileInterface { private $resource; - public function __construct(private string $path, private IStorage $store) { + public function __construct(private Node $file) { } /** @@ -15,7 +15,8 @@ public function __construct(private string $path, private IStorage $store) { * @throws \Exception */ public function open(?string $path = null) { - $this->resource = $this->store->fopen($this->path, "r"); + $store = $this->file->getStorage(); + $this->resource = $store->fopen($this->file->getInternalPath(), "r"); if (!$this->resource) { throw new \Exception('Something went wrong. Couldn\'t open the file.'); @@ -29,6 +30,12 @@ public function close(): bool { } public function getPath(): string { - return $this->path; + return $this->file->getInternalPath(); + } + + public function getSize(): false|int|float + { + $store = $this->file->getStorage(); + return $store->filesize($this->file->getInternalPath()); } }