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"; } }
/** * Gets the local file from either File, Url, SourceRefNode or DataString params. * * @param array $params The user supplied parameters * @throws MediaServiceException if unable to fetch file * @return array [sourceFile], [sourceFileName] */ public function getSourceFile($params) { $sourceFile = null; $sourceFileName = null; // get the actual file - check in order - uploaded file, url then node if (!empty($params['File'])) { $sourceFileName = $params['File']; $uploadedFiles = $params['_uploadedFiles']; foreach ($uploadedFiles as $uploadFile) { if ($uploadFile->getName() == $sourceFileName) { $sourceFile = $uploadFile->getTemporaryName(); break; } } if (empty($sourceFile)) { throw new MediaServiceException("File '{$sourceFileName}' was not uploaded"); } } else { if (!empty($params['Url'])) { // go fetch file from url $url = $params['Url']; $data = $this->HttpRequest->fetchURL($url); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch'); file_put_contents($sourceFile, $data); $parts = parse_url($url); $sourceFileName = basename($parts['path']); } else { if (!empty($params['SourceNodeRef'])) { // go fetch file from another node's #original $node = $this->getNode($this->getNodeRef($params['SourceNodeRef']), '#original'); $tag = $node->getOutTag('#original'); if (!$tag) { throw new MediaServiceException("Node '{$params['SourceNodeRef']}' has no #original tag"); } $fileNode = $tag->getTagLinkNode(); $element = $node->getNodeRef()->getElement(); list($storageFacility, $sfParams) = $this->deriveStorageFacility($element); $file = $storageFacility->getFile($sfParams, $fileNode->Title, true); $contents = $file->getContents(); // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'sffetch'); file_put_contents($sourceFile, $contents); $sourceFileName = basename($fileNode->Title); } else { if (!empty($params['DataString'])) { $data = $params['DataString']; if (preg_match('/^data:/', $params['DataString'])) { $data = substr($data, strpos($data, ",") + 1); } if (!($file = base64_decode($data, true))) { throw new MediaServiceException('Data string is not a valid base64 encoded file.'); } // create a unique output file name $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'base64fetch'); file_put_contents($sourceFile, $file); $sourceFileName = $params['DataFileName']; } else { throw new MediaServiceException("No source file specified"); } } } } return array($sourceFile, $sourceFileName); }