Example #1
0
 public function store($file, $desiredName)
 {
     if (!$this->uploadUrl) {
         throw new UnsupportedMethodException('Don`t know how to store file!');
     }
     $sendRequest = HttpRequest::create()->setMethod(HttpMethod::post())->setUrl(HttpUrl::create()->parse($this->uploadUrl));
     $options = array_merge($this->uploadOptions, array($this->uploadFieldName => '@' . $file));
     $curl = CurlHttpClient::create()->setOption(CURLOPT_POSTFIELDS, $options);
     $upload = function () use($curl, $sendRequest) {
         $resp = $curl->send($sendRequest);
         return $resp;
     };
     $resp = $this->tryToDo($upload, "Tried to upload file but something happened: %s");
     return $resp->getBody();
 }
 protected function unlink($file)
 {
     $sendRequest = HttpRequest::create()->setMethod(HttpMethod::delete())->setUrl(HttpUrl::create()->parse($this->getUploadLink($file)));
     /** @var CurlHttpResponse $resp */
     $curl = CurlHttpClient::create()->setOption(CURLOPT_CUSTOMREQUEST, "DELETE")->setOption(CURLOPT_TIMEOUT, 25);
     if (is_array($this->uploadOptions) && isset($this->uploadOptions['userpwd'])) {
         $curl->setOption(CURLOPT_HTTPAUTH, CURLAUTH_ANY)->setOption(CURLOPT_USERPWD, $this->uploadOptions['userpwd']);
     }
     $delete = function () use($curl, $sendRequest) {
         $response = $curl->send($sendRequest);
         $status = $response->getStatus()->getId();
         if ($status < 200 || $status >= 400) {
             throw new MissingElementException("Got HTTP response code {$status}");
         }
     };
     $this->tryToDo($delete, "File ({$file}) was not deleted, reason: %s");
     return true;
 }
Example #3
0
 protected function httpStat($url)
 {
     $result = array();
     try {
         $sendRequest = HttpRequest::create()->setMethod(HttpMethod::get())->setUrl(HttpUrl::create()->parse($url));
         $res = CurlHttpClient::create()->setOption(CURLOPT_RETURNTRANSFER, true)->setOption(CURLOPT_NOBODY, true)->send($sendRequest);
         $result['mime'] = $res->getHeader('content-type');
         $result['size'] = $res->getHeader('content-length');
     } catch (Exception $e) {
     }
     return $result;
 }