src/libraries/controllers/ApiPhotoController.php
/**
    * Upload a photo.
    * This stores the original photo plus a base version used for future manipulations.
    * If a returnoptions value is present then a version using that value is also generated.
    * This stores in the remote file system as well as the remote data store.
    * Parameters are contained in _POST.
    *
    * @return string standard json envelope
    */
Upload a photo.
This stores the original photo plus a base version used for future manipulations.
If a returnoptions value is present then a version using that value is also generated.
This stores in the remote file system as well as the remote data store.
Parameters are contained in _POST.

@return string standard json envelope
Upload a photo . This stores the original photo plus a base version used for future manipulations . If a returnoptions value is present then a version using that value is also generated . This stores in the remote file system as well as the remote data store . Parameters are contained in _POST . @return string standard json envelope
public function upload()
  {
    getAuthentication()->requireAuthentication();
    getAuthentication()->requireCrumb();
    $httpObj = new Http;
    $attributes = $_REQUEST;
    extract($this->parsePhotoFromRequest());
    $utility = new Utility;
    if(!$utility->isValidMimeType($localFile))
    {
      $this->logger->warn(sprintf('Invalid mime type for %s', $localFile));
      unlink($localFile);
      return $this->error('Invalid mime type', false);;
    }
    if(isset($attributes['__route__']))
      unset($attributes['__route__']);
    if(isset($attributes['photo']))
      unset($attributes['photo']);
    if(isset($attributes['crumb']))
      unset($attributes['crumb']);
    if(isset($attributes['returnSizes']))
    {
      $returnSizes = $attributes['returnSizes'];
      unset($attributes['returnSizes']);
    }
    $photoId = false;
    $attributes['hash'] = sha1_file($localFile);
    $allowDuplicate = $this->config->site->allowDuplicate;
    if(isset($attributes['allowDuplicate']))
      $allowDuplicate = $attributes['allowDuplicate'];
    if($allowDuplicate == '0')
    {
      $hashResp = $this->api->invoke("/{$this->apiVersion}/photos/list.json", EpiRoute::httpGet, array('_GET' => array('hash' => $attributes['hash'])));
      if($hashResp['result'][0]['totalRows'] > 0)
      {
        unlink($localFile);
        return $this->conflict('This photo already exists based on a sha1 hash. To allow duplicates pass in allowDuplicate=1', $hashResp['result'][0]);
      }
    }
    if(!isset($attributes['allowAutoRotate']) || $attributes['allowAutoRotate'] != '0')
    {
      $exiftran = $this->config->modules->exiftran;
      if(is_executable($exiftran))
        exec(sprintf('%s -ai %s', $exiftran, escapeshellarg($localFile)));
    }
    $photoId = $this->photo->upload($localFile, $name, $attributes);
    if($photoId)
    {
      if(isset($attributes['albums']))
        $this->updateAlbums($attributes['albums'], $photoId);
      if(isset($returnSizes))
      {
        $sizes = (array)explode(',', $returnSizes);
        if(!in_array('100x100xCR', $sizes))
          $sizes[] = '100x100xCR';
      }
      else
      {
        $sizes = array('100x100xCR');
      }
      foreach($sizes as $size)
      {
        $options = $this->photo->generateFragmentReverse($size);
        $hash = $this->photo->generateHash($photoId, $options['width'], $options['height'], $options['options']);
        $this->photo->generate($photoId, $hash, $options['width'], $options['height'], $options['options']);
      }
      $apiResp = $this->api->invoke("/{$this->apiVersion}/photo/{$photoId}/view.json", EpiRoute::httpGet, array('_GET' => array('returnSizes' => implode(',', $sizes))));
      $photo = $apiResp['result'];
      $webhookApi = $this->api->invoke("/{$this->apiVersion}/webhooks/photo.upload/list.json", EpiRoute::httpGet);
      if(!empty($webhookApi['result']) && is_array($webhookApi['result']))
      {
        $photoAsArgs = $photo;
        $photoAsArgs['tags'] = implode(',', $photoAsArgs['tags']);
        foreach($webhookApi['result'] as $key => $hook)
        {
          $httpObj->fireAndForget($hook['callback'], 'POST', $photoAsArgs);
          $this->logger->info(sprintf('Webhook callback executing for photo.upload: %s', $hook['callback']));
        }
      }
      $this->plugin->setData('photo', $photo);
      $this->plugin->invoke('onPhotoUploaded');
      $permission = isset($attributes['permission']) ? $attributes['permission'] : 0;
      $this->api->invoke(
        "/{$this->apiVersion}/activity/create.json", 
        EpiRoute::httpPost, 
        array('_POST' => array('type' => 'photo-upload', 'data' => $photo, 'permission' => $permission))
      );
      return $this->created("Photo {$photoId} uploaded successfully", $photo);
    }
    return $this->error('File upload failure', false);
  }
public function upload ( ) { getAuthentication ( ) -> requireAuthentication ( ) ; getAuthentication ( ) -> requireCrumb ( ) ; $ httpObj = new Http ; $ attributes = $ _REQUEST ; extract ( $ this -> parsePhotoFromRequest ( ) ) ; $ utility = new Utility ; if ( ! $ utility -> isValidMimeType ( $ localFile ) ) { $ this -> logger -> warn ( sprintf ( 'Invalid mime type for %s' , $ localFile ) ) ; unlink ( $ localFile ) ; return $ this -> error ( 'Invalid mime type' , false ) ; ; } if ( isset ( $ attributes [ '__route__' ] ) ) unset ( $ attributes [ '__route__' ] ) ; if ( isset ( $ attributes [ 'photo' ] ) ) unset ( $ attributes [ 'photo' ] ) ; if ( isset ( $ attributes [ 'crumb' ] ) ) unset ( $ attributes [ 'crumb' ] ) ; if ( isset ( $ attributes [ 'returnSizes' ] ) ) { $ returnSizes = $ attributes [ 'returnSizes' ] ; unset ( $ attributes [ 'returnSizes' ] ) ; } $ photoId = false ; $ attributes [ 'hash' ] = sha1_file ( $ localFile ) ; $ allowDuplicate = $ this -> config -> site -> allowDuplicate ; if ( isset ( $ attributes [ 'allowDuplicate' ] ) ) $ allowDuplicate = $ attributes [ 'allowDuplicate' ] ; if ( $ allowDuplicate == '0' ) { $ hashResp = $ this -> api -> invoke ( " / { $ this -> apiVersion } /photos/list.json " , EpiRoute :: httpGet , array ( '_GET' => array ( 'hash' => $ attributes [ 'hash' ] ) ) ) ; if ( $ hashResp [ 'result' ] [ 0 ] [ 'totalRows' ] > 0 ) { unlink ( $ localFile ) ; return $ this -> conflict ( 'This photo already exists based on a sha1 hash. To allow duplicates pass in allowDuplicate=1' , $ hashResp [ 'result' ] [ 0 ] ) ; } } if ( ! isset ( $ attributes [ 'allowAutoRotate' ] ) || $ attributes [ 'allowAutoRotate' ] != '0' ) { $ exiftran = $ this -> config -> modules -> exiftran ; if ( is_executable ( $ exiftran ) ) exec ( sprintf ( '%s -ai %s' , $ exiftran , escapeshellarg ( $ localFile ) ) ) ; } $ photoId = $ this -> photo -> upload ( $ localFile , $ name , $ attributes ) ; if ( $ photoId ) { if ( isset ( $ attributes [ 'albums' ] ) ) $ this -> updateAlbums ( $ attributes [ 'albums' ] , $ photoId ) ; if ( isset ( $ returnSizes ) ) { $ sizes = ( array ) explode ( ',' , $ returnSizes ) ; if ( ! in_array ( '100x100xCR' , $ sizes ) ) $ sizes [ ] = '100x100xCR' ; } else { $ sizes = array ( '100x100xCR' ) ; } foreach ( $ sizes as $ size ) { $ options = $ this -> photo -> generateFragmentReverse ( $ size ) ; $ hash = $ this -> photo -> generateHash ( $ photoId , $ options [ 'width' ] , $ options [ 'height' ] , $ options [ 'options' ] ) ; $ this -> photo -> generate ( $ photoId , $ hash , $ options [ 'width' ] , $ options [ 'height' ] , $ options [ 'options' ] ) ; } $ apiResp = $ this -> api -> invoke ( " / { $ this -> apiVersion } /photo/ { $ photoId } /view.json " , EpiRoute :: httpGet , array ( '_GET' => array ( 'returnSizes' => implode ( ',' , $ sizes ) ) ) ) ; $ photo = $ apiResp [ 'result' ] ; $ webhookApi = $ this -> api -> invoke ( " / { $ this -> apiVersion } /webhooks/photo.upload/list.json " , EpiRoute :: httpGet ) ; if ( ! empty ( $ webhookApi [ 'result' ] ) && is_array ( $ webhookApi [ 'result' ] ) ) { $ photoAsArgs = $ photo ; $ photoAsArgs [ 'tags' ] = implode ( ',' , $ photoAsArgs [ 'tags' ] ) ; foreach ( $ webhookApi [ 'result' ] as $ key => $ hook ) { $ httpObj -> fireAndForget ( $ hook [ 'callback' ] , 'POST' , $ photoAsArgs ) ; $ this -> logger -> info ( sprintf ( 'Webhook callback executing for photo.upload: %s' , $ hook [ 'callback' ] ) ) ; } } $ this -> plugin -> setData ( 'photo' , $ photo ) ; $ this -> plugin -> invoke ( 'onPhotoUploaded' ) ; $ permission = isset ( $ attributes [ 'permission' ] ) ? $ attributes [ 'permission' ] : 0 ; $ this -> api -> invoke ( " / { $ this -> apiVersion } /activity/create.json " , EpiRoute :: httpPost , array ( '_POST' => array ( 'type' => 'photo-upload' , 'data' => $ photo , 'permission' => $ permission ) ) ) ; return $ this -> created ( " Photo  { $ photoId }  uploaded successfully " , $ photo ) ; } return $ this -> error ( 'File upload failure' , false ) ; }
Upload a photo.
Upload a photo .
// this determines where to get the photo from and populates $localFile and $name // check if file type is valid // set default to config and override with parameter // auto rotation is enabled by default but requires exiftran // jmathai - check where this gets set