mirror of https://github.com/MISP/MISP
chg: [internal] Use tmp file fro Feed::getCache
parent
3d12cb3f3c
commit
f46ca1634a
|
@ -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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
App::uses('AppModel', 'Model');
|
||||
App::uses('RandomTool', 'Tools');
|
||||
App::uses('TmpFileTool', 'Tools');
|
||||
|
||||
class Feed extends AppModel
|
||||
{
|
||||
|
@ -161,10 +162,10 @@ class Feed extends AppModel
|
|||
/**
|
||||
* @param array $feed
|
||||
* @param HttpSocket $HttpSocket
|
||||
* @return array
|
||||
* @return Generator|array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCache($feed, $HttpSocket)
|
||||
public function getCache(array $feed, HttpSocket $HttpSocket)
|
||||
{
|
||||
$uri = $feed['Feed']['url'] . '/hashes.csv';
|
||||
$data = $this->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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue