private function checkModifiedPictures($timeout)
 {
     //Get all it's images and create a hashmap for matching (img > img path)
     $container = $this->getContainer();
     $root = $container->getParameter('album_root');
     $paths = array();
     $cmd = sprintf('find %s -iname "*.jpg" -mmin "-%d"', escapeshellarg($root), $timeout);
     exec($cmd, $paths);
     $doctrine = $this->getContainer()->get('doctrine');
     $em = $doctrine->getManager();
     $folderRepo = $doctrine->getRepository('JccAlbumBundle:Folder');
     $pictureRepo = $doctrine->getRepository('JccAlbumBundle:Picture');
     $folders = array();
     foreach ($paths as $path) {
         //Check folder
         $folderPath = trim(str_replace('root', '', dirname($path)), '/');
         $folder = null;
         if (isset($folders[$folderPath])) {
             $folder = $folders[$folderPath];
         } else {
             $folder = $folderRepo->findOneByPath(dirname($folderPath));
         }
         if (!$folder) {
             $class = $folderRepo->getClassName();
             $folder = new $class();
             $folder->setName(basename(dirname($path)));
             $folder->setPath(trim(str_replace($root, '', dirname($path)), '/'));
             $em->persist($folder);
         }
         $folders[$folderPath] = $folder;
         //Check image existence
         $picture = $pictureRepo->findOneBy(array('folder' => $folder, 'path' => dirname($path)));
         if (!$picture) {
             $picture = new Picture();
             $picture->setPath(basename($path));
             $picture->setHash(substr(md5($path), 0, 12));
             $picture->setFolder($folder);
             $exif = @exif_read_data($path);
             if (!empty($exif['FileDateTime'])) {
                 $picture->setOriginalDate(new DateTime("@" . $exif['FileDateTime']));
             }
             $em->persist($picture);
         }
     }
     $em->flush();
 }
Beispiel #2
0
 /**
  * @param \Jcc\Bundle\AlbumBundle\Entity\Folder $folder
  *
  * @return array
  */
 public function crawlFolderPictures(Entity\Folder $folder)
 {
     //Get all it's images and create a hashmap for matching (img > img path)
     $root = $this->parameters['album_root'];
     $all = $pictures = $saved = array();
     foreach ($folder->getPictures() as $picture) {
         $saved[$picture->getHash()] = $picture;
     }
     foreach (new DirectoryIterator($root . '/' . $folder->getPath()) as $file) {
         $all[] = array('filename' => $file->getFilename(), 'pathname' => $file->getPathname());
     }
     usort($all, function ($a, $b) {
         return strcmp($a['filename'], $b['filename']);
     });
     foreach ($all as $file) {
         $extension = strtolower(pathinfo($file['filename'], PATHINFO_EXTENSION));
         if ('jpg' != $extension && 'jpeg' != $extension) {
             continue;
         }
         if (strpos($file['filename'], '.') === 0) {
             continue;
         }
         $hash = substr(md5($file['pathname']), 0, 12);
         if (isset($saved[$hash])) {
             $picture = $saved[$hash];
             unset($saved[$hash]);
         } else {
             $picture = new Entity\Picture();
             $picture->setPath($file['filename']);
             $picture->setHash($hash);
             $picture->setFolder($folder);
             $date = null;
             $exif = @exif_read_data($file['pathname']);
             if (!empty($exif['FileDateTime'])) {
                 $date = new DateTime("@" . $exif['FileDateTime']);
             } else {
                 $output = array();
                 exec(sprintf('exiftool %s', escapeshellarg($file['pathname'])), $output, $error);
                 foreach ($output ?: array() as $line) {
                     if (strpos($line, 'Date/Time Original') === false) {
                         continue;
                     }
                     list($key, $value) = preg_split('/\\s+:\\s+/', $line);
                     if ($key == 'Date/Time Original') {
                         $date = \DateTime::createFromFormat('Y:m:d h:i:s', $value);
                         break;
                     }
                 }
             }
             if ($date) {
                 $picture->setOriginalDate($date);
             }
             $this->em->persist($picture);
         }
         $pictures[] = $picture;
     }
     //Remove inexistant pictures
     foreach ($saved as $picture) {
         $this->em->remove($picture);
     }
     $this->em->flush();
     //Sort pictures by date shot (EXIF)
     usort($pictures, function ($a, $b) {
         if (!$a->getOriginalDate() || !$b->getOriginalDate()) {
             return strcmp($a->getPath(), $b->getPath());
         }
         return $a->getOriginalDate() > $b->getOriginalDate();
     });
     return $pictures;
 }