public function testExiftoolAvailable()
 {
     $exiftool = new \Stefanheinen\Exiftool\Exiftool();
     $testfilename = dirname(__FILE__) . '/testresources/one.jpg';
     $exif = $exiftool->readFromFile($testfilename);
     $this->assertEquals('2013:11:16 09:32:51Z', $exif[0]['GPSDateTime']);
 }
 /**
  * Implement the R in CRUD. Calls to ‘‘Model::find()‘‘ arrive here.
  *
  * This will recursively read all the files in the base dir
  * return them in an array
  */
 public function read(Model $model, $queryData = array(), $recursive = null)
 {
     if (!$this->isConnected()) {
         return FALSE;
     }
     /**
      * Initialize the conditions array so we can check for them later on
      */
     $conditions = $queryData['conditions'];
     /**
      * Get the baseDir
      */
     $baseDir = $this->_getBaseDir();
     /**
      * Check if we have a cache hit
      * if not, we have to do the actual reading of the directory
      * $queryData['conditions'] has to be included since for example
      * getExif could be true on first read, but not on second
      */
     $cachePath = 'filesystemDatasource_' . md5($baseDir . serialize($queryData));
     Cache::set(array('duration' => '+10 seconds'));
     $result = Cache::read($cachePath);
     if ($result === FALSE) {
         /**
          * Read all files in the directory
          */
         $dir = new Folder($baseDir);
         $dirItems = $dir->findRecursive('.*', true);
         //@TODO: will only find files! maybe use different method
         if (empty($dirItems)) {
             return array();
         }
         /**
          * Build the return array with all properties
          */
         if (isset($queryData['getExif']) && $queryData['getExif']) {
             $exif = new Stefanheinen\Exiftool\Exiftool();
         }
         foreach ($dirItems as $dirItem) {
             $fullPath = realpath($dirItem);
             if (!$this->_checkPathAllowed($fullPath)) {
                 continue;
             }
             $relPath = str_replace($baseDir, '', $fullPath);
             // read mime type
             $file = new File($fullPath);
             $fileInfo = $file->info();
             $mime = $fileInfo['mime'];
             // read exif data
             if (isset($queryData['getExif']) && $queryData['getExif']) {
                 $exif->readFromFile($fullPath);
                 $exifData = $exif->getDataArray();
                 if (!is_null($exifData)) {
                     $exifData = serialize($exifData);
                     if ($exifData === FALSE) {
                         $exifData = NULL;
                     }
                 }
             } else {
                 $exifData = NULL;
             }
             // read file content
             if (isset($queryData['getContent']) && $queryData['getContent']) {
                 $content = file_get_contents($fullPath);
                 //if($content === FALSE)
                 //	$content = NULL;
                 //@TODO: png file evaluates to false, what's happening here? also the view doesn't display the content of an image file.
             } else {
                 $content = NULL;
             }
             $result[] = array($model->alias => array('id' => $relPath, 'relative_path' => $relPath, 'modified' => filemtime($fullPath), 'size' => filesize($fullPath), 'is_dir' => is_dir($fullPath) ? true : false, 'is_dot_file' => substr(basename($relPath), 0, 1) == '.' ? true : false, 'exif' => $exifData, 'mime_type' => $mime, 'content' => $content));
         }
         Cache::set(array('duration' => '+10 seconds'));
         Cache::write($cachePath, $result);
     }
     /**
      * Remove all items which do not meet the conditions.
      */
     $filteredResult = array();
     foreach ($result as $resultKey => $dirItem) {
         foreach ($conditions as $conditionName => $conditionValue) {
             $flatDirItem = Hash::flatten($dirItem);
             if ($flatDirItem[$conditionName] != $conditionValue) {
                 continue 2;
             }
         }
         $filteredResult[] = $result[$resultKey];
     }
     $sortedFilteredResult = $this->_sortItems($model, $filteredResult, $queryData['order']);
     $pagedSortedFilteredResult = $this->_getPage($sortedFilteredResult, $queryData);
     /**
      * return count
      */
     $queryFields = Hash::extract($queryData, 'fields');
     if (!empty($queryFields[0]) && $queryFields[0] == '__count') {
         return array(array($model->alias => array('count' => count($pagedSortedFilteredResult))));
     }
     return $pagedSortedFilteredResult;
 }