diff --git a/resources/file_resource.inc b/resources/file_resource.inc index 551247d..5709639 100644 --- a/resources/file_resource.inc +++ b/resources/file_resource.inc @@ -73,33 +73,6 @@ function _file_resource_definition() { ), ), ), - 'actions' => array( - 'nodeFiles' => array( - 'help' => t('This method returns the files on a given node.'), - 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/file_resource'), - 'access callback' => '_file_resource_node_access', - 'access arguments' => array('view'), - 'access arguments append' => TRUE, - 'callback' => '_file_resource_load_node_files', - 'args' => array( - array( - 'name' => 'nid', - 'type' => 'int', - 'description' => t('The node id to load files for.'), - 'source' => 'data', - 'optional' => FALSE, - ), - array( - 'name' => 'file_contents', - 'type' => 'int', - 'description' => t('To return file contents or not.'), - 'source' => 'data', - 'optional' => FALSE, - 'default value' => TRUE, - ), - ), - ), - ), ), ); } diff --git a/resources/node_resource.inc b/resources/node_resource.inc index 7d6e609..9a70c4f 100644 --- a/resources/node_resource.inc +++ b/resources/node_resource.inc @@ -104,6 +104,33 @@ function _node_resource_definition() { ), 'access arguments' => array('access content'), ), + 'relationships' => array( + 'nodeFiles' => array( + 'help' => t('This method returns the files on a given node.'), + 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'), + 'access callback' => '_node_resource_access', + 'access arguments' => array('view'), + 'access arguments append' => TRUE, + 'callback' => '_node_resource_load_node_files', + 'args' => array( + array( + 'name' => 'nid', + 'type' => 'int', + 'description' => t('The node id to load files for.'), + 'source' => 'data', + 'optional' => FALSE, + ), + array( + 'name' => 'file_contents', + 'type' => 'int', + 'description' => t('To return file contents or not.'), + 'source' => 'data', + 'optional' => FALSE, + 'default value' => TRUE, + ), + ), + ), + ), ), ); } @@ -321,6 +348,49 @@ function _node_resource_index($page, $fields, $parameters) { return $nodes; } +/** + * Generates an array of base64 encoded files attached to a node + * + * @param $nid + * Number. Node ID + * @param $include_file_contents + * Bool Whether or not to include the base64_encoded version of the file. + * @return + * Array. A list of all files from the given node + */ +function _node_resource_load_node_files($nid, $include_file_contents = TRUE) { + module_load_include('inc', 'services', 'resources/file_resource'); + $node = node_load($nid); + if (!isset($node->files)) { + return services_error(t('There are no files on given node.')); + } + + $return = array(); + foreach ($node->files as $file) { + // Do not return files that are not listed. + if (!$file->list) { + continue; + } + $return[$file->fid] = array( + 'filename' => $file->filename, + 'uid' => $file->uid, + 'filemime' => $file->filemime, + 'filesize' => $file->filesize, + 'status' => $file->status, + 'timestamp' => $file->timestamp + ); + // If to add content of the file. + if ($file_include_contents) { + $filepath = file_create_path($file->filepath); + $binaryfile = fopen($filepath, 'rb'); + $return[$file->fid]['file'] = base64_encode(fread($binaryfile, filesize($filepath))); + fclose($binaryfile); + } + + } + return $return; +} + /** * Determine whether the current user can access a node resource. * diff --git a/tests/functional/ServicesResourceFileTests.test b/tests/functional/ServicesResourceFileTests.test index 7ef54b2..f2e750f 100644 --- a/tests/functional/ServicesResourceFileTests.test +++ b/tests/functional/ServicesResourceFileTests.test @@ -127,39 +127,7 @@ class ServicesResourceFileTests extends ServicesWebTestCase { $fid = db_result(db_query('SELECT fid FROM {files} WHERE fid = %d', $testfile['fid'])); $this->assertTrue(empty($fid), t('File deleted.'), 'FileResource: Delete'); } - - /** - * Test nodeFiles action. - */ - public function testNodeFiles() { - $testfile1 = $this->uploadTestFile(); - $testfile2 = $this->uploadTestFile(); - $testfile3 = $this->uploadTestFile(); - - // Needed for upload module to create new records in {upload} table. - // @see upload_save() - $testfile1['new'] = TRUE; - $testfile2['new'] = TRUE; - $testfile3['new'] = TRUE; - // First two files are listed. Third is not listed. - $testfile1['list'] = 1; - $testfile2['list'] = 1; - $testfile3['list'] = 0; - - // Create node with three files. - $settings = array('files' => array( - $testfile1['fid'] => (object)$testfile1, - $testfile2['fid'] => (object)$testfile2, - $testfile3['fid'] => (object)$testfile3, - )); - $node = $this->drupalCreateNode($settings); - - $result = $this->servicesPost($this->endpoint->path . '/file/nodeFiles', array('nid' => $node->nid)); - $this->assertTrue(isset($result['body'][$testfile1['fid']]) && isset($result['body'][$testfile2['fid']]) && !isset($result['body'][$testfile3['fid']]), - t('Attached files listed.'), 'FileResource: nodeFiles'); - } - - /** + /** * Emulate uploaded file. * * Copy file from simpletest file samples and create record in files table. diff --git a/tests/functional/ServicesResourceNodeTests.test b/tests/functional/ServicesResourceNodeTests.test index ce6b38e..3a20609 100644 --- a/tests/functional/ServicesResourceNodeTests.test +++ b/tests/functional/ServicesResourceNodeTests.test @@ -29,7 +29,8 @@ class ServicesResourceNodetests extends ServicesWebtestCase { 'services', 'rest_server', 'services_sessauth', - 'inputstream' + 'inputstream', + 'upload' ); // Set up endpoint. $this->endpoint = $this->saveNewEndpoint(); @@ -201,7 +202,45 @@ class ServicesResourceNodetests extends ServicesWebtestCase { $terms_number = db_result(db_query('SELECT COUNT(*) result FROM {term_node} r INNER JOIN {node} n ON n.vid = r.vid WHERE n.nid = %d', $response['body']['nid'])); $this->assertTrue($terms_number == 2, t('Node with two terms was successfully created.'), 'NodeResource: Create'); } + /** + * Test nodeFiles action. + */ + public function testNodeFiles() { + $this->privileged_user = $this->drupalCreateUser(array( + 'get own binary files', + 'save file information', + 'administer services', + 'administer site configuration', + 'upload files', + )); + $this->drupalLogin($this->privileged_user); + $testfiles = $this->drupalGetTestFiles('text'); + $testfile1 = $this->uploadTestFile($testfiles); + $testfile2 = $this->uploadTestFile($testfiles); + $testfile3 = $this->uploadTestFile($testfiles); + + // Needed for upload module to create new records in {upload} table. + // @see upload_save() + $testfile1['new'] = TRUE; + $testfile2['new'] = TRUE; + $testfile3['new'] = TRUE; + // First two files are listed. Third is not listed. + $testfile1['list'] = 1; + $testfile2['list'] = 1; + $testfile3['list'] = 0; + + // Create node with three files. + $settings = array('files' => array( + $testfile1['fid'] => (object)$testfile1, + $testfile2['fid'] => (object)$testfile2, + $testfile3['fid'] => (object)$testfile3, + )); + $node = $this->drupalCreateNode($settings); + $result = $this->servicesPost($this->endpoint->path . '/node/nodeFiles', array('nid' => $node->nid)); + $this->assertTrue(isset($result['body'][$testfile1['fid']]) && isset($result['body'][$testfile2['fid']]) && !isset($result['body'][$testfile3['fid']]), + t('Attached files listed.'), 'FileResource: nodeFiles'); + } /** * testing node_resource Update */ @@ -299,4 +338,36 @@ class ServicesResourceNodetests extends ServicesWebtestCase { $this->assertFalse($responseArray['code'] == 200, t('Node was deleted. It shoudlnt have been because it doesnt exist'), 'NodeResource: Deleted'); } + /** + * Emulate uploaded file. + * + * Copy file from simpletest file samples and create record in files table. + * + * @return array + * File data. + */ + public function uploadTestFile($file = NULL) { + if (empty($file)) { + $file = next($this->testfiles); + } + $testfile = array( + 'fid' => NULL, + 'uid' => $this->privileged_user->uid, + 'filename' => trim(basename($file->filename), '.'), + 'filepath' => $file->filename, + 'filemime' => file_get_mimetype($file->filename), + 'filesize' => filesize($file->filename), + 'status' => FILE_STATUS_PERMANENT, + 'timestamp' => time(), + ); + $source = $testfile['filepath']; + $destination = file_directory_path() . '/' . $testfile['filepath']; + $dirname = dirname($destination); + file_check_directory($dirname, FILE_CREATE_DIRECTORY); + file_copy($source, $destination); + + drupal_write_record('files', $testfile); + + return $testfile; + } }