static function removeByID($id, $version) { if ($version == null) { eZPersistentObject::removeObject(eZBinaryFile::definition(), array('contentobject_attribute_id' => $id)); } else { eZPersistentObject::removeObject(eZBinaryFile::definition(), array('contentobject_attribute_id' => $id, 'version' => $version)); } }
/** * Regression test for issue #14983 * * @link http://issues.ez.no/14983 */ public function testIssue14983() { $className = 'eZBinaryFileType test class'; $classIdentifier = 'ezbinaryfiletype_test_class'; $attributeName = 'File'; $attributeIdentifier = 'file'; $attributeType = 'ezbinaryfile'; $filePath = 'tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression_issue14983.txt'; $class = new ezpClass( $className, $classIdentifier, $className ); $classAttribute = $class->add( $attributeName, $attributeIdentifier, $attributeType ); $class->store(); $object = new ezpObject( $classIdentifier, 2 ); $object->name = __FUNCTION__; { $dataMap = $object->object->dataMap(); $fileAttribute = $dataMap[$attributeIdentifier]; { $dataType = new eZBinaryFileType(); $dataType->fromString( $fileAttribute, $filePath ); } $fileAttribute->store(); } $object->publish(); $object->refresh(); $contentObjectAttributeID = $fileAttribute->attribute( "id" ); $files = eZBinaryFile::fetch( $contentObjectAttributeID ); foreach ( $files as $file ) { // Read stored path, move to trash, and read stored path again $this->assertNotEquals( $file, null ); $storedFileInfo = $file->storedFileInfo(); $storedFilePath = $storedFileInfo['filepath']; $version = $file->attribute( 'version' ); $object->object->removeThis(); $object->refresh(); $file = eZBinaryFile::fetch( $contentObjectAttributeID, $version ); $storedFileInfo = $file->storedFileInfo(); $storedFilePathAfterTrash = $storedFileInfo['filepath']; $this->assertNotEquals( $storedFilePath, $storedFilePathAfterTrash, 'The stored file should be renamed when trashed' ); } }
/** * @param eZContentObjectAttribute $contentObjectAttribute the attribute to serialize * @param eZContentClassAttribute $contentClassAttribute the content class of the attribute to serialize * @return json encoded string for further processing * required first level elements 'method', 'version_format', 'data_type_identifier', 'content' * optional first level element is 'rendered' which should store (template) rendered xhtml snippets */ public static function getAttributeContent(eZContentObjectAttribute $contentObjectAttribute, eZContentClassAttribute $contentClassAttribute) { $dataTypeIdentifier = $contentObjectAttribute->attribute('data_type_string'); $attributeID = $contentObjectAttribute->attribute("id"); $version = $contentObjectAttribute->attribute("version"); if (!$contentObjectAttribute->hasContent()) { $content = null; } else { $binaryFile = eZBinaryFile::fetch($attributeID, $version); $content = $binaryFile->storedFileInfo(); } // This is not really the place, but for now initiate the safeguarding of the file itself here $archiveFileHandler = ezpFileArchiveFactory::getFileArchiveHandler('filesystem'); // todo: insert check if handler is really returned and of the right class before calling the archive action // maybe use the attribute id as prefix as well, may be useful for bookkeeping/recovery and potentially easier restore as well $archiveResult = $archiveFileHandler->archiveFile($content['filepath'], array($content['filepath']), $attributeID, 'ezbinaryfile'); $target = array('content' => $content, 'has_rendered_content' => false, 'rendered' => null, 'archived' => true, 'archive' => $archiveResult); return $target; }
function downloadFileObject($contentObject, $contentObjectAttribute) { $contentObjectAttributeID = $contentObjectAttribute->attribute('id'); $version = $contentObject->attribute('current_version'); $fileObject = eZBinaryFile::fetch($contentObjectAttributeID, $version); if ($fileObject) { return $fileObject; } $fileObject = eZMedia::fetch($contentObjectAttributeID, $version); return $fileObject; }
function downloadType($contentObject, $contentObjectAttribute) { $contentObjectAttributeID = $contentObjectAttribute->attribute('id'); $version = $contentObject->attribute('current_version'); $fileObject = eZBinaryFile::fetch($contentObjectAttributeID, $version); if ($fileObject) { return self::TYPE_FILE; } $fileObject = eZMedia::fetch($contentObjectAttributeID, $version); if ($fileObject) { return self::TYPE_MEDIA; } return false; }
* @copyright Copyright (C) eZ Systems AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. * @version 2014.07.0 * @package kernel */ require 'autoload.php'; $cli = eZCLI::instance(); $script = eZScript::instance(array('description' => "\nAdds the file extension suffix to the files stored by the binary file datatype\n" . "where it is currently missing.\n", 'use-session' => false, 'use-modules' => false, 'use-extensions' => true)); $script->startup(); $options = $script->getOptions('', '', array()); $script->initialize(); $limit = 20; $offset = 0; $db = eZDB::instance(); $script->setIterationData('.', '~'); while ($binaryFiles = eZPersistentObject::fetchObjectList(eZBinaryFile::definition(), null, null, null, array('offset' => $offset, 'limit' => $limit))) { foreach ($binaryFiles as $binaryFile) { $fileName = $binaryFile->attribute('filename'); if (strpos($fileName, '.') !== false) { $text = "skipping {$fileName}, it contains a suffix"; $script->iterate($cli, true, $text); continue; } $suffix = eZFile::suffix($binaryFile->attribute('original_filename')); if ($suffix) { $newFileName = $fileName . '.' . $suffix; $db->begin(); $oldFilePath = $binaryFile->attribute('filepath'); $binaryFile->setAttribute('filename', $newFileName); $binaryFile->store(); $newFilePath = $binaryFile->attribute('filepath');
function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode ) { $fileNode = $attributeNode->getElementsByTagName( 'binary-file' )->item( 0 ); if ( !is_object( $fileNode ) or !$fileNode->hasAttributes() ) { return; } $binaryFile = eZBinaryFile::create( $objectAttribute->attribute( 'id' ), $objectAttribute->attribute( 'version' ) ); $sourcePath = $package->simpleFilePath( $fileNode->getAttribute( 'filekey' ) ); if ( !file_exists( $sourcePath ) ) { eZDebug::writeError( "The file '$sourcePath' does not exist, cannot initialize file attribute with it", __METHOD__ ); return false; } $ini = eZINI::instance(); $mimeType = $fileNode->getAttribute( 'mime-type' ); list( $mimeTypeCategory, $mimeTypeName ) = explode( '/', $mimeType ); $destinationPath = eZSys::storageDirectory() . '/original/' . $mimeTypeCategory . '/'; if ( !file_exists( $destinationPath ) ) { $oldumask = umask( 0 ); if ( !eZDir::mkdir( $destinationPath, false, true ) ) { umask( $oldumask ); return false; } umask( $oldumask ); } $basename = basename( $fileNode->getAttribute( 'filename' ) ); while ( file_exists( $destinationPath . $basename ) ) { $basename = substr( md5( mt_rand() ), 0, 8 ) . '.' . eZFile::suffix( $fileNode->getAttribute( 'filename' ) ); } eZFileHandler::copy( $sourcePath, $destinationPath . $basename ); eZDebug::writeNotice( 'Copied: ' . $sourcePath . ' to: ' . $destinationPath . $basename, __METHOD__ ); $binaryFile->setAttribute( 'contentobject_attribute_id', $objectAttribute->attribute( 'id' ) ); $binaryFile->setAttribute( 'filename', $basename ); $binaryFile->setAttribute( 'original_filename', $fileNode->getAttribute( 'original-filename' ) ); $binaryFile->setAttribute( 'mime_type', $fileNode->getAttribute( 'mime-type' ) ); $binaryFile->store(); $fileHandler = eZClusterFileHandler::instance(); $fileHandler->fileStore( $destinationPath . $basename, 'binaryfile', true ); }