Skip to content

Commit

Permalink
refactor(controller,files): Create a function to detect simicolon or …
Browse files Browse the repository at this point in the history
…pipe
  • Loading branch information
zak39 committed Jul 30, 2024
1 parent 970587e commit 20da66c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
12 changes: 5 additions & 7 deletions lib/Controller/FileCSVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -273,12 +273,10 @@ public function getFromFiles():JSONResponse {
$this->translate->t('The file must be in <b>CSV format</b>.'),
);
}

$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'),
Expand Down
31 changes: 16 additions & 15 deletions lib/Files/Csv/SeparatorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions lib/Files/FileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

interface FileInterface extends BasicStreamInterface {
public function getPath(): string;
public function getSize(): false|int|float;
}
5 changes: 5 additions & 0 deletions lib/Files/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 11 additions & 4 deletions lib/Files/NextcloudFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

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) {
}

/**
* @return resource|false
* @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.');
Expand All @@ -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());
}
}

0 comments on commit 20da66c

Please sign in to comment.