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());
 }
 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);
 }
Example #3
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;
 }