Commit d1400e30 authored by Dries's avatar Dries

Issue #1831808 by marcingy: move remaining file managed queries into field...

Issue #1831808 by marcingy: move remaining file managed queries into field database entity storage controller.
parent 99c64fea
......@@ -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.
*
......
......@@ -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);
......
......@@ -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
));
}
}
......@@ -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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment