コード例 #1
0
ファイル: Add.php プロジェクト: 4otaku/api
 protected function process($params)
 {
     $fetchMeta = false;
     if (reset($params) == 'протегай') {
         array_shift($params);
         $fetchMeta = true;
     }
     $url = empty($params) ? $this->fetchUrlFromDB() : $this->fetchUrlFromParams($params);
     if (empty($url)) {
         return "Не удалось найти валидный url для загрузки";
     }
     $request = new ApiRequestInner(array('file' => $url));
     $worker = new ApiUploadArt($request);
     $worker->process_request();
     $data = $worker->get_response();
     $file = reset($data['files']);
     if (!empty($file['error_code'])) {
         if ($file['error_code'] == 30) {
             return "Арт уже есть под номером <http://art.4otaku.org/{$file['error_text']}/|{$file['error_text']}>";
         } else {
             return "Не удалось скачать файл {$url}";
         }
     }
     $file = $this->addCookie($file);
     $request = new ApiRequestInner($file);
     $worker = new ApiCreateArt($request);
     $worker->process_request();
     $data = $worker->get_response();
     $error = reset($data['errors']);
     if (!empty($error)) {
         if ($error['code'] == 30) {
             return "Арт уже есть под номером {$error['message']}";
         } else {
             return "Произошла неизвестная ошибка, приносим свои извинения";
         }
     }
     $key = substr($file["upload_key"], 0, 32);
     if ($fetchMeta) {
         try {
             $this->fetchMeta($data['id'], $key);
             $error = "";
         } catch (Error $e) {
             $error = "\n" . $e->getMessage();
         }
     }
     return "Успешно добавлено как <http://art.4otaku.org/{$data['id']}/|{$data['id']}>\n" . "http://images.4otaku.org/art/" . $key . "_largethumb.jpg" . $error;
 }
コード例 #2
0
ファイル: Upload.php プロジェクト: 4otaku/art
 protected function make_request()
 {
     $url = Config::getInstance()->get('api', 'url');
     $link = false;
     foreach ($_FILES as $file) {
         foreach ((array) $file['type'] as $key => $type) {
             if (strpos($type, 'link') !== false) {
                 $link = file_get_contents($file['tmp_name'][$key]);
                 break 2;
             }
         }
     }
     if ($link) {
         if (!Config::getInstance()->get('api', 'inner')) {
             $request = new Request('upload_art', $this, ['file' => $link]);
             $request->perform();
         } else {
             $api = new ApiUploadArt(new ApiRequestInner(['file' => $link]));
             $response = $api->set_base_path(API_IMAGES)->process_request()->get_response();
             $this->recieve_data($response);
         }
         return array();
     }
     // Hacked because of file send
     if (!Config::getInstance()->get('api', 'inner')) {
         $url .= '/upload/art';
         $post = [];
         foreach ($_FILES['file']['tmp_name'] as $key => $file) {
             $post['file' . $key] = '@' . $file . ';filename=' . $_FILES['file']['name'][$key] . ';type=' . $_FILES['file']['type'][$key];
         }
         $header = ['Content-type: multipart/form-data'];
         $ch = curl_init($url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_ENCODING, '');
         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
         $response = curl_exec($ch);
         curl_close($ch);
     } else {
         $api = new ApiUploadArt(new ApiRequest('dummy'));
         $response = $api->set_base_path(API_IMAGES)->process_request()->get_response();
     }
     $this->recieve_data(json_decode($response, true));
     return array();
 }