Index: kernel/classes/ezdatatype.php
===================================================================
--- kernel/classes/ezdatatype.php	(revision 24397)
+++ kernel/classes/ezdatatype.php	(working copy)
@@ -856,6 +856,14 @@
     }
 
     /*!
+     Do any necessary changes to stored object attribute when moving an object to trash.
+     \note Default implementation does nothing.
+    */
+    function trashStoredObjectAttribute( $objectAttribute, $version = null )
+    {
+    }
+
+    /*!
      Clean up stored object attribute
      \note Default implementation does nothing.
     */
Index: kernel/classes/datatypes/ezmedia/ezmedia.php
===================================================================
--- kernel/classes/datatypes/ezmedia/ezmedia.php	(revision 24397)
+++ kernel/classes/datatypes/ezmedia/ezmedia.php	(working copy)
@@ -185,6 +185,27 @@
         }
     }
 
+    static function fetchByFileName( $filename, $version = null, $asObject = true )
+    {
+        if ( $version == null )
+        {
+            return eZPersistentObject::fetchObjectList( eZMedia::definition(),
+                                                        null,
+                                                        array( 'filename' => $filename ),
+                                                        null,
+                                                        null,
+                                                        $asObject );
+        }
+        else
+        {
+            return eZPersistentObject::fetchObject( eZMedia::definition(),
+                                                    null,
+                                                    array( 'filename' => $filename,
+                                                           'version' => $version ),
+                                                    $asObject );
+        }
+    }
+
     static function removeByID( $id, $version )
     {
         if( $version == null )
Index: kernel/classes/datatypes/ezmedia/ezmediatype.php
===================================================================
--- kernel/classes/datatypes/ezmedia/ezmediatype.php	(revision 24397)
+++ kernel/classes/datatypes/ezmedia/ezmediatype.php	(working copy)
@@ -83,6 +83,58 @@
     }
 
     /*!
+     The object is being moved to trash, do any necessary changes to the attribute.
+     Rename file and update db row with new name, so that access to the file using old links no longer works.
+    */
+    function trashStoredObjectAttribute( $contentObjectAttribute, $version = null )
+    {
+        $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
+        $sys = eZSys::instance();
+        $storage_dir = $sys->storageDirectory();
+
+        if ( $version == null )
+            $mediaFiles = eZMedia::fetch( $contentObjectAttributeID );
+        else
+            $mediaFiles = array( eZMedia::fetch( $contentObjectAttributeID, $version ) );
+
+        foreach ( $mediaFiles as $mediaFile )
+        {
+            if ( $mediaFile == null )
+                continue;
+            $mimeType =  $mediaFile->attribute( "mime_type" );
+            list( $prefix, $suffix ) = split ('[/]', $mimeType );
+            $orig_dir = $storage_dir . '/original/' . $prefix;
+            $fileName = $mediaFile->attribute( "filename" );
+
+            // Check if there are any other records in ezmedia that point to that fileName.
+            $mediaObjectsWithSameFileName = eZMedia::fetchByFileName( $fileName );
+
+            $filePath = $orig_dir . "/" . $fileName;
+            $file = eZClusterFileHandler::instance( $filePath );
+
+            if ( $file->exists() and count( $mediaObjectsWithSameFileName ) <= 1 )
+            {
+                // create dest filename in the same manner as eZHTTPFile::store()
+                // grab file's suffix
+                $fileSuffix = eZFile::suffix( $fileName );
+                // prepend dot
+                if ( $fileSuffix )
+                    $fileSuffix = '.' . $fileSuffix;
+                // grab filename without suffix
+                $fileBaseName = basename( $fileName, $fileSuffix );
+                // create dest filename
+                $newFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
+                $newFilePath = $orig_dir . "/" . $newFileName;
+
+                // rename the file, and update the database data
+                $file->move( $newFilePath );
+                $mediaFile->setAttribute( 'filename', $newFileName );
+                $mediaFile->store();
+            }
+        }
+    }
+
+    /*!
      Delete stored attribute
     */
     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
Index: kernel/classes/datatypes/ezimage/ezimagetype.php
===================================================================
--- kernel/classes/datatypes/ezimage/ezimagetype.php	(revision 24397)
+++ kernel/classes/datatypes/ezimage/ezimagetype.php	(working copy)
@@ -58,6 +58,56 @@
         }
     }
 
+    /*!
+     The object is being moved to trash, do any necessary changes to the attribute.
+     Rename file and update db row with new name, so that access to the file using old links no longer works.
+    */
+    function trashStoredObjectAttribute( $contentObjectAttribute, $version = null )
+    {
+        $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
+        $imageHandler = $contentObjectAttribute->attribute( 'content' );
+        $imageFiles = eZImageFile::fetchForContentObjectAttribute( $contentObjectAttributeID );
+
+        foreach ( $imageFiles as $imageFile )
+        {
+            if ( $imageFile == null )
+                continue;
+            $existingFilepath = $imageFile;
+
+            // Check if there are any other records in ezimagefile that point to that filename.
+            $imageObjectsWithSameFileName = eZImageFile::fetchByFilepath( $existingFilepath );
+
+            $file = eZClusterFileHandler::instance( $existingFilepath );
+
+            if ( $file->exists() and count( $imageObjectsWithSameFileName ) <= 1 )
+            {
+                $orig_dir = dirname( $existingFilepath ) . '/trashed';
+                $fileName = basename( $existingFilepath );
+
+                // create dest filename in the same manner as eZHTTPFile::store()
+                // grab file's suffix
+                $fileSuffix = eZFile::suffix( $fileName );
+                // prepend dot
+                if ( $fileSuffix )
+                    $fileSuffix = '.' . $fileSuffix;
+                // grab filename without suffix
+                $fileBaseName = basename( $fileName, $fileSuffix );
+                // create dest filename
+                $newFileBaseName = md5( $fileBaseName . microtime() . mt_rand() );
+                $newFileName = $newFileBaseName . $fileSuffix;
+                $newFilepath = $orig_dir . '/' . $newFileName;
+
+                // rename the file, and update the database data
+                $imageHandler->updateAliasPath( $orig_dir, $newFileBaseName );
+                if ( $imageHandler->isStorageRequired() )
+                {
+                    $imageHandler->store( $contentObjectAttribute );
+                    $contentObjectAttribute->store();
+                }
+            }
+        }
+    }
+
     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
     {
         if ( $version === null )
Index: kernel/classes/datatypes/ezbinaryfile/ezbinaryfiletype.php
===================================================================
--- kernel/classes/datatypes/ezbinaryfile/ezbinaryfiletype.php	(revision 24397)
+++ kernel/classes/datatypes/ezbinaryfile/ezbinaryfiletype.php	(working copy)
@@ -126,6 +126,58 @@
     }
 
     /*!
+     The object is being moved to trash, do any necessary changes to the attribute.
+     Rename file and update db row with new name, so that access to the file using old links no longer works.
+    */
+    function trashStoredObjectAttribute( $contentObjectAttribute, $version = null )
+    {
+        $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
+        $sys = eZSys::instance();
+        $storage_dir = $sys->storageDirectory();
+
+        if ( $version == null )
+            $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
+        else
+            $binaryFiles = array( eZBinaryFile::fetch( $contentObjectAttributeID, $version ) );
+
+        foreach ( $binaryFiles as $binaryFile )
+        {
+            if ( $binaryFile == null )
+                continue;
+            $mimeType =  $binaryFile->attribute( "mime_type" );
+            list( $prefix, $suffix ) = split ('[/]', $mimeType );
+            $orig_dir = $storage_dir . '/original/' . $prefix;
+            $fileName = $binaryFile->attribute( "filename" );
+
+            // Check if there are any other records in ezbinaryfile that point to that fileName.
+            $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
+
+            $filePath = $orig_dir . "/" . $fileName;
+            $file = eZClusterFileHandler::instance( $filePath );
+
+            if ( $file->exists() and count( $binaryObjectsWithSameFileName ) <= 1 )
+            {
+                // create dest filename in the same manner as eZHTTPFile::store()
+                // grab file's suffix
+                $fileSuffix = eZFile::suffix( $fileName );
+                // prepend dot
+                if ( $fileSuffix )
+                    $fileSuffix = '.' . $fileSuffix;
+                // grab filename without suffix
+                $fileBaseName = basename( $fileName, $fileSuffix );
+                // create dest filename
+                $newFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
+                $newFilePath = $orig_dir . "/" . $newFileName;
+
+                // rename the file, and update the database data
+                $file->move( $newFilePath );
+                $binaryFile->setAttribute( 'filename', $newFileName );
+                $binaryFile->store();
+            }
+        }
+    }
+
+    /*!
      Delete stored attribute
     */
     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
Index: kernel/classes/ezcontentobjecttrashnode.php
===================================================================
--- kernel/classes/ezcontentobjecttrashnode.php	(revision 24397)
+++ kernel/classes/ezcontentobjecttrashnode.php	(working copy)
@@ -165,6 +165,21 @@
     function storeToTrash()
     {
         $this->store();
+
+        $db = eZDB::instance();
+        $db->begin();
+
+        $contentObject = $this->attribute( 'object' );
+        $contentobjectAttributes = $contentObject->allContentObjectAttributes( $contentObject->attribute( 'id' ) );
+        foreach ( $contentobjectAttributes as $contentobjectAttribute )
+        {
+            $dataType = $contentobjectAttribute->dataType();
+            if ( !$dataType )
+                continue;
+            $dataType->trashStoredObjectAttribute( $contentobjectAttribute );
+        }
+
+        $db->commit();
     }
 
     static function purgeForObject( $contentObjectID )
Index: tests/tests/kernel/suite.php
===================================================================
--- tests/tests/kernel/suite.php	(revision 24397)
+++ tests/tests/kernel/suite.php	(working copy)
@@ -50,6 +50,9 @@
         $this->addTestSuite( 'eZContentClassTest' );
         $this->addTestSuite( 'eZPackageRegression' );
         $this->addTestSuite( 'eZContentFunctionsTest' );
+        $this->addTestSuite( 'eZBinaryFileTypeRegression' );
+        $this->addTestSuite( 'eZMediaTypeRegression' );
+        $this->addTestSuite( 'eZImageTypeRegression' );
 
         // This test suite is commented out until it will be fixed to work on any machine
         // $this->addTestSuite( 'eZRSSExportTest' );
Index: tests/tests/kernel/datatypes/ezimage/ezimagetype_regression_issue14983.png
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

Property changes on: tests/tests/kernel/datatypes/ezimage/ezimagetype_regression_issue14983.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Index: tests/tests/kernel/datatypes/ezimage/ezimagetype_regression.php
===================================================================
--- tests/tests/kernel/datatypes/ezimage/ezimagetype_regression.php	(revision 0)
+++ tests/tests/kernel/datatypes/ezimage/ezimagetype_regression.php	(revision 0)
@@ -0,0 +1,69 @@
+<?php
+/**
+ * File containing the eZImageTypeRegression class
+ *
+ * @copyright Copyright (C) 1999-2009 eZ Systems AS. All rights reserved.
+ * @license http://ez.no/licenses/gnu_gpl GNU GPLv2
+ * @package tests
+ */
+
+class eZImageTypeRegression extends ezpDatabaseTestCase
+{
+    protected $backupGlobals = false;
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setName( "eZImageType Regression Tests" );
+    }
+
+    /**
+     * Regression test for issue #14983
+     *
+     * @link http://issues.ez.no/14983
+     **/
+    public function testIssue14983()
+    {
+        $className = 'eZImageType test class';
+        $classIdentifier = 'ezimagetype_test_class';
+        $attributeName = 'Image';
+        $attributeIdentifier = 'image';
+        $attributeType = 'ezimage';
+        $filePath = 'tests/tests/kernel/datatypes/ezimage/ezimagetype_regression_issue14983.png';
+
+        $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 eZImageType();
+                $dataType->fromString( $fileAttribute, $filePath );
+            }
+            $fileAttribute->store();
+        }
+        $object->publish();
+        $object->refresh();
+
+        $contentObjectAttributeID = $fileAttribute->attribute( "id" );
+        $files = eZImageFile::fetchForContentObjectAttribute( $contentObjectAttributeID );
+        $file = $files[0];
+
+        // Read stored path, move to trash, and read stored path again
+        $this->assertNotEquals( $file, null );
+
+        $oldFile = $file;
+        $object->object->removeThis();
+        $object->refresh();
+        $files = eZImageFile::fetchForContentObjectAttribute( $contentObjectAttributeID );
+        $file = $files[0];
+
+        $this->assertNotEquals( $oldFile, $file, 'The stored file should be renamed when trashed' );
+    }
+}
+
+?>
Index: tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression_issue14983.txt
===================================================================
--- tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression_issue14983.txt	(revision 0)
+++ tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression_issue14983.txt	(revision 0)
@@ -0,0 +1 @@
+test file
Index: tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression.php
===================================================================
--- tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression.php	(revision 0)
+++ tests/tests/kernel/datatypes/ezbinaryfile/ezbinaryfiletype_regression.php	(revision 0)
@@ -0,0 +1,75 @@
+<?php
+/**
+ * File containing the eZBinaryFileTypeRegression class
+ *
+ * @copyright Copyright (C) 1999-2009 eZ Systems AS. All rights reserved.
+ * @license http://ez.no/licenses/gnu_gpl GNU GPLv2
+ * @package tests
+ */
+
+class eZBinaryFileTypeRegression extends ezpDatabaseTestCase
+{
+    protected $backupGlobals = false;
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setName( "eZBinaryFileType Regression Tests" );
+    }
+
+    /**
+     * 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' );
+        }
+    }
+}
+
+?>
Index: tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression.php
===================================================================
--- tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression.php	(revision 0)
+++ tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression.php	(revision 0)
@@ -0,0 +1,75 @@
+<?php
+/**
+ * File containing the eZMediaTypeRegression class
+ *
+ * @copyright Copyright (C) 1999-2009 eZ Systems AS. All rights reserved.
+ * @license http://ez.no/licenses/gnu_gpl GNU GPLv2
+ * @package tests
+ */
+
+class eZMediaTypeRegression extends ezpDatabaseTestCase
+{
+    protected $backupGlobals = false;
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setName( "eZMediaType Regression Tests" );
+    }
+
+    /**
+     * Regression test for issue #14983
+     *
+     * @link http://issues.ez.no/14983
+     **/
+    public function testIssue14983()
+    {
+        $className = 'eZMediaType test class';
+        $classIdentifier = 'ezmediatype_test_class';
+        $attributeName = 'Media';
+        $attributeIdentifier = 'media';
+        $attributeType = 'ezmedia';
+        $filePath = 'tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression_issue14983.flv';
+
+        $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 eZMediaType();
+                $dataType->fromString( $fileAttribute, $filePath );
+            }
+            $fileAttribute->store();
+        }
+        $object->publish();
+        $object->refresh();
+
+        $contentObjectAttributeID = $fileAttribute->attribute( "id" );
+        $files = eZMedia::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 = eZMedia::fetch( $contentObjectAttributeID, $version );
+
+            $storedFileInfo = $file->storedFileInfo();
+            $storedFilePathAfterTrash = $storedFileInfo['filepath'];
+
+            $this->assertNotEquals( $storedFilePath, $storedFilePathAfterTrash, 'The stored file should be renamed when trashed' );
+        }
+    }
+}
+
+?>
Index: tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression_issue14983.flv
===================================================================
--- tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression_issue14983.flv	(revision 0)
+++ tests/tests/kernel/datatypes/ezmedia/ezmediatype_regression_issue14983.flv	(revision 0)
@@ -0,0 +1 @@
+test file
