/**
* @param string $url
* @return string
*/
protected function downloadFile($url, $title)
{
//download image to TMP dir and get $resultFilename
$net = new \Ip\Internal\NetHelper();
$tmpFilename = $net->downloadFile($url, ipFile('file/tmp/'), 'bigstock_' . time());
if (!$tmpFilename) {
return null;
}
//find out file mime type to know required extension
try {
$mime = \Ip\Internal\File\Functions::getMimeType(ipFile('file/tmp/' . $tmpFilename));
switch ($mime) {
case 'image/png':
$ext = '.jpg';
break;
case 'image/gif':
$ext = '.gif';
break;
case 'image/bmp':
$ext = '.bmp';
break;
case 'image/pjpeg':
case 'image/jpeg':
default:
$ext = '.jpg';
break;
}
} catch (\Ip\PhpException $e) {
$ext = '.jpg';
}
//get real nice new file name
$title = \Ip\Internal\File\Functions::cleanupFileName($title);
$words = explode(' ', $title);
$cleanTitle = '';
foreach ($words as $word) {
//limit file name to 30 symbols
if (strlen($cleanTitle . '_' . $word) > 30) {
break;
}
if ($cleanTitle != '') {
$cleanTitle .= '_';
}
$cleanTitle .= $word;
}
if ($cleanTitle == '') {
$cleanTitle = 'file';
}
$niceFileName = $cleanTitle . $ext;
$destinationDir = ipFile('file/repository/');
$destinationFileName = \Ip\Internal\File\Functions::genUnoccupiedName($niceFileName, $destinationDir);
copy(ipFile('file/tmp/' . $tmpFilename), $destinationDir . $destinationFileName);
unlink(ipFile('file/tmp/' . $tmpFilename));
$browserModel = \Ip\Internal\Repository\BrowserModel::instance();
$file = $browserModel->getFile($destinationFileName);
return $file;
}