From 7aff468346f8c36476e05012e979a40282185330 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 7 Jan 2013 17:24:19 +0800 Subject: [PATCH] MDL-36708 files: Checking the size limit of a draft area does include references --- lib/filelib.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index eaa3590e702..b539e975a2e 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -468,6 +468,7 @@ function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $fil * 'filecount' => number of files in the draft area. * 'filesize' => total size of the files in the draft area. * 'foldercount' => number of folders in the draft area. + * 'filesize_without_references' => total size of the area excluding file references. * (more information will be added as needed). */ function file_get_draft_area_info($draftitemid, $filepath = '/') { @@ -479,7 +480,8 @@ function file_get_draft_area_info($draftitemid, $filepath = '/') { $results = array( 'filecount' => 0, 'foldercount' => 0, - 'filesize' => 0 + 'filesize' => 0, + 'filesize_without_references' ); if ($filepath != '/') { @@ -493,7 +495,12 @@ function file_get_draft_area_info($draftitemid, $filepath = '/') { } else { $results['filecount'] += 1; } - $results['filesize'] += $file->get_filesize(); + + $filesize = $file->get_filesize(); + $results['filesize'] += $filesize; + if (!$file->is_external_file()) { + $results['filesize_without_references'] += $filesize; + } } return $results; @@ -507,13 +514,18 @@ function file_get_draft_area_info($draftitemid, $filepath = '/') { * @param int $draftitemid the draft area item id. * @param int $areamaxbytes the maximum size allowed in this draft area. * @param int $newfilesize the size that would be added to the current area. + * @param bool $includereferences true to include the size of the references in the area size. * @return bool true if the area will/has exceeded its limit. * @since 2.4 */ -function file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $newfilesize = 0) { +function file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $newfilesize = 0, $includereferences = false) { if ($areamaxbytes != FILE_AREA_MAX_BYTES_UNLIMITED) { $draftinfo = file_get_draft_area_info($draftitemid); - if ($draftinfo['filesize'] + $newfilesize > $areamaxbytes) { + $areasize = $draftinfo['filesize_without_references']; + if ($includereferences) { + $areasize = $draftinfo['filesize']; + } + if ($areasize + $newfilesize > $areamaxbytes) { return true; } }