forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-22236 Webservice file upload - Allow webservice file uploads dire…
…ct to a draft filearea. Via post to /webservice/upload.php
- Loading branch information
Damyon Wiese
committed
Aug 8, 2013
1 parent
f05ea82
commit 106c55f
Showing
11 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
This files describes API changes in /webservice/* | ||
information provided here is intended especially for developers. | ||
|
||
This information is intended for authors of webservices, not people writing webservice clients. | ||
|
||
=== 2.6 === | ||
|
||
* webservice/upload.php | ||
Accepts 2 new post parameters to allow uploading of files to a users draft area. | ||
- filearea should be either 'private' (default) or 'draft' | ||
- itemid unused if the filearea is 'private', for 'draft' it can be the id of a previously | ||
created draft area - or 0 which will generate a new draft area for the files. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,11 @@ | |
* filepath => the private file aera path (where files will be stored) | ||
* [_FILES] => for example you can send the files with <input type=file>, | ||
* or with curl magic: 'file_1' => '@/path/to/file', or ... | ||
* filearea => 'private' or 'draft' (default = 'private'). These are the only 2 areas we are allowing | ||
* direct uploads via webservices. The private file area is deprecated - please don't use it. | ||
* itemid => For draft areas this is the draftid - this can be used to add a list of files | ||
* to a draft area in separate requests. If it is 0, a new draftid will be generated. | ||
* For private files, this is ignored. | ||
* | ||
* @package core_webservice | ||
* @copyright 2011 Dongsheng Cai <[email protected]> | ||
|
@@ -42,13 +47,22 @@ | |
require_once(dirname(dirname(__FILE__)) . '/config.php'); | ||
require_once($CFG->dirroot . '/webservice/lib.php'); | ||
$filepath = optional_param('filepath', '/', PARAM_PATH); | ||
// The default file area is 'private' for user private files. This | ||
// area is actually deprecated and only supported for backwards compatibility with | ||
// the mobile app. | ||
$filearea = optional_param('filearea', 'private', PARAM_ALPHA); | ||
$itemid = optional_param('itemid', 0, PARAM_INT); | ||
|
||
echo $OUTPUT->header(); | ||
|
||
// authenticate the user | ||
$token = required_param('token', PARAM_ALPHANUM); | ||
$webservicelib = new webservice(); | ||
$authenticationinfo = $webservicelib->authenticate_user($token); | ||
$fileuploaddisabled = empty($authenticationinfo['service']->uploadfiles); | ||
if ($fileuploaddisabled) { | ||
throw new webservice_access_exception('Web service file upload must be enabled in external service settings'); | ||
} | ||
|
||
// check the user can manage his own files (can upload) | ||
$context = context_user::instance($USER->id); | ||
|
@@ -105,13 +119,34 @@ | |
|
||
$fs = get_file_storage(); | ||
|
||
$usedspace = 0; | ||
$privatefiles = $fs->get_area_files($context->id, 'user', 'private', false, 'id', false); | ||
foreach ($privatefiles as $file) { | ||
$usedspace += $file->get_filesize(); | ||
if ($filearea == 'draft' && $itemid <= 0) { | ||
$itemid = file_get_unused_draft_itemid(); | ||
} | ||
|
||
// Get any existing file size limits. | ||
$maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED; | ||
$maxupload = get_user_max_upload_file_size($context, $CFG->maxbytes); | ||
if ($filearea == 'private') { | ||
// Private files area is limited by $CFG->userquota. | ||
if (!has_capability('moodle/user:ignoreuserquota', $context)) { | ||
$maxareabytes = $CFG->userquota; | ||
} | ||
} | ||
|
||
// Count the size of all existing files in this area. | ||
if ($maxareabytes > 0) { | ||
$usedspace = 0; | ||
$existingfiles = $fs->get_area_files($context->id, 'user', $filearea, false, 'id', false); | ||
foreach ($existingfiles as $file) { | ||
$usedspace += $file->get_filesize(); | ||
} | ||
if ($totalsize > ($maxareabytes - $usedspace)) { | ||
throw new file_exception('userquotalimit'); | ||
} | ||
} | ||
|
||
if ($totalsize > ($CFG->userquota - $usedspace)) { | ||
// Check the size of this upload. | ||
if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload) { | ||
throw new file_exception('userquotalimit'); | ||
} | ||
|
||
|
@@ -126,10 +161,10 @@ | |
$file_record->component = 'user'; | ||
$file_record->contextid = $context->id; | ||
$file_record->userid = $USER->id; | ||
$file_record->filearea = 'private'; | ||
$file_record->filearea = $filearea; | ||
$file_record->filename = $file->filename; | ||
$file_record->filepath = $filepath; | ||
$file_record->itemid = 0; | ||
$file_record->itemid = $itemid; | ||
$file_record->license = $CFG->sitedefaultlicense; | ||
$file_record->author = fullname($authenticationinfo['user']); | ||
$file_record->source = ''; | ||
|