protected function _importPhotosFromJson($file) { try { $contents = file_get_contents($file); $json = JSONUtils::decode($contents); foreach ($json as $v) { echo "importing {$v->title}..."; $url = $v->src; $parts = parse_url($url); $slug = SlugUtils::createSlug(basename($parts['path'])); preg_match('/(\\.\\w*)$/', $parts['path'], $ext); $nodeRef = $this->NodeRefService->oneFromAspect('@images'); $nodeRef = $this->NodeRefService->generateNodeRef($nodeRef, $slug); $node = $nodeRef->generateNode(); if (!$this->NodeService->refExists($node->getNodeRef())) { // go fetch file from url $data = $this->HttpRequest->fetchURL($url); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch', !empty($ext[1]) ? strtolower($ext[1]) : null); file_put_contents($sourceFile, $data); $node->Title = rtrim($v->title); $node->Status = "published"; $node->ActiveDate = $this->DateFactory->newStorageDate(); $node = $this->ImageService->storeMedia($sourceFile, $node, basename($parts['path'])); $this->NodeService->add($node); echo "done\n"; } else { echo "exists\n"; } unset($nodeRef); unset($node); } } catch (Exception $e) { echo "Exception: " . $e->getMessage() . "\n"; } }
/** * Extracts files from the archive and stores the media as temporary-zipped-media nodes. * * @throws Exception * @param $params * @return array Array of created nodes */ protected function _extractArchive($params) { $nodes = array(); $workdir = FileSystemUtils::createWorkingDirectory(); try { list($sourceFile, $sourceFileName) = $this->getSourceFile($params); $zip = zip_open($sourceFile); if (is_resource($zip)) { while ($zip_entry = zip_read($zip)) { $entryFileName = basename(zip_entry_name($zip_entry)); $entryFile = rtrim($workdir, '/') . '/' . $entryFileName; if (preg_match('/^\\./', $entryFileName)) { continue; } $path = pathinfo($entryFile); if (empty($path['extension'])) { continue; } try { // Create the file if (zip_entry_open($zip, $zip_entry, 'r')) { if ($fd = @fopen($entryFile, 'w+')) { fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry))); fclose($fd); $nodeRef = new NodeRef($this->NodeRefService->oneFromAspect('@temporary-zipped-media')->getElement(), SlugUtils::createSlug($entryFileName)); $nodeRef = $this->NodeService->generateUniqueNodeRef($nodeRef, null, true); // create node $node = $nodeRef->generateNode(); $this->NodeMapper->defaultsOnNode($node); if ($node != null) { $node->Title = $entryFileName; $node->Slug = SlugUtils::createSlug($entryFileName); $this->getErrors()->throwOnError(); $this->RegulatedNodeService->quickAdd($node); $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials()); // add the original media $file_params = $params; $file_params['File'] = $entryFileName; $file_params['NodeSlug'] = $node->Slug; $file_params['ElementSlug'] = 'temporary-zipped-media'; $file_params['_uploadedFiles']['file'] = new UploadedFile($entryFileName, $entryFile, zip_entry_filesize($zip_entry), FileSystemUtils::getMimetype($path['extension']), 0); $this->addOriginal($file_params); $this->_buildCmsThumbnail($node); $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials('all', '#original.fields')); $nodes[] = $node; @unlink($entryFile); } } zip_entry_close($zip_entry); } } catch (Exception $e) { @unlink($entryFile); throw $e; } } zip_close($zip); } else { throw new Exception($this->_zipFileErrMsg($zip)); } } catch (Exception $e) { // clean up //@unlink($sourceFile); throw $e; } return $nodes; }