public function testGetBaseName() { $mediaSource = new HttpSource(); $this->assertEquals('tao.png', $mediaSource->getBaseName(Template::img('tao.png', 'tao'))); $this->assertEquals('tao.png', $mediaSource->getBaseName(Template::img('tao.png?a=b', 'tao'))); $this->setExpectedException(tao_models_classes_FileNotFoundException::class); $mediaSource->getBaseName('http://notevenavaliddomain'); }
/** * * @param unknown $xhtml * @param unknown $destination * @return common_report_Report */ protected function retrieveExternalResources($xhtml, $compiledDirectory) { $authorizedMedia = array("jpg", "jpeg", "png", "gif", "mp3", 'mp4', 'webm', 'swf', 'wma', 'wav', 'css', 'js'); $mediaList = array(); $expr = "/http[s]?:(\\\\)?\\/(\\\\)?\\/[^<'\"&?]+\\.(" . implode('|', $authorizedMedia) . ")/mi"; //take into account json encoded url preg_match_all($expr, $xhtml, $mediaList, PREG_PATTERN_ORDER); $uniqueMediaList = array_unique($mediaList[0]); $report = new common_report_Report(common_report_Report::TYPE_SUCCESS); $source = new HttpSource(); foreach ($uniqueMediaList as $mediaUrl) { // This is a file that has to be stored in the item compilation folder itself... // I do not get why they are all copied. They are all there they were copied from the item module... // But I agree that remote resources (somewhere on the Internet) should be copied via curl. // So if the URL does not matches a place where the TAO server is, we curl the resource and store it. // FileManager files should be considered as remote resources to avoid 404 issues. Indeed, a backoffice // user might delete an image in the filemanager during a delivery campain. This is dangerous. $decodedMediaUrl = str_replace('\\/', '/', $mediaUrl); if (substr($decodedMediaUrl, 0, strlen(ROOT_URL)) != ROOT_URL) { try { $stream = $source->getFileStream($decodedMediaUrl); $localPath = basename($decodedMediaUrl); $compiledDirectory->getFile($localPath)->write($stream); $xhtml = str_replace($mediaUrl, $localPath, $xhtml, $replaced); //replace only when copyFile is successful } catch (\Exception $e) { $report = new common_report_Report(common_report_Report::TYPE_ERROR, __('Failed retrieving %s', $decodedMediaUrl)); break; } } } if ($report->getType() == common_report_Report::TYPE_SUCCESS) { $report->setData($xhtml); } return $report; }
protected function replaceImage($file) { $size = getimagesize($file); if (!$this->dryRun) { $source = new HttpSource(); $newFile = $source->download('http://lorempixel.com/' . $size[0] . '/' . $size[1] . '/'); rename($newFile, $file); } echo '[II] Replaced ' . $file . PHP_EOL; }