Created
February 19, 2018 17:14
-
-
Save 1d10t/2595ce72bc2d9f4c092f32f94f4e1f3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* @param string $url | |
* @param integer $i | |
* @param array $userdata compact('data_type', 'prefix', 'data')) | |
*/ | |
protected function saveImageToYandexDisk(&$url, $i, $userdata = array()){ | |
static $config = null; | |
static $yandex_disk_client = null; | |
static $dirs = array(); | |
if(!$config){ | |
$config = ParserConfig::getInstance(); | |
} | |
if(!$yandex_disk_client){ | |
$yandex_disk_client = new DiskClient($config['yandex']['global']['token']); | |
$yandex_disk_client->setServiceScheme(DiskClient::HTTPS_SCHEME); | |
} | |
$image_contents = $this->get($url, '', array(CURLOPT_REFERER => $userdata['referer'])); | |
if(!($isize = getimagesizefromstring($image_contents))) | |
return; | |
switch ($isize[2]) { | |
case IMAGETYPE_GIF: | |
$ext = 'gif'; | |
break; | |
case IMAGETYPE_JPEG: | |
$ext = 'jpg'; | |
break; | |
case IMAGETYPE_JPEG2000: | |
$ext = 'jpeg'; | |
break; | |
case IMAGETYPE_PNG: | |
$ext = 'png'; | |
break; | |
case IMAGETYPE_BMP: | |
$ext = 'bmp'; | |
break; | |
case IMAGETYPE_WBMP: | |
$ext = 'wbmp'; | |
break; | |
case IMAGETYPE_ICO: | |
$ext = 'ico'; | |
break; | |
case IMAGETYPE_WEBP: | |
$ext = 'webp'; | |
break; | |
default: | |
$ext = 'bin'; | |
break; | |
} | |
$date = explode(' ', $userdata['data'][$userdata['prefix'].'added']); | |
$date = array_shift($date); | |
$par_id = $userdata['data']['par_id']; | |
$ext_id = $userdata['data'][$userdata['prefix'].'ext_id']; | |
$path = "$par_id/{$userdata['data_type']}/$date/$ext_id/"; | |
$dir = $config['yandex']['disk']['images_dir'] . $path; | |
$dir_parts = explode('/', $dir); | |
for($pi = 2; $pi < count($dir_parts); $pi++){ | |
$tdir_parts = array_slice($dir_parts, 0, $pi); | |
$tdir = join('/', $tdir_parts) . '/'; | |
if(!isset($dirs[$tdir])){ | |
try{ | |
if($this->debug_mode){ | |
echo "create dir: ".$tdir."\r\n"; | |
} | |
$yandex_disk_client->createDirectory($tdir); | |
}catch(\Yandex\Disk\Exception\DiskRequestException $e){ | |
if($e->getCode() == 405){ | |
if($this->debug_mode){ | |
echo "dir exists: ".$tdir."\r\n"; | |
} | |
} | |
} | |
} | |
$dirs[$tdir] = true; | |
} | |
$temporary_file_path = tempnam( | |
$this->tmpFilePath(''), | |
'image_'.$userdata['data']['par_id'].'_'.getmypid().'_' | |
); | |
file_put_contents($temporary_file_path, $image_contents); | |
$file_name = basename($temporary_file_path) . '.' . $ext; | |
$ok = false; | |
$file = array( | |
'name' => $file_name, | |
'size' => strlen($image_contents), | |
'path' => $temporary_file_path, | |
); | |
if($this->debug_mode){ | |
echo "uploading file:\r\n"; | |
var_dump(compact('dir', 'file')); | |
} | |
if($yandex_disk_client->uploadFile($dir, $file)){ | |
$url = $config['yandex']['disk']['images_url'] . $path . $file_name; | |
} | |
unlink($temporary_file_path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment