From f46ca1634a2e566f662b19ea4338d1d702714c88 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sat, 20 Jun 2020 09:54:32 +0200 Subject: [PATCH] chg: [internal] Use tmp file fro Feed::getCache --- app/Lib/Tools/TmpFileTool.php | 34 ++++++++++++++++++++++++++++++++-- app/Model/Feed.php | 20 ++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/app/Lib/Tools/TmpFileTool.php b/app/Lib/Tools/TmpFileTool.php index f34fa0445..882668956 100644 --- a/app/Lib/Tools/TmpFileTool.php +++ b/app/Lib/Tools/TmpFileTool.php @@ -37,16 +37,34 @@ class TmpFileTool } } + /** + * @return Generator + * @throws Exception + */ + public function lines() + { + $this->rewind(); + while (!feof($this->tmpfile)) { + $result = fgets($this->tmpfile); + if ($result === false) { + throw new Exception('Could not read line from temporary file.'); + } + yield $result; + } + fclose($this->tmpfile); + $this->tmpfile = null; + } + /** * @return string * @throws Exception */ public function finish() { - fseek($this->tmpfile, 0); + $this->rewind(); $final = stream_get_contents($this->tmpfile); if ($final === false) { - throw new Exception("Could not read from temporary file."); + throw new Exception('Could not read from temporary file.'); } fclose($this->tmpfile); $this->tmpfile = null; @@ -61,4 +79,16 @@ class TmpFileTool { return $this->finish(); } + + /** + * Seek to start of file. + * + * @throws Exception + */ + private function rewind() + { + if (fseek($this->tmpfile, 0) === -1) { + throw new Exception('Could not seek to start of temporary file.'); + } + } } diff --git a/app/Model/Feed.php b/app/Model/Feed.php index d1c956859..32f0b4b61 100644 --- a/app/Model/Feed.php +++ b/app/Model/Feed.php @@ -1,6 +1,7 @@ feedGetUri($feed, $uri, $HttpSocket); @@ -173,13 +174,16 @@ class Feed extends AppModel throw new Exception("File '$uri' with hashes for cache filling is empty."); } - $data = trim($data); - $data = explode("\n", $data); - $result = array(); - foreach ($data as $v) { - $result[] = explode(',', $v); + // CSV file can be pretty big to do operations in memory, so we save content to temp and iterate line by line. + $tmpFile = new TmpFileTool(); + $tmpFile->write(trim($data)); + unset($data); + + foreach ($tmpFile->lines() as $line) { + yield explode(',', rtrim($line)); } - return $result; + + return array(); } /**