protected function createFileInternal(FileData $fileData)
 {
     if (!$this->checkRequiredInputParams($fileData->toArray(), array('name', 'src'))) {
         return null;
     }
     $accessToken = $this->getAccessToken();
     $fileName = $fileData->getName();
     $fileName = $this->convertToUtf8($fileName);
     $http = new HttpClient(array('redirect' => false, 'socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $fileName = urlencode($fileName);
     $accessToken = urlencode($accessToken);
     if ($http->query('PUT', "https://apis.live.net/v5.0/me/skydrive/files/{$fileName}?access_token={$accessToken}", IO\File::getFileContents(IO\Path::convertPhysicalToLogical($fileData->getSrc()))) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_FILE_INTERNAL)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     $finalOutput = Json::decode($http->getResult());
     if ($finalOutput === null) {
         $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON)));
         return null;
     }
     $fileData->setId($finalOutput['id']);
     $props = $this->getFileData($fileData);
     if ($props === null) {
         return null;
     }
     $fileData->setLinkInService($props['link']);
     return $fileData;
 }
Beispiel #2
0
 /**
  * Repacks exported document from Google.Drive, which has wrong order files in archive to show preview
  * in Google.Viewer. In Google.Viewer document should have file '[Content_Types].xml' on first position in archive.
  * @param FileData $fileData
  * @return FileData
  * @throws IO\FileNotFoundException
  * @throws IO\InvalidPathException
  * @internal
  */
 public function repackDocument(FileData $fileData)
 {
     if (!extension_loaded('zip')) {
         return null;
     }
     if (!TypeFile::isDocument($fileData->getName())) {
         return null;
     }
     $file = new IO\File($fileData->getSrc());
     if (!$file->isExists() || $file->getSize() > 15 * 1024 * 1024) {
         return null;
     }
     unset($file);
     $ds = DIRECTORY_SEPARATOR;
     $targetDir = \CTempFile::getDirectoryName(2, 'disk_repack' . $ds . md5(uniqid('di', true)));
     checkDirPath($targetDir);
     $targetDir = IO\Path::normalize($targetDir) . $ds;
     $zipOrigin = new \ZipArchive();
     if (!$zipOrigin->open($fileData->getSrc())) {
         return null;
     }
     if ($zipOrigin->getNameIndex(0) === '[Content_Types].xml') {
         $zipOrigin->close();
         return null;
     }
     if (!$zipOrigin->extractTo($targetDir)) {
         $zipOrigin->close();
         return null;
     }
     $zipOrigin->close();
     unset($zipOrigin);
     if (is_dir($targetDir) !== true) {
         return null;
     }
     $newName = md5(uniqid('di', true));
     $newFilepath = $targetDir . '..' . $ds . $newName;
     $repackedZip = new \ZipArchive();
     if (!$repackedZip->open($newFilepath, \ZipArchive::CREATE)) {
         return null;
     }
     $source = realpath($targetDir);
     $repackedZip->addFile($source . $ds . '[Content_Types].xml', '[Content_Types].xml');
     $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
     foreach ($files as $file) {
         if ($file->getBasename() === '[Content_Types].xml') {
             continue;
         }
         $file = str_replace('\\', '/', $file);
         $file = realpath($file);
         if (is_dir($file) === true) {
             $repackedZip->addEmptyDir(str_replace('\\', '/', str_replace($source . $ds, '', $file . $ds)));
         } elseif (is_file($file) === true) {
             $repackedZip->addFile($file, str_replace('\\', '/', str_replace($source . $ds, '', $file)));
         }
     }
     $repackedZip->close();
     $newFileData = new FileData();
     $newFileData->setSrc($newFilepath);
     return $newFileData;
 }
Beispiel #3
0
 protected function getLocationForResumableUpload(FileData $fileData)
 {
     if (!$this->checkRequiredInputParams($fileData->toArray(), array('name', 'mimeType', 'size'))) {
         return null;
     }
     $accessToken = $this->getAccessToken();
     $fileName = $fileData->getName();
     $fileName = $this->convertToUtf8($fileName);
     $http = new HttpClient(array('redirect' => false, 'socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Content-Type', 'application/json; charset=UTF-8');
     $http->setHeader('Authorization', "Bearer {$accessToken}");
     $http->setHeader('X-Upload-Content-Type', $fileData->getMimeType());
     $http->setHeader('X-Upload-Content-Length', $fileData->getSize());
     $postFields = "{\"title\":\"{$fileName}\"}";
     if ($http->post('https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&convert=' . ($fileData->isNeedConvert() ? 'true' : 'false'), $postFields) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_LOCATION_FOR_UPLOAD)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     return $http->getHeaders()->get('Location');
 }