public function createFileNode(Element $parentElement, $desiredFilename, $workingFilePath, StorageFacilityFile $preconfiguredFile = null) { if (empty($desiredFilename)) { $desiredFilename = $this->filenameFromBasename(basename($workingFilePath)); } $path = pathinfo($workingFilePath); $ext = !empty($path['extension']) ? strtolower($path['extension']) : ''; if (in_array(strtolower($ext), array_map("strtolower", $this->mediaRestrictedExtensions))) { throw new Exception('Cannot add file with restricted extension [' . $ext . ']'); } list($storageFacility, $sfParams) = $this->deriveStorageFacility($parentElement); $element = $this->NodeRefService->oneFromAspect('@files')->getElement(); $file = $preconfiguredFile !== null ? $preconfiguredFile : new StorageFacilityFile(); $file->setId($storageFacility->findUniqueFileID($sfParams, $desiredFilename)); $file->setLocalPath($workingFilePath); $file = $storageFacility->putFile($sfParams, $file); list($width, $height) = ImageUtils::getImageDimensions($file->getLocalPath()); $mimetype = FileSystemUtils::getMimetype($file->getExtension()); //CREATE FILE NODE & SAVE $nodeRef = new NodeRef($element, SlugUtils::createSlug($file->getId())); $nodeRef = $this->NodeService->generateUniqueNodeRef($nodeRef, null, true); $node = $nodeRef->generateNode(); $this->NodeMapper->defaultsOnNode($node); $node->Title = $file->getId(); $node->setMeta('#url', $file->getURL()); // $node->setMeta('#path',$file->getLocalPath()); $node->setMeta('#mimetype', $mimetype); $node->setMeta('#size', filesize($file->getLocalPath())); $node->setMeta('#width', $width); $node->setMeta('#height', $height); $node->setMeta('#parent-element', $parentElement->Slug); $this->NodeService->add($node); return $node; }
/** * @dataProvider getBadExtensions * * @param string $extension */ public function testGetMimetypeBadExtensions($extension) { $this->assertEquals('application/octet-stream', \FileSystemUtils::getMimetype($extension)); }
/** * Add a new file node and adds the associated file. * * @param Element|string $parentElement * @param string $newFile Full path to the new file * @param string $newFileName the new disired filename * @return Node */ public function addFileNode($parentElement, $newFile, $newFileName) { // get the file element $element = $this->ElementService->oneFromAspect('@files'); // get storage facility list($storageFacility, $sfParams) = $this->deriveStorageFacility($parentElement); // add the new file $newId = $storageFacility->findUniqueFileID($sfParams, $this->buildFilename($newFileName)); $file = new StorageFacilityFile($newId, $newFile); $file = $storageFacility->putFile($sfParams, $file); //create the node $nodeRef = new NodeRef($element); $slug = SlugUtils::createSlug($newId); $nodeRef = $this->NodeService->generateUniqueNodeRef($nodeRef, $slug); $fileNode = $nodeRef->generateNode(); // set the new id as title $fileNode->Title = $newId; // set permenant fields $fileNode->ActiveDate = $this->DateFactory->newStorageDate(); $fileNode->Status = 'draft'; // get metrics list($width, $height) = ImageUtils::getImageDimensions($newFile); // and mime-type $mimetype = FileSystemUtils::getMimetype($file->getExtension()); // add the meta fields $fileNode->setMeta('#url', $file->getURL()); $fileNode->setMeta('#mimetype', $mimetype); $fileNode->setMeta('#size', filesize($newFile)); $fileNode->setMeta('#width', $width); $fileNode->setMeta('#height', $height); $fileNode->setMeta('#parent-element', $parentElement instanceof Element ? $parentElement->Slug : $parentElement); // do the add $this->RegulatedNodeService->add($fileNode); //add the node return $fileNode; }