public function testContentType()
 {
     $upload = new FormUpload(__DIR__ . '/Fixtures/google.png');
     $upload->setName('company[logo]');
     $upload->setContentType('foo/bar');
     $this->assertEquals(array('Content-Disposition: form-data; name="company[logo]"; filename="google.png"', 'Content-Type: foo/bar'), $upload->getHeaders());
 }
 /**
  * @param string $endpoint
  * @param string $content
  * @param array $headers
  * @param array $files
  * @return Response
  */
 public function send($endpoint, $content, array $headers = array(), array $files = array())
 {
     // Make headers buzz friendly
     array_walk($headers, function (&$value, $key) {
         $value = sprintf('%s: %s', $key, $value);
     });
     if ($files) {
         // HTTP query content
         parse_str($content, $fields);
         // Add files to request
         foreach ($files as $key => $items) {
             $fields[$key] = array();
             foreach ($items as $name => $item) {
                 $item = new FormUpload($item);
                 if (!is_numeric($name)) {
                     $item->setName($name);
                 }
                 $fields[$key] = $item;
             }
         }
         $response = $this->browser->submit($endpoint, $fields, RequestInterface::METHOD_POST, array_values($headers));
     } else {
         // JSON content
         $response = $this->browser->post($endpoint, array_values($headers), $content);
     }
     return new Response($response->getStatusCode(), $response->getContent());
 }
 public function testFilenamelessUpload()
 {
     $this->setExpectedException('LogicException');
     $upload = new FormUpload();
     $upload->setContent('foobar');
     $request = new FormRequest();
     $request->setField('user[name]', 'Kris');
     $request->setField('user[image]', $upload);
     $content = $request->getContent();
 }
 /**
  * @param  string $binaryImage
  * @param  string $filename
  * @param  int    $expire
  *
  * @return Response
  */
 public function call($binaryImage, $filename, $expire)
 {
     $response = new Response();
     $image = new FormUpload();
     $image->setFilename($filename);
     $image->setContent($binaryImage);
     $request = $this->buildRequest($image, $expire);
     $this->client->setOption(CURLOPT_TIMEOUT, 10000);
     $this->client->send($request, $response);
     return $this->processResponse($response);
 }
 public function uploadActivity($file)
 {
     $request = new FormRequest(FormRequest::METHOD_POST);
     // Set the request URL
     $url = new Url('https://connect.garmin.com/proxy/upload-service-1.1/json/upload/.fit');
     $url->applyToRequest($request);
     // Set the form fields
     $request->setField('responseContentType', 'text/html');
     $data = new FormUpload($file);
     $data->setContentType('image/x-fits');
     $request->setField('data', $data);
     $response = new Response();
     $this->browser->getClient()->send($request, $response);
     return json_decode($response->getContent(), true);
 }
 /**
  * @param UploadedFile $file
  * @param string $clientName
  * @param string $context
  *
  * @return Response
  *
  * @throws MediaStorageClientApiException
  *
  * @todo config to $context
  */
 public function sendFile(UploadedFile $file, $clientName, $context)
 {
     try {
         $request = new FormRequest();
         $request->setHeaders(['headers' => 'enctype:multipart/form-data']);
         $request->setMethod($this->getClientsConfig($clientName, 'method') ? $this->getClientsConfig($clientName, 'method') : FormRequest::METHOD_POST);
         $request->setHost($this->getClientsConfig($clientName, 'base_url'));
         $formUpload = new FormUpload($file->getRealPath());
         $formUpload->setFilename($file->getClientOriginalName());
         $request->addFields(['binaryContent' => $formUpload, 'context' => $context]);
         $request->setResource($this->getClientsConfig($clientName, 'add_media_url'));
         $this->logger->debug('Send ' . $this->getClientsConfig($clientName, 'base_url') . $this->getClientsConfig($clientName, 'add_media_url'));
         //.' '.$request->getContent()
         /** @var Response $response */
         $response = $this->client->send($request, null);
         $this->logger->debug('Response: ' . $response->getStatusCode() . ' ' . substr($response->getContent(), 0, 300));
     } catch (\Exception $e) {
         throw new MediaStorageClientApiException($e->getMessage());
     }
     return $response;
 }
Пример #7
0
 /**
  * Executes a http request.
  *
  * @param Request $request The http request.
  *
  * @return Response The http response.
  */
 public function execute(Request $request)
 {
     $method = $request->getMethod();
     $endpoint = $request->getEndpoint();
     $params = $request->getParams();
     $headers = $request->getHeaders();
     try {
         if ($method === 'GET') {
             $buzzResponse = $this->client->call($endpoint . '?' . http_build_query($params), $method, $headers, array());
         } else {
             $buzzRequest = new FormRequest();
             $buzzRequest->fromUrl($endpoint);
             $buzzRequest->setMethod($method);
             $buzzRequest->setHeaders($headers);
             foreach ($params as $key => $value) {
                 if ($value instanceof Image) {
                     $value = new FormUpload($value->getData());
                 }
                 $buzzRequest->setField($key, $value);
             }
             $buzzResponse = new BuzzResponse();
             $this->client->send($buzzRequest, $buzzResponse);
         }
     } catch (RequestException $e) {
         throw new Exception($e->getMessage());
     }
     return static::convertResponse($request, $buzzResponse);
 }
 public function provideClientAndUpload()
 {
     $stringUpload = new FormUpload();
     $stringUpload->setFilename('google.png');
     $stringUpload->setContent(file_get_contents(__DIR__ . '/../Message/Fixtures/google.png'));
     $uploads = array($stringUpload, new FormUpload(__DIR__ . '/../Message/Fixtures/google.png'));
     $clients = $this->provideClient();
     $data = array();
     foreach ($clients as $client) {
         foreach ($uploads as $upload) {
             $data[] = array($client[0], $upload);
         }
     }
     return $data;
 }
Пример #9
0
 /**
  * @Given /^that i want to create "([^"]*)" with name "([^"]*)" and content "([^"]*)" with type "([^"]*)"$/
  */
 public function thatIWantToCreateWithNameAndContentWithType($form, $name, $content, $contentType)
 {
     if ($contentType == 'file') {
         $fileName = $content;
         $content = new FormUpload(__DIR__ . '/assets/' . $fileName);
         $content->setName($fileName);
     }
     if (!array_key_exists($form, $this->fields)) {
         $this->fields[$form] = array();
     }
     $this->fields[$form][$name] = $content;
 }
Пример #10
0
 /**
  * Upload file- Not working!!!! - 408 error code...
  *
  * IMPROVE....
  *
  * @param string $destFolderPath Target folder of the file
  * @param boolean $createParents Create parents folder if not exist
  * @param string $filePath Local file to upload
  * @param boolean $overwrite (Optionnal - Default null) null : Set error if file exists, true : overwrite file, false : skip upload if file exist
  * @param \DateTime $mtime (Optionnal) Set the modification time
  * @param \DateTime $crtime (Optionnal) Set the creation time
  * @param \DateTime $atime (Optionnal) Set the access time
  *
  * @return array Response of the request ("json_decoded")
  *
  * @throws PatbzhSynologyException In case synology api sends an error response
  * @throws \\InvalidArgumentException
  */
 public function uploadFile($destFolderPath, $createParents, $filePath, $overwrite = null, $mtime = null, $crtime = null, $atime = null)
 {
     if (!is_string($destFolderPath)) {
         throw new \InvalidArgumentException('$destFolderPath should be a string');
     }
     if (!is_string($filePath)) {
         throw new \InvalidArgumentException('$filePath should be a string');
     }
     if (!is_bool($createParents)) {
         throw new \InvalidArgumentException('$createParents should be a boolean');
     }
     if (!is_null($overwrite) && !is_bool($overwrite)) {
         throw new \InvalidArgumentException('$overwrite should be a boolean');
     }
     if (!is_null($mtime) && !$mtime instanceof \DateTime) {
         throw new \InvalidArgumentException('$mtime should be a \\DateTime');
     }
     if (!is_null($crtime) && !$crtime instanceof \DateTime) {
         throw new \InvalidArgumentException('$crtime should be a \\DateTime');
     }
     if (!is_null($atime) && !$atime instanceof \DateTime) {
         throw new \InvalidArgumentException('$atime should be a \\DateTime');
     }
     $additionalParams['dest_folder_path'] = $destFolderPath;
     if ($createParents) {
         $additionalParams['create_parents'] = 'true';
     }
     if (!$createParents) {
         $additionalParams['create_parents'] = 'false';
     }
     if (!is_null($overwrite) && $createParents) {
         $additionalParams['overwrite'] = 'true';
     }
     if (!is_null($overwrite) && !$createParents) {
         $additionalParams['overwrite'] = 'false';
     }
     if (!is_null($mtime)) {
         $additionalParams['mtime'] = $mtime->getTimestamp();
     }
     if (!is_null($crtime)) {
         $additionalParams['crtime'] = $crtime->getTimestamp();
     }
     if (!is_null($atime)) {
         $additionalParams['atime'] = $atime->getTimestamp();
     }
     //        $additionalParams['file'] = $file;
     $cgiPath = 'FileStation/api_upload.cgi';
     $apiName = 'SYNO.FileStation.Upload';
     $version = 1;
     // Check Available APIS
     if ($this->getIsQueriesValidated() && !in_array($apiName, array('SYNO.API.Auth', 'SYNO.API.Info')) && !isset($this->availableApis[$apiName])) {
         $this->retrieveAvailableApis($apiName);
         // Validate API information
         $calledApiInformation = $this->availableApis[$apiName];
         if (is_null($calledApiInformation)) {
             throw new \RuntimeException('Unavailable API');
         }
         if ($cgiPath != $calledApiInformation['ApiPath']) {
             throw new \RuntimeException('CGI Path problem [Requested: ' . $cgiPath . '][ServerVersion: ' . $calledApiInformation['ApiPath'] . ']');
         }
         if ($calledApiInformation['MinVersion'] > $version) {
             throw new \RuntimeException('API Version issue [Requested: ' . $version . '][MinVersion: ' . $calledApiInformation['MinVersion'] . ']');
         }
         if ($calledApiInformation['MaxVersion'] < $version) {
             throw new \RuntimeException('API Version issue [Requested: ' . $version . '][MaxVersion: ' . $calledApiInformation['MaxVersion'] . ']');
         }
     }
     // Login if required
     if (!$this->isLoggedIn()) {
         $this->login();
     }
     // Set "mandatory" parameters list
     $params = array('api' => $apiName, 'version' => $version, 'method' => 'upload');
     if ($this->isLoggedIn()) {
         $params['_sid'] = $this->getSessionId();
     }
     // Add additionnal parameters
     if ($additionalParams !== null) {
         $params = $params + $additionalParams;
     }
     // Set request
     $queryString = 'webapi/' . $cgiPath;
     $request = new FormRequest('POST', $queryString, $this->getBaseUrl());
     $request->addFields($params);
     $uploadedFile = new FormUpload($filePath);
     $uploadedFile->setContentType('application/octet-stream');
     $request->setField('file', $uploadedFile);
     $response = new Response();
     // Handle request
     $this->httpClient->send($request, $response);
     // Check response
     $parsedResponse = json_decode($response->getContent(), true);
     // Throw exception in case of error
     if ($parsedResponse['success'] !== true) {
         throw new PatbzhSynologyException($this->getErrorMessage($parsedResponse['error']['code']) . ' (' . $parsedResponse['error']['code'] . ')', $parsedResponse['error']['code']);
     }
     // Return json_decoded response
     return $parsedResponse;
 }