Link.FYI

Pastebin

Create New My Pastes

Code (PHP) pasted on 2019-08-05, 22:07 Raw Source

  1. <?php
  2. /**
  3.  * the Import Module just iterates
  4.  */
  5. ini_set('exif.encode_unicode', 'UTF-8');
  6.  
  7. use Aws\S3\S3Client;
  8. use League\Flysystem\AwsS3v3\AwsS3Adapter;
  9. use League\Flysystem\Cached\CachedAdapter;
  10. use League\Flysystem\Cached\Storage\Memory as MemoryStore;
  11. use League\Flysystem\Filesystem;
  12.  
  13. $client = new S3Client($config['minio'][0]);
  14.  
  15. $s3adapter = new AwsS3Adapter($client, 'photos', '', ['StorageClass' => 'REDUCED_REDUNDANCY']);
  16. $cacheStore = new MemoryStore();
  17. $adapter = new CachedAdapter($s3adapter, $cacheStore);
  18. $filesystem = new Filesystem($adapter);
  19.  
  20. $contents = $filesystem->listContents('/', true);
  21. foreach ($contents as $object) {
  22.     if (stristr($object['path'], 'dark')) {
  23.         if ($object['type'] == "file") {
  24.  
  25.             $photoBean = R::findOne('photo', 'basename = ? AND path = ?', [$object['basename'], $object['path']]);
  26.             if (is_null($photoBean)) {
  27.                 $photoBean = R::dispense('photo');
  28.                 $photoBean->basename = $object['basename'];
  29.                 $photoBean->path = $object['path'];
  30.             }
  31.  
  32.             $stream = $filesystem->readStream($object['path']);
  33.             $content = "";
  34.             $startFound = false;
  35.             while (!$startFound && !feof($stream)) {
  36.                 $content .= stream_get_contents($stream, 128);
  37.                 $offset = strpos($content, '<x:xmpmeta');
  38.                 if ($offset != false) {
  39.                     $startFound = true;
  40.                 }
  41.             }
  42.  
  43.             // jump to the found offset
  44.             $content = stream_get_contents($stream, 128, $offset);
  45.  
  46.             $endFound = false;
  47.             while (!$endFound && !feof($stream)) {
  48.                 $content .= stream_get_contents($stream, 128);
  49.                 $offset = strpos($content, '</x:xmpmeta>');
  50.                 if ($offset != false) {
  51.                     $endFound = true;
  52.                 }
  53.             }
  54.  
  55.             fclose($stream);
  56.  
  57.             $document = new DOMDocument();
  58.             $document->loadXml($content);
  59.             $xpath = new DOMXpath($document);
  60.  
  61.             /**
  62.              * most likely a bit hackish.. thats how I do get all namespaces...
  63.              */
  64.             $results = preg_match_all('_xmlns:([A-Za-z]+)="(.*)"_U', $content, $matches);
  65.             foreach ($matches[1] as $key => $alias) {
  66.                 // echo "registering $alias for ".$matches[2][$key]."\n";
  67.                 $xpath->registerNamespace($alias, $matches[2][$key]);
  68.             }
  69.             $elements = $xpath->query("//*[name()='dc:subject']//rdf:li");
  70.             $tags = [];
  71.             if (!is_null($elements)) {
  72.                 foreach ($elements as $element) {
  73.                     $value = trim($element->nodeValue);
  74.                     if (!empty($value)) {
  75.                         $tags[] = strtolower($value);
  76.                     }
  77.                 }
  78.             }
  79.             $beanTags = R::tag($photoBean);
  80.             if ((count($beanTags) > 0) && count($tags) > 0) {
  81.                 // the bean has already tags connected to it.
  82.                 // add new tags
  83.                 $newTags = array_diff($tags, $beanTags);
  84.                 if (count($newTags) > 0) {
  85.                     foreach ($newTags as $tag) {
  86.                         R::addTags($photoBean, [$tag]);
  87.                     }
  88.                 }
  89.                 // and remove old tags
  90.                 $deleteTags = array_diff($beanTags, $tags);
  91.                 if (count($deleteTags) > 0) {
  92.                     foreach ($deleteTags as $tag) {
  93.                         R::untag($photoBean, [$tag]);
  94.                     }
  95.                 }
  96.             } elseif (count($tags) > 0) {
  97.                 // new bean - just add all tags.
  98.                 R::addTags($photoBean, $tags);
  99.             } else {
  100.                 if (count($beanTags) > 0) {
  101.                     // old bean with tags attached though the pic does not have
  102.                     // any tags anymore. remove them
  103.                     foreach ($beanTags as $tag) {
  104.                         R::untag($photoBean, [$tag]);
  105.                     }
  106.                 }
  107.             }
  108.  
  109.             // finally, store the bean and it's tags.
  110.             R::store($photoBean);
  111.         }
  112.     }
  113. }