function mkdir_ex($path, $mod = '0777') { if (!is_dir($path)) { mkdir_ex(dirname($path), $mod); mkdir($path, $mod); } }
/** * download images of an archive * @param Archive $archive */ protected function download_archive_images(Archive $archive) { $archive_start_at = microtime(true); $this->site->parse_archive($archive); $all_count = count($archive->images); $done_count = 0; $digit = strlen($all_count); $digit = $digit > 3 ? $digit : 3; $this->log('archive_start', date('Y-m-d H:i:s', $archive_start_at) . ' ' . $archive->id . " {$all_count} images start"); for ($i = 0; $i < $all_count; $i++) { if (PHP_OS == 'WINNT') { $title = iconv(strtoupper($archive->site->charset), strtoupper($GLOBALS['app_config']['win_charset']), $archive->title); } if ($archive->cover_mode) { $archive_folder = ''; $file_name = $archive->id . '_' . $title . '.' . get_file_ext($archive->images[$i]); // we need a shorter $file_id to write in log because $file_name would be too long sometimes. $file_id = $archive->id . '_.' . get_file_ext($archive->images[$i]); } else { $archive_folder = $archive->id . '_' . $title . '/'; $file_name = $archive->id . '_' . sprintf("%0{$digit}d", $i + 1) . '.' . get_file_ext($archive->images[$i]); $file_id = $file_name; } $save_path = ROOTPATH . 'storage/images/' . $this->config['save_folder'] . $archive_folder; mkdir_ex($save_path); $file = $save_path . $file_name; if (file_exists($file)) { $done_count++; continue; } // to do: detect file size & type $image_start_at = microtime(true); $result = curl_download($archive->images[$i], $file); $image_end_at = microtime(true); if ($result['status']['flag']) { $line = date('Y-m-d H:i:s', $image_end_at) . ' ' . $file_id . ' ok ' . sprintf('%.1fs', $image_end_at - $image_start_at); $this->log('image_done', $line); $done_count++; } else { unlink($file); $line = date('Y-m-d H:i:s', $image_end_at) . ' ' . $file_id . ' error ' . sprintf('%.1fs', $image_end_at - $image_start_at); $this->log('image_error', $line); } // usleep(mt_rand(200, 500)); } $archive_end_at = microtime(true); if ($done_count < $all_count) { $line = date('Y-m-d H:i:s', $archive_end_at) . ' ' . $archive->id . " {$done_count}/{$all_count}" . ' done(need redo) ' . sprintf('%.1fs', $archive_end_at - $archive_start_at); $this->log('archive_done', $line); return false; } else { $line = date('Y-m-d H:i:s', $archive_end_at) . ' ' . $archive->id . " {$done_count}/{$all_count}" . ' done ' . sprintf('%.1fs', $archive_end_at - $archive_start_at); $this->log('archive_done', $line); return true; } }