Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Shared files #121

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@
OC\Files\Filesystem::signal_post_rename,
'OCA\Search_Lucene\Hooks\Files',
OCA\Search_Lucene\Hooks\Files::handle_post_rename);

//register to receive notification of sharing status changes
OCP\Util::connectHook(
'OCP\Share',
'post_shared',
'OCA\Search_Lucene\Hooks\Share',
'postShareHook');

OCP\Util::connectHook('OCP\Share',
'post_unshare',
'OCA\Search_Lucene\Hooks\Share',
'postUnshareHook');
4 changes: 4 additions & 0 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Search_Lucene\Controller\ApiController;
use OCA\Search_Lucene\Core\Logger;
use OCA\Search_Lucene\Db\StatusMapper;
use OCA\Search_Lucene\Db\FilesInFolder;
use OCA\Search_Lucene\Lucene\Index;
use OCA\Search_Lucene\Lucene\Indexer;
use OCA\Search_Lucene\Core\Files;
Expand Down Expand Up @@ -119,6 +120,9 @@ public function __construct (array $urlParams=array()) {
return $c->query('ServerContainer')->getRootFolder();
});

$container->registerService('FilesInFolder', function($c) {
return new FilesInFolder($c->query('Db'));
});
}


Expand Down
4 changes: 1 addition & 3 deletions core/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public function setUpIndexFolder($userId = null) {
// FIXME \OC::$server->getAppFolder() returns '/search'
//$indexFolder = \OC::$server->getAppFolder();

$userHome = $this->setUpUserHome($userId);

return $this->getOrCreateSubFolder($userHome, 'lucene_index');
return $this->getOrCreateSubFolder($this->rootFolder, '/' . 'lucene_index');
}

/**
Expand Down
61 changes: 61 additions & 0 deletions db/filesinfolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* ownCloud - search_lucene
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Devin M. Ceartas <[email protected]>
* @copyright Devin M. Ceartas 2016
*/

namespace OCA\Search_Lucene\Db;

use OCP\IDb;

class FilesInFolder {

private $db;
private $files;

public function __construct(IDb $db) {
$this->db = $db;
$this->files = array();
}

public function files($folder_id) {
$sth = $this->db->prepare('SELECT fileid FROM `*PREFIX*filecache` WHERE `parent` = ?');
$sth->bindValue(1, $folder_id, \PDO::PARAM_INT);
$sth->execute();
$result = $sth->fetchAll();

foreach ($result as $row) {
if ($this->is_sub_folder($row['fileid'])) {
return $this->files($row['fileid']);
} else {
$this->files[] = $row['fileid'];
}
}

return array_unique($this->files);
}

private function is_sub_folder($folder_id) {
$inner = $this->db->prepare('SELECT count(fileid) AS `is_sub` FROM `*PREFIX*filecache` WHERE `parent` = ?');
$inner->bindValue(1, $folder_id, \PDO::PARAM_INT);
$inner->execute();
$sub_result = $inner->fetchAll();
return ($sub_result['is_sub'] > 0);
}

public function logit($text) {
$FH = fopen('/var/www/html/data/borealis.log', 'a');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will just use logger instead

if(is_string($text) || is_numeric($text) || is_bool($text)) {
fwrite($FH, $text . "\n");
} else {
fwrite($FH, print_r($text, true));
}
fclose($FH);
}

}
56 changes: 56 additions & 0 deletions hooks/share.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* ownCloud - search_lucene
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Devin M. Ceartas <[email protected]>
* @copyright Devin M. Ceartas 2016
*/

namespace OCA\Search_Lucene\Hooks;

use OCA\Search_Lucene\AppInfo\Application;

class Share {

private static function reIndex($fileIds) {
$app = new Application();
$container = $app->getContainer();
self::logit(get_class($container));
$container->query('Indexer')->indexFiles($fileIds);
}

public static function postShareHook(array $param) {
if ('file' == $param['itemType']) {
Copy link
Contributor Author

@georgehrke georgehrke Apr 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ===

self::reIndex(array($param['itemSource']));
} else {
$app = new Application();
$container = $app->getContainer();
$files = $container->query('FilesInFolder')->files($param['itemSource']);
self::reIndex($files);
}
}

public static function postUnshareHook(array $param) {
if ('file' == $param['itemType']) {
self::reIndex(array($param['itemSource']));
} else {
$app = new Application();
$container = $app->getContainer();
$files = $container->query('FilesInFolder')->files($param['itemSource']);
self::reIndex($files);
}
}

public static function logit($text) {
$FH = fopen( '/var/www/html/data/borealis.log', 'a' );
if(is_string($text) || is_numeric($text) || is_bool($text)) {
fwrite($FH, $text . "\n");
} else {
fwrite($FH, print_r($text, true));
}
fclose( $FH );
}
}
11 changes: 2 additions & 9 deletions jobs/deletejob.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,8 @@ public function run($arguments){
/** @var Logger $logger */
$logger = $container->query('Logger');


if (empty($arguments['user'])) {
$logger->debug('indexer job did not receive user in arguments: '.json_encode($arguments) );
return;
}

$userId = $arguments['user'];
$logger->debug('background job optimizing index for '.$userId );
$container->query('FileUtility')->setUpIndexFolder($userId);
$logger->debug('background job optimizing index');
$container->query('FileUtility')->setUpIndexFolder();

/** @var Index $index */
$index = $container->query('Index');
Expand Down
11 changes: 3 additions & 8 deletions jobs/optimizejob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ public function run($arguments){
/** @var Logger $logger */
$logger = $container->query('Logger');

if (!empty($arguments['user'])) {
$userId = $arguments['user'];
$logger->debug('background job optimizing index for '.$userId );
$container->query('FileUtility')->setUpIndexFolder($userId);
$container->query('Index')->optimizeIndex();
} else {
$logger->debug('indexer job did not receive user in arguments: '.json_encode($arguments) );
}
$logger->debug('background job optimizing index');
$container->query('FileUtility')->setUpIndexFolder();
$container->query('Index')->optimizeIndex();
}
}
13 changes: 13 additions & 0 deletions lucene/indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OCA\Search_Lucene\Lucene;

use OCP\Files\File;
use OCP\Share;
use OCA\Search_Lucene\Core\Files;
use OCA\Search_Lucene\Db\StatusMapper;
use OCA\Search_Lucene\Document\Ods;
Expand Down Expand Up @@ -236,6 +237,18 @@ public function indexFile(File $file, $commit = true) {

// Store filecache id as unique id to lookup by when deleting
$doc->addField(Document\Field::Keyword('fileId', $file->getId()));

// Store names of individuals who have read permissions
// FIXME: this next four lines seem a bit awkward. The goal is to get the "path" as
// getUsersSharingFile() method expects it, which is relative to the user's dir
// of files, without assuming the data directory is the same as the user name.
$full_home_dir = \OC::$server->getUserManager()->get(\OC_User::getUser())->getHome();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getHome called on potential null

$datadirectory = \OC::$server->getConfig()->getSystemValue('datadirectory');
$prefix = preg_replace( "!^" . $datadirectory . "!", "", $full_home_dir ) . '/files/';
$path = preg_replace( '!'.$prefix.'!', "", $file->getPath());
$canReadIndiv = Share::getUsersSharingFile($path, \OC_User::getUser(), true, false);
$concatenated = '_' . implode('_', $canReadIndiv['users']) . '_';
$doc->addField(Document\Field::Text('can_read', $concatenated, 'UTF-8'));

// Store document path for the search results
$doc->addField(Document\Field::Text('path', $file->getPath(), 'UTF-8'));
Expand Down
4 changes: 4 additions & 0 deletions search/luceneprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function search($query){
$hits = $index->find($query);

//limit results. we cant show more than ~30 anyway. TODO use paging later
$user = \OC_User::getUser();
for ($i = 0; $i < 30 && $i < count($hits); $i++) {
if (false === strpos($hits[$i]->can_read, '_'.$user.'_')) {
continue;
}
$results[] = new LuceneResult($hits[$i]);
}

Expand Down
15 changes: 9 additions & 6 deletions search/luceneresult.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ class LuceneResult extends File {
*/
public function __construct(QueryHit $hit) {
$this->id = (string)$hit->fileId;
$this->path = $this->getRelativePath($hit->path);
$this->path = $hit->path;
$this->name = basename($this->path);
$this->size = (int)$hit->size;
$this->score = $hit->score;
$this->link = \OCP\Util::linkTo(
'files',
'index.php',
array('dir' => dirname($this->path), 'scrollto' => $this->name)
$dir = preg_replace("!/".$this->name."$!", "", $hit->path);
$this->link = \OC::$server->getURLGenerator()->linkToRoute(
'files.view.index',
[
'dir' => $dir,
'scrollto' => $this->name,
]
);
$this->permissions = $this->getPermissions($this->path);
$this->modified = (int)$hit->mtime;
Expand All @@ -53,7 +56,7 @@ public function __construct(QueryHit $hit) {
protected function getRelativePath ($path) {
$root = \OC::$server->getUserFolder();
return $root->getRelativePath($path);
}
}

/**
* Determine permissions for a given file path
Expand Down
6 changes: 2 additions & 4 deletions tests/unit/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ public function setUp() {

// create test user
$this->userName = 'test';
$user = $um->get($this->userName);
if ($user) {
$user->delete();
if (!\OC_User::userExists($this->userName)) {
$um->createUser($this->userName, $this->userName);
}
$um->createUser($this->userName, $this->userName);

\OC_Util::tearDownFS();
$this->userSession->setUser(null);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/testsearchprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function testSearchLuceneResultContent($fileId, $name, $path, $size, $score, $mi
$this->assertInstanceOf('OCA\Search_Lucene\Search\LuceneResult', $searchResult);
$this->assertEquals($fileId, $searchResult->id);
$this->assertEquals('lucene', $searchResult->type);
$this->assertEquals($path, $searchResult->path);
$this->assertEquals('/test/files'.$path, $searchResult->path);
$this->assertEquals($name, $searchResult->name);
$this->assertEquals($mimeType, $searchResult->mime_type);
$this->assertEquals($size, $searchResult->size);
Expand Down