summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAngie Byron2010-02-17 05:39:51 (GMT)
committerAngie Byron2010-02-17 05:39:51 (GMT)
commit00319d8e9a8b35148a6856d3380d572c571b94dc (patch)
treec6c299f7318cf8e360d037e20ca0980ef15870c0
parent94a0bb3afda084011f13a9c6d626d2706a8844c3 (diff)
#618654 by Steven Merrill, justinrandell, jim0203, quicksketch, and David_Rothstein: Fixed File and image fields are treated as temporary files and automatically deleted after six hours.
-rw-r--r--modules/file/file.field.inc16
-rw-r--r--modules/file/tests/file.test14
-rw-r--r--modules/image/image.field.inc7
3 files changed, 37 insertions, 0 deletions
diff --git a/modules/file/file.field.inc b/modules/file/file.field.inc
index eb43c4f..4fce284 100644
--- a/modules/file/file.field.inc
+++ b/modules/file/file.field.inc
@@ -242,6 +242,22 @@ function file_field_prepare_view($entity_type, $entities, $field, $instances, $l
}
/**
+ * Implements hook_field_presave().
+ */
+function file_field_presave($obj_type, $object, $field, $instance, $langcode, &$items) {
+ // Make sure that each file which will be saved with this object has a
+ // permanent status, so that it will not be removed when temporary files are
+ // cleaned up.
+ foreach ($items as $item) {
+ $file = file_load($item['fid']);
+ if (($file->status & FILE_STATUS_PERMANENT) == 0) {
+ $file->status |= FILE_STATUS_PERMANENT;
+ file_save($file);
+ }
+ }
+}
+
+/**
* Implements hook_field_update().
*
* Check for files that have been removed from the object.
diff --git a/modules/file/tests/file.test b/modules/file/tests/file.test
index 9fa70a9..22e4c71 100644
--- a/modules/file/tests/file.test
+++ b/modules/file/tests/file.test
@@ -172,6 +172,14 @@ class FileFieldTestCase extends DrupalWebTestCase {
$message = isset($message) ? $message : t('File %file exists in database at the correct path.', array('%file' => $file->uri));
$this->assertFalse(file_load($file->fid), $message);
}
+
+ /**
+ * Assert that a file's status is set to permanent in the database.
+ */
+ function assertFileIsPermanent($file, $message = NULL) {
+ $message = isset($message) ? $message : t('File %file is permanent.', array('%file' => $file->uri));
+ $this->assertTrue($file->status == FILE_STATUS_PERMANENT, $message);
+ }
}
/**
@@ -215,6 +223,7 @@ class FileFieldRevisionTestCase extends FileFieldTestCase {
$node_vid_r1 = $node->vid;
$this->assertFileExists($node_file_r1, t('New file saved to disk on node creation.'));
$this->assertFileEntryExists($node_file_r1, t('File entry exists in database on node creation.'));
+ $this->assertFileIsPermanent($node_file_r1, t('File is permanent.'));
// Upload another file to the same node in a new revision.
$this->replaceNodeFile($test_file, $field_name, $nid);
@@ -223,12 +232,14 @@ class FileFieldRevisionTestCase extends FileFieldTestCase {
$node_vid_r2 = $node->vid;
$this->assertFileExists($node_file_r2, t('Replacement file exists on disk after creating new revision.'));
$this->assertFileEntryExists($node_file_r2, t('Replacement file entry exists in database after creating new revision.'));
+ $this->assertFileIsPermanent($node_file_r2, t('Replacement file is permanent.'));
// Check that the original file is still in place on the first revision.
$node = node_load($nid, $node_vid_r1, TRUE);
$this->assertEqual($node_file_r1, (object) $node->{$field_name}[LANGUAGE_NONE][0], t('Original file still in place after replacing file in new revision.'));
$this->assertFileExists($node_file_r1, t('Original file still in place after replacing file in new revision.'));
$this->assertFileEntryExists($node_file_r1, t('Original file entry still in place after replacing file in new revision'));
+ $this->assertFileIsPermanent($node_file_r1, t('Original file is still permanent.'));
// Save a new version of the node without any changes.
// Check that the file is still the same as the previous revision.
@@ -237,6 +248,7 @@ class FileFieldRevisionTestCase extends FileFieldTestCase {
$node_file_r3 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
$node_vid_r3 = $node->vid;
$this->assertEqual($node_file_r2, $node_file_r3, t('Previous revision file still in place after creating a new revision without a new file.'));
+ $this->assertFileIsPermanent($node_file_r3, t('New revision file is permanent.'));
// Revert to the first revision and check that the original file is active.
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
@@ -244,12 +256,14 @@ class FileFieldRevisionTestCase extends FileFieldTestCase {
$node_file_r4 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
$node_vid_r4 = $node->vid;
$this->assertEqual($node_file_r1, $node_file_r4, t('Original revision file still in place after reverting to the original revision.'));
+ $this->assertFileIsPermanent($node_file_r4, t('Original revision file still permanent after reverting to the original revision.'));
// Delete the second revision and check that the file is kept (since it is
// still being used by the third revision).
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete'));
$this->assertFileExists($node_file_r3, t('Second file is still available after deleting second revision, since it is being used by the third revision.'));
$this->assertFileEntryExists($node_file_r3, t('Second file entry is still available after deleting second revision, since it is being used by the third revision.'));
+ $this->assertFileIsPermanent($node_file_r3, t('Second file entry is still permanent after deleting second revision, since it is being used by the third revision.'));
// Delete the third revision and check that the file is deleted also.
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete'));
diff --git a/modules/image/image.field.inc b/modules/image/image.field.inc
index 77ce16e..e8b4612 100644
--- a/modules/image/image.field.inc
+++ b/modules/image/image.field.inc
@@ -226,6 +226,13 @@ function image_field_prepare_view($entity_type, $entities, $field, $instances, $
}
/**
+ * Implements hook_field_presave().
+ */
+function image_field_presave($obj_type, $object, $field, $instance, $langcode, &$items) {
+ file_field_presave($obj_type, $object, $field, $instance, $langcode, $items);
+}
+
+/**
* Implements hook_field_insert().
*/
function image_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {