MDL-33445 Added file size check to files inserted by reference
authorMarina Glancy <marina@moodle.com>
Thu, 31 May 2012 04:59:43 +0000 (12:59 +0800)
committerMarina Glancy <marina@moodle.com>
Tue, 5 Jun 2012 01:26:26 +0000 (09:26 +0800)
repository/repository_ajax.php

index cb7dea8..5258fb5 100644 (file)
@@ -254,21 +254,24 @@ switch ($action) {
                     $event['existingfile']->filepath = $saveas_path;
                     $event['existingfile']->filename = $saveas_filename;
                     $event['existingfile']->url      = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();;
-                    echo json_encode($event);
-                    die;
+                } else {
+                    $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
+                    $event = array(
+                        'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
+                        'id'=>$storedfile->get_itemid(),
+                        'file'=>$storedfile->get_filename(),
+                        'icon' => $OUTPUT->pix_url(file_file_icon($storedfile, 32))->out(),
+                    );
+                }
+                if ($maxbytes !== -1 && $storedfile->get_filesize() > $maxbytes) {
+                    $storedfile->delete();
+                    throw new file_exception('maxbytes');
                 }
-                $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
                 // Repository plugin callback
                 // You can cache reository file in this callback
                 // or complete other tasks.
                 $repo->cache_file_by_reference($reference, $storedfile);
-                $info = array(
-                    'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
-                    'id'=>$storedfile->get_itemid(),
-                    'file'=>$storedfile->get_filename(),
-                    'icon' => $OUTPUT->pix_url(file_file_icon($storedfile, 32))->out(),
-                );
-                echo json_encode($info);
+                echo json_encode($event);
                 die;
             } else if ($repo->has_moodle_files()) {
                 // Some repository plugins (local, user, coursefiles, recent) are hosting moodle