diff --git a/resources/node_resource.inc b/resources/node_resource.inc index 928e09d..179835e 100644 --- a/resources/node_resource.inc +++ b/resources/node_resource.inc @@ -114,6 +114,31 @@ function _node_resource_definition() { 'access arguments' => array('access content'), ), ), + 'targeted_actions' => array( + 'attach_file' => array( + 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), + 'callback' => '_node_resource_attach_file', + 'args' => array( + array( + 'name' => 'nid', + 'optional' => FALSE, + 'source' => array('path' => 0), + 'type' => 'int', + 'description' => 'The nid of the node to attach a file to', + ), + array( + 'name' => 'field_name', + 'optional' => TRUE, + 'source' => array('param' => 'field_name'), + 'description' => 'The file parameters', + 'type' => 'array', + ), + ), + 'access callback' => '_node_resource_access', + 'access arguments' => array('update'), + 'access arguments append' => TRUE, + ), + ), 'relationships' => array( 'files' => array( 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), @@ -534,3 +559,73 @@ function _node_resource_load_node_comments($nid, $count = 0, $start = 0) { return !empty($cids) ? comment_load_multiple($cids) : array(); } + +/** + * Attaches file(s) to an existing node based on nodeID, and the field's + * machine_name + * + * Example form element used to post files to attach_file: + *
+ * + * + * + * The name="files[anything]" format is required to use file_save_upload(). + * + * @param $nid + * Node ID of the node the file(s) is being attached to. + * @param $field_name + * Machine name of the field that is attached to the node. + * @return + * An array of files that were attached in the form: + * array( array( fid=>N, uri=>public://path/to/file1), array( fid=>N, + * uri=>public://path/to/file2) ) + * + * @see file_save_upload() + * @see file + */ +function _node_resource_attach_file($nid, $field_name) { + global $user; + //Load the information for this field + $node = node_load($nid); + $field = field_read_field($field_name[0]); + $instance = field_read_instance('node', $field_name[0], $node->type); + $destination = file_field_widget_uri($field, $instance ); + + $validators = array( + 'file_validate_extensions' => array(0 => $instance['settings']['file_extensions']), + 'file_validate_size' => array(), + ); + + $files = array(); + $counter=0; + foreach ($_FILES['files']['name'] as $key => $val) { + + // let the file module handle the upload and moving. + if (!$file = file_save_upload($key, $validators, $destination, FILE_EXISTS_RENAME) ) { + return services_error(t('Failed to upload file. %upload', array('%upload' => $key)), 406); + } + + // Required to be able to reference this file. + if ($file->fid) { + //Add the file to the node. + $node->{$field_name[0]}['und'][$counter] = (array)$file; + //Add info to the array that will be returned/encdoed to xml/json + $files[] = array( + 'fid' => $file->fid, + 'uri' => services_resource_uri(array('file', $file->fid)), + ); + } + else { + return services_error(t('An unknown error occurred'), 500); + } + + $counter++; + //Check if the maximum number of files for this field has been reached. A + //cardinality of -1 (unlimited) will never match + if ($field['cardinality'] == $counter ) { break; } + } + node_save($node); + + return $files; +} diff --git a/servers/rest_server/includes/RESTServer.inc b/servers/rest_server/includes/RESTServer.inc index a456b76..55adb99 100755 --- a/servers/rest_server/includes/RESTServer.inc +++ b/servers/rest_server/includes/RESTServer.inc @@ -471,7 +471,7 @@ class RESTServer { } public static function parseMultipart() { - return $_POST; + return $_FILES; } public static function parseYAML($handle) {