示例#1
0
 private function putFileInDirectory($directory, $localFile, $destinationName)
 {
     $createDirectory = false;
     // TODO confirm is this is the best way to do this
     //  it maybe a noop but calling get_file_info might be faster
     try {
         $newFolder = new Box_Client_Folder();
         $newFolder->attr('name', $directory);
         $newFolder->attr('parent_id', $this->boxFolderId);
         $newFolder->attr('share', 0);
         $status = $this->box->create($newFolder);
         if ($status !== self::statusCreatedOk && $status !== self::statusFolderExists) {
             getLogger()->warn(sprintf('Box API returned an unexpected response of (%s) from folder create call', $status));
             return false;
         }
     } catch (Box_Rest_Client_Exception $e) {
         getLogger()->warn('Box exception from folder create call', $e);
         return false;
     }
     try {
         // The way Box_Rest_Client works it uses the file's name as the display name
         $file = new Box_Client_File($localFile, $destinationName);
         $file->attr('folder_id', $newFolder->attr('folder_id'));
         $result = $this->box->upload($file, array('new_copy' => '1'), true);
         if ($result === self::statusUploadOk) {
             $this->metaDataMap[$localFile] = array('boxFileId' => $file->attr('id'));
             getLogger()->info(sprintf('Successfully stored file (%s) on Box.', $destinationName));
             return true;
         } else {
             getLogger()->crit('Could not put file on Box.', $e);
             return false;
         }
     } catch (Box_Rest_Client_Exception $e) {
         getLogger()->warn('Box exception from upload call', $e);
         return false;
     }
 }
示例#2
0
 /**
  * 
  * Imports the tree structure and allows us to provide some extended functionality 
  * at some point. Don't run import manually. It expects certain things that are 
  * delivered through the API. Instead, if you need a tree structure of something, 
  * simply call Box_Rest_Client->folder(folder_id); and it will automatically return 
  * the right stuff.
  * 
  * Due to an inconsistency with the Box.net ReST API, this section invovles a few 
  * more checks than normal to ensure that all the necessary values are available 
  * when doing the import.
  * @param array $tree
  */
 public function import(array $tree)
 {
     foreach ($tree['@attributes'] as $key => $val) {
         $this->attr[$key] = $val;
     }
     if (array_key_exists('folders', $tree)) {
         if (array_key_exists('folder', $tree['folders'])) {
             if (array_key_exists('@attributes', $tree['folders']['folder'])) {
                 // this is the case when there is a single folder within the root
                 $box_folder = new Box_Client_Folder();
                 $box_folder->import($tree['folders']['folder']);
                 $this->folder[] = $box_folder;
             } else {
                 // this is the case when there are multiple folders within the root
                 foreach ($tree['folders']['folder'] as $i => $folder) {
                     $box_folder = new Box_Client_Folder();
                     $box_folder->import($folder);
                     $this->folder[] = $box_folder;
                 }
             }
         }
     }
     if (array_key_exists('files', $tree)) {
         if (array_key_exists('file', $tree['files'])) {
             if (array_key_exists('@attributes', $tree['files']['file'])) {
                 // this is the case when there is a single file within a directory
                 $box_file = new Box_Client_File();
                 $box_file->import($tree['files']['file']);
                 $this->file[] = $box_file;
             } else {
                 // this is the case when there are multiple files in a directory
                 foreach ($tree['files']['file'] as $i => $file) {
                     $box_file = new Box_Client_File();
                     $box_file->import($file);
                     $this->file[] = $box_file;
                 }
             }
         }
     }
 }