Merge pull request #288 from rvanginneken/fix-duplicate-file-retrieval

Fix duplicate file retrieval
This commit is contained in:
Nicola Asuni 2021-03-27 08:40:41 +00:00 committed by GitHub
commit 843235b4d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1832,6 +1832,14 @@ class TCPDF {
*/
protected $gdgammacache = array();
/**
* Cache array for file content
* @protected
* @var array
* @sinde 6.3.5 (2020-09-28)
*/
protected $fileContentCache = array();
//------------------------------------------------------------
// METHODS
//------------------------------------------------------------
@ -4890,7 +4898,7 @@ class TCPDF {
}
reset($this->embeddedfiles);
foreach ($this->embeddedfiles as $filename => $filedata) {
$data = TCPDF_STATIC::fileGetContents($filedata['file']);
$data = $this->getCachedFileContents($filedata['file']);
if ($data !== FALSE) {
$rawsize = strlen($data);
if ($rawsize > 0) {
@ -6910,18 +6918,14 @@ class TCPDF {
$exurl = $file;
}
// check if file exist and it is valid
if (!@TCPDF_STATIC::file_exists($file)) {
if (!@$this->fileExists($file)) {
return false;
}
if (($imsize = @getimagesize($file)) === FALSE) {
if (in_array($file, $this->imagekeys)) {
// get existing image data
$info = $this->getImageBuffer($file);
$imsize = array($info['w'], $info['h']);
} elseif (strpos($file, '__tcpdf_'.$this->file_id.'_img') === FALSE) {
$imgdata = TCPDF_STATIC::fileGetContents($file);
}
}
if (false !== $info = $this->getImageBuffer($file)) {
$imsize = array($info['w'], $info['h']);
} elseif (($imsize = @getimagesize($file)) === FALSE && strpos($file, '__tcpdf_'.$this->file_id.'_img') === FALSE){
$imgdata = $this->getCachedFileContents($file);
}
}
if (!empty($imgdata)) {
// copy image to cache
@ -7126,7 +7130,7 @@ class TCPDF {
$svgimg = substr($file, 1);
} else {
// get SVG file content
$svgimg = TCPDF_STATIC::fileGetContents($file);
$svgimg = $this->getCachedFileContents($file);
}
if ($svgimg !== FALSE) {
// get width and height
@ -14942,7 +14946,7 @@ class TCPDF {
if ($file[0] === '@') { // image from string
$data = substr($file, 1);
} else { // EPS/AI file
$data = TCPDF_STATIC::fileGetContents($file);
$data = $this->getCachedFileContents($file);
}
if ($data === FALSE) {
$this->Error('EPS file not found: '.$file);
@ -16361,7 +16365,7 @@ class TCPDF {
$type = array();
if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
// read CSS data file
$cssdata = TCPDF_STATIC::fileGetContents(trim($type[1]));
$cssdata = $this->getCachedFileContents(trim($type[1]));
if (($cssdata !== FALSE) AND (strlen($cssdata) > 0)) {
$css = array_merge($css, TCPDF_STATIC::extractCSSproperties($cssdata));
}
@ -22833,7 +22837,7 @@ Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value:
$svgdata = substr($file, 1);
} else { // SVG file
$this->svgdir = dirname($file);
$svgdata = TCPDF_STATIC::fileGetContents($file);
$svgdata = $this->getCachedFileContents($file);
}
if ($svgdata === FALSE) {
$this->Error('SVG file not found: '.$file);
@ -24628,6 +24632,33 @@ Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value:
// --- END SVG METHODS -----------------------------------------------------
/**
* Keeps files in memory, so it doesn't need to downloaded everytime in a loop
* @param string $file
* @return string
*/
protected function getCachedFileContents($file)
{
if (!isset($this->fileContentCache[$file])) {
$this->fileContentCache[$file] = TCPDF_STATIC::fileGetContents($file);
}
return $this->fileContentCache[$file];
}
/**
* Avoid multiple calls to an external server to see if a file exists
* @param string $file
* @return bool
*/
protected function fileExists($file)
{
if (isset($this->fileContentCache[$file]) || false !== $this->getImageBuffer($file)) {
return true;
}
return TCPDF_STATIC::file_exists($file);
}
} // END OF TCPDF CLASS
//============================================================+