MDL-22991, fixed file api calls
authorDongsheng Cai <unoter@gmail.com>
Tue, 6 Jul 2010 07:15:08 +0000 (07:15 +0000)
committerDongsheng Cai <unoter@gmail.com>
Tue, 6 Jul 2010 07:15:08 +0000 (07:15 +0000)
repository/local/lib.php
repository/recent/lib.php
repository/repository_ajax.php
repository/user/lib.php

index 184d889..96f04ad 100755 (executable)
@@ -56,6 +56,7 @@ class repository_local extends repository {
                 $filename = clean_param($params['filename'], PARAM_FILE);
                 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
                 $filepath = clean_param($params['filepath'], PARAM_PATH);;
+                $component = clean_param($params['component'], PARAM_ALPHAEXT);
                 $context  = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
             }
         } else {
@@ -63,13 +64,13 @@ class repository_local extends repository {
             $filename = null;
             $filearea = null;
             $filepath = null;
+            $component = null;
             $context  = get_system_context();
         }
 
         $browser = get_file_browser();
 
-        if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) {
-            echo_fb($fileinfo);
+        if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
             // build path navigation
             $pathnodes = array();
             $encodedpath = base64_encode(serialize($fileinfo->get_params()));
@@ -172,11 +173,12 @@ class repository_local extends repository {
         $filename = clean_param($params['filename'], PARAM_FILE);
         $filepath = clean_param($params['filepath'], PARAM_PATH);;
         $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
+        $component = clean_param($params['component'], PARAM_ALPHAEXT);
 
         $context = get_context_instance_by_id($contextid);
 
-        $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
-        $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename);
+        $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
+        $file_info->copy_to_storage($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename);
 
         $info['itemid'] = $new_itemid;
         $info['title']  = $new_filename;
index 5f5216e..53ee42b 100755 (executable)
@@ -70,6 +70,7 @@ class repository_recent extends repository {
             $info['contextid'] = $file_record->contextid;
             $info['itemid'] = $file_record->itemid;
             $info['filearea'] = $file_record->filearea;
+            $info['component'] = $file_record->component;
             $info['filepath'] = $file_record->filepath;
             $info['filename'] = $file_record->filename;
             $result[$file_record->pathnamehash] = $info;
@@ -171,6 +172,7 @@ class repository_recent extends repository {
         $filename = clean_param($params['filename'], PARAM_FILE);
         $filepath = clean_param($params['filepath'], PARAM_PATH);;
         $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
+        $component = clean_param($params['component'], PARAM_ALPHAEXT);
 
         // XXX:
         // When user try to pick a file from other filearea, normally file api will use file browse to
@@ -179,10 +181,10 @@ class repository_recent extends repository {
         //
         // To get 'recent' plugin working, we need to use lower level file_stoarge class to bypass the
         // capability check, we will use a better workaround to improve it.
-        if ($stored_file = $fs->get_file($contextid, $filearea, $fileitemid, $filepath, $filename)) {
-            $file_record = array('contextid'=>$user_context->id, 'filearea'=>$new_filearea,
+        if ($stored_file = $fs->get_file($contextid, $component, $filearea, $fileitemid, $filepath, $filename)) {
+            $file_record = array('contextid'=>$user_context->id, 'component'=>'user', 'filearea'=>'draft',
                 'itemid'=>$new_itemid, 'filepath'=>$new_filepath, 'filename'=>$new_filename);
-            if ($file = $fs->get_file($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename)) {
+            if ($file = $fs->get_file($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename)) {
                 $file->delete();
             }
             $fs->create_file_from_storedfile($file_record, $stored_file);
index 53c07de..d8d9f2d 100755 (executable)
@@ -168,7 +168,7 @@ switch ($action) {
             $mimetypes = '*';
         } else {
             foreach ($accepted_types as $type) {
-                $mimetypes = mimeinfo('type', $type);
+                $mimetypes[] = mimeinfo('type', $type);
             }
             if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
                 throw new moodle_exception('invalidfiletype', 'repository', '', mimeinfo('type', $_FILES[$elname]['name']));
@@ -183,7 +183,7 @@ switch ($action) {
             $info = array();
             $info['file'] = $fileinfo['title'];
             $info['id'] = $itemid;
-            $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
+            $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
             $filesize = $fileinfo['filesize'];
             if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
                 throw new file_exception('maxbytes');
index dfb4d98..1fc6c32 100755 (executable)
@@ -56,20 +56,22 @@ class repository_user extends repository {
                 $filename = clean_param($params['filename'], PARAM_FILE);
                 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
                 $filepath = clean_param($params['filepath'], PARAM_PATH);;
+                $component = clean_param($params['component'], PARAM_ALPHAEXT);
                 $context  = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
             }
         } else {
             $itemid   = 0;
             $filename = null;
-            $filearea = 'user_private';
+            $filearea = 'private';
             $filepath = '/';
+            $component = 'user';
             $context = get_context_instance(CONTEXT_USER, $USER->id);
         }
 
         try {
             $browser = get_file_browser();
 
-            if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) {
+            if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
                 $pathnodes = array();
                 $level = $fileinfo;
                 $params = $fileinfo->get_params();
@@ -158,10 +160,11 @@ class repository_user extends repository {
         $filename = clean_param($params['filename'], PARAM_FILE);
         $filepath = clean_param($params['filepath'], PARAM_PATH);;
         $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
+        $component = clean_param($params['component'], PARAM_ALPHAEXT);
 
         $context    = get_context_instance_by_id($contextid);
-        $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
-        $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename);
+        $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
+        $file_info->copy_to_storage($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename);
 
         $info = array();
         $info['itemid'] = $new_itemid;