diff --git a/core/includes/file.inc b/core/includes/file.inc index 5d4a2bc2530820ca5cbfe929c0408acfdcb4dc16..137dd0d7509d927c809cf922ebf3d896a52af22b 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -1025,29 +1025,6 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) { return file_unmanaged_delete($path); } -/** - * Determines total disk space used by a single user or the whole filesystem. - * - * @param $uid - * Optional. A user id, specifying NULL returns the total space used by all - * non-temporary files. - * @param $status - * Optional. The file status to consider. The default is to only - * consider files in status FILE_STATUS_PERMANENT. - * - * @return - * An integer containing the number of bytes used. - */ -function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) { - $query = db_select('file_managed', 'f'); - $query->condition('f.status', $status); - $query->addExpression('SUM(f.filesize)', 'filesize'); - if (isset($uid)) { - $query->condition('f.uid', $uid); - } - return $query->execute()->fetchField(); -} - /** * Saves a file upload to a new location. * diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 445e7ab69a80ded749d063ecbe362e25fd9d0ec1..b61e52f53a1f1668c1a00c98f0d7a77c4d86174a 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -397,8 +397,8 @@ function file_validate_size(File $file, $file_limit = 0, $user_limit = 0) { $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($file_limit))); } - // Save a query by only calling file_space_used() when a limit is provided. - if ($user_limit && (file_space_used($user->uid) + $file->filesize) > $user_limit) { + // Save a query by only calling spaceUsed() when a limit is provided. + if ($user_limit && (entity_get_controller('file')->spaceUsed($user->uid) + $file->filesize) > $user_limit) { $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array('%filesize' => format_size($file->filesize), '%quota' => format_size($user_limit))); } @@ -710,13 +710,7 @@ function file_file_download($uri, $field_type = 'file') { * Implements file_cron() */ function file_cron() { - // Remove temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. - // Use separate placeholders for the status to avoid a bug in some versions - // of PHP. See http://drupal.org/node/352956. - $result = db_query('SELECT fid FROM {file_managed} WHERE status <> :permanent AND timestamp < :timestamp', array( - ':permanent' => FILE_STATUS_PERMANENT, - ':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE - )); + $result = entity_get_controller('file')->retrieveTemporaryFiles(); foreach ($result as $row) { if ($file = file_load($row->fid)) { $references = file_usage()->listUsage($file); diff --git a/core/modules/file/lib/Drupal/file/FileStorageController.php b/core/modules/file/lib/Drupal/file/FileStorageController.php index 195ab95a194cdc3813cc032a0759bc09034c30f5..fb72a5d5be686d24ef3d3b86b11b2576960ccfe9 100644 --- a/core/modules/file/lib/Drupal/file/FileStorageController.php +++ b/core/modules/file/lib/Drupal/file/FileStorageController.php @@ -62,4 +62,41 @@ public function preDelete($entities) { ->execute(); } + /** + * Determines total disk space used by a single user or the whole filesystem. + * + * @param int $uid + * Optional. A user id, specifying NULL returns the total space used by all + * non-temporary files. + * @param $status + * Optional. The file status to consider. The default is to only + * consider files in status FILE_STATUS_PERMANENT. + * + * @return int + * An integer containing the number of bytes used. + */ + public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT) { + $query = db_select($this->entityInfo['base_table'], 'f') + ->condition('f.status', $status); + $query->addExpression('SUM(f.filesize)', 'filesize'); + if (isset($uid)) { + $query->condition('f.uid', $uid); + } + return $query->execute()->fetchField(); + } + + /** + * Retrieve temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. + * + * @return + * A list of files to be deleted. + */ + public function retrieveTemporaryFiles() { + // Use separate placeholders for the status to avoid a bug in some versions + // of PHP. See http://drupal.org/node/352956. + return db_query('SELECT fid FROM {' . $this->entityInfo['base_table'] . '} WHERE status <> :permanent AND timestamp < :timestamp', array( + ':permanent' => FILE_STATUS_PERMANENT, + ':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE + )); + } } diff --git a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php index db62d5a31b01aa1c655a7f34509296b247769b07..67ec63b265b3190b9eeb64af4afe03d9b86a081f 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php @@ -8,13 +8,13 @@ namespace Drupal\file\Tests; /** - * This will run tests against the file_space_used() function. + * This will run tests against the $file_managed->spaceUsed() function. */ class SpaceUsedTest extends FileManagedTestBase { public static function getInfo() { return array( 'name' => 'File space used tests', - 'description' => 'Tests the file_space_used() function.', + 'description' => 'Tests the spaceUsed() function.', 'group' => 'File API', ); } @@ -43,21 +43,22 @@ function setUp() { * Test different users with the default status. */ function testFileSpaceUsed() { + $file = entity_get_controller('file'); // Test different users with default status. - $this->assertEqual(file_space_used(2), 70); - $this->assertEqual(file_space_used(3), 300); - $this->assertEqual(file_space_used(), 370); + $this->assertEqual($file->spaceUsed(2), 70); + $this->assertEqual($file->spaceUsed(3), 300); + $this->assertEqual($file->spaceUsed(), 370); // Test the status fields - $this->assertEqual(file_space_used(NULL, 0), 4); - $this->assertEqual(file_space_used(NULL, FILE_STATUS_PERMANENT), 370); + $this->assertEqual($file->spaceUsed(NULL, 0), 4); + $this->assertEqual($file->spaceUsed(NULL, FILE_STATUS_PERMANENT), 370); // Test both the user and status. - $this->assertEqual(file_space_used(1, 0), 0); - $this->assertEqual(file_space_used(1, FILE_STATUS_PERMANENT), 0); - $this->assertEqual(file_space_used(2, 0), 1); - $this->assertEqual(file_space_used(2, FILE_STATUS_PERMANENT), 70); - $this->assertEqual(file_space_used(3, 0), 3); - $this->assertEqual(file_space_used(3, FILE_STATUS_PERMANENT), 300); + $this->assertEqual($file->spaceUsed(1, 0), 0); + $this->assertEqual($file->spaceUsed(1, FILE_STATUS_PERMANENT), 0); + $this->assertEqual($file->spaceUsed(2, 0), 1); + $this->assertEqual($file->spaceUsed(2, FILE_STATUS_PERMANENT), 70); + $this->assertEqual($file->spaceUsed(3, 0), 3); + $this->assertEqual($file->spaceUsed(3, FILE_STATUS_PERMANENT), 300); } }