Skip to content

Latest commit

 

History

History
130 lines (104 loc) · 4.71 KB

php.md

File metadata and controls

130 lines (104 loc) · 4.71 KB

Upload file to OSS (PHP)

At this section we actually need 3 features:

  1. Create buckets
  2. List buckets & objects (files)
  3. Upload objects (files)

OSS.php

Create a /server/oss.php file with the following content:

<?php
namespace Autodesk\ForgeServices;
use Autodesk\Forge\Client\Api\BucketsApi;
use Autodesk\Forge\Client\Model\PostBucketsPayload;
use Autodesk\Forge\Client\Api\ObjectsApi;

class DataManagement{
    public function __construct(){
        set_time_limit(0);
    }    

    public function createOneBucket(){
         global $twoLeggedAuth;
         $accessToken = $twoLeggedAuth->getTokenInternal();
         
         // get the request body
         $body = json_decode(file_get_contents('php://input', 'r'), true);
         
         $bucketKey = $body['bucketKey'];
         // $policeKey = $body['policyKey'];
         $policeKey = "transient";
 
         $apiInstance = new BucketsApi($accessToken);
         $post_bucket = new PostBucketsPayload(); 
         $post_bucket->setBucketKey($bucketKey);
         $post_bucket->setPolicyKey($policeKey);
 
         try {
             $result = $apiInstance->createBucket($post_bucket);
             print_r($result);
         } catch (Exception $e) {
             echo 'Exception when calling BucketsApi->createBucket: ', $e->getMessage(), PHP_EOL;
         }   
      }
 
 
      /////////////////////////////////////////////////////////////////////////
      public function getBucketsAndObjects(){
         global $twoLeggedAuth;
         $accessToken = $twoLeggedAuth->getTokenInternal();
         
         $id = $_GET['id'];
         try{
             if ($id === '#') {// root
                 $apiInstance = new BucketsApi($accessToken);
                 $result = $apiInstance->getBuckets();
                 $resultArray = json_decode($result, true);
                 $buckets = $resultArray['items'];
                 $bucketsLength = count($buckets);
                 $bucketlist = array();
                 for($i=0; $i< $bucketsLength; $i++){
                     $bucketInfo = array('id'=>$buckets[$i]['bucketKey'],
                                         'text'=>$buckets[$i]['bucketKey'],
                                         'type'=>'bucket',
                                         'children'=>true
                     );
                     array_push($bucketlist, $bucketInfo);
                 }
                 print_r(json_encode($bucketlist));
             }
             else{
                 $apiInstance = new ObjectsApi($accessToken);
                 $bucket_key = $id; 
                 $result = $apiInstance->getObjects($bucket_key);
                 $resultArray = json_decode($result, true);
                 $objects = $resultArray['items'];
 
                 $objectsLength = count($objects);
                 $objectlist = array();
                 for($i=0; $i< $objectsLength; $i++){
                     $objectInfo = array('id'=>base64_encode($objects[$i]['objectId']),
                                         'text'=>$objects[$i]['objectKey'],
                                         'type'=>'object',
                                         'children'=>false
                     );
                     array_push($objectlist, $objectInfo);
                 }
                 print_r(json_encode($objectlist));
             }
         }catch(Exception $e){
             echo 'Exception when calling ObjectsApi->getObjects: ', $e->getMessage(), PHP_EOL;
         }
 
      }
 

      public function uploadFile(){
          global $twoLeggedAuth;
          $accessToken = $twoLeggedAuth->getTokenInternal();
          
          $body = $_POST;
          $file = $_FILES;
  
          $apiInstance = new ObjectsApi($accessToken);
          $bucket_key  = $body['bucketKey']; 
          $fileToUpload    = $file['fileToUpload'];
          $filePath = $fileToUpload['tmp_name'];
          $content_length = filesize($filePath); 
  
          $fileRead = fread($filePath, $content_length);
          
          try {
              $result = $apiInstance->uploadObject($bucket_key, $fileRead, $content_length, $filePath );
              print_r($result);
          } catch (Exception $e) {
              echo 'Exception when calling ObjectsApi->uploadObject: ', $e->getMessage(), PHP_EOL;
          }
      }        
}

As we plan to support the jsTree, our GET oss/buckets needs to return handle the id querystring parameter and return buckets when id=# and objects for a given bucketKey passed as id=bucketKey. The upload endpoint still have problem to upload, will check that later.

Note how we reuse the /server/oauth.php file to call .getTokenInternal() on all functions.

Next: Translate the file