exlu collection in quarantaine when upload

This commit is contained in:
aynsix
2019-12-11 11:48:23 +04:00
parent 867d5d40b1
commit 1c0b1db9c7
8 changed files with 172 additions and 6 deletions

View File

@@ -34,6 +34,11 @@ abstract class AbstractChecker implements CheckerInterface
*/ */
protected $collections = []; protected $collections = [];
/**
* @var \collection[]
*/
protected $ignoreCollections = [];
public function __construct(Application $app) public function __construct(Application $app)
{ {
$this->app = $app; $this->app = $app;
@@ -44,7 +49,7 @@ abstract class AbstractChecker implements CheckerInterface
* Warning, you can not restrict on both databoxes and collections * Warning, you can not restrict on both databoxes and collections
* *
* @param \databox[] $databoxes A databox or an array of databoxes * @param \databox[] $databoxes A databox or an array of databoxes
* @return bool * @return \databox[]
* *
* @throws \LogicException If already restricted to collections * @throws \LogicException If already restricted to collections
* @throws \InvalidArgumentException In case invalid databoxes are provided * @throws \InvalidArgumentException In case invalid databoxes are provided
@@ -72,7 +77,7 @@ abstract class AbstractChecker implements CheckerInterface
* Warning, you can not restrict on both databoxes and collections * Warning, you can not restrict on both databoxes and collections
* *
* @param \collection[] $collections * @param \collection[] $collections
* @return bool * @return \collection[]
* *
* @throws \LogicException If already restricted to databoxes * @throws \LogicException If already restricted to databoxes
* @throws \InvalidArgumentException In case invalid collections are provided * @throws \InvalidArgumentException In case invalid collections are provided
@@ -95,6 +100,11 @@ abstract class AbstractChecker implements CheckerInterface
return $this->collections; return $this->collections;
} }
public function setIgnoreCollections($collections)
{
$this->ignoreCollections = $collections;
}
/** /**
* Returns true if the checker should be executed against the current file * Returns true if the checker should be executed against the current file
* *

View File

@@ -45,8 +45,18 @@ class Filename extends AbstractChecker
*/ */
public function check(EntityManager $em, File $file) public function check(EntityManager $em, File $file)
{ {
$boolean = empty(\record_adapter::get_records_by_originalname( $excludedCollIds = [];
$file->getCollection()->get_databox(), $file->getOriginalName(), $this->sensitive, 0, 1 if (!empty($this->ignoreCollections)) {
foreach ($this->ignoreCollections as $collection) {
// use only collection in the same databox and retrieve the coll_id
if ($collection->get_sbas_id() === $file->getCollection()->get_sbas_id()) {
$excludedCollIds[] = $collection->get_coll_id();
}
}
}
$boolean = empty(\record_adapter::getRecordsByOriginalnameWithExcludedCollIds(
$file->getCollection()->get_databox(), $file->getOriginalName(), $this->sensitive, 0, 1, $excludedCollIds
)); ));
return new Response($boolean, $this); return new Response($boolean, $this);

View File

@@ -34,7 +34,17 @@ class Sha256 extends AbstractChecker
*/ */
public function check(EntityManager $em, File $file) public function check(EntityManager $em, File $file)
{ {
$boolean = empty($file->getCollection()->get_databox()->getRecordRepository()->findBySha256($file->getSha256())); $excludedCollIds = [];
if (!empty($this->ignoreCollections)) {
foreach ($this->ignoreCollections as $collection) {
// use only collection in the same databox and retrieve the coll_id
if ($collection->get_sbas_id() === $file->getCollection()->get_sbas_id()) {
$excludedCollIds[] = $collection->get_coll_id();
}
}
}
$boolean = empty($file->getCollection()->get_databox()->getRecordRepository()->findBySha256WithExcludedCollIds($file->getSha256(), $excludedCollIds));
return new Response($boolean, $this); return new Response($boolean, $this);
} }

View File

@@ -33,7 +33,17 @@ class UUID extends AbstractChecker
*/ */
public function check(EntityManager $em, File $file) public function check(EntityManager $em, File $file)
{ {
$boolean = empty($file->getCollection()->get_databox()->getRecordRepository()->findByUuid($file->getUUID())); $excludedCollIds = [];
if (!empty($this->ignoreCollections)) {
foreach ($this->ignoreCollections as $collection) {
// use only collection in the same databox and retrieve the coll_id
if ($collection->get_sbas_id() === $file->getCollection()->get_sbas_id()) {
$excludedCollIds[] = $collection->get_coll_id();
}
}
}
$boolean = empty($file->getCollection()->get_databox()->getRecordRepository()->findByUuidWithExcludedCollIds($file->getUUID(), $excludedCollIds));
return new Response($boolean, $this); return new Response($boolean, $this);
} }

View File

@@ -78,6 +78,20 @@ class BorderManagerServiceProvider implements ServiceProviderInterface
$checkerObj->restrictToCollections($collections); $checkerObj->restrictToCollections($collections);
} }
if (isset($checker['compare-ignore-collections'])) {
$collections = [];
foreach ($checker['compare-ignore-collections'] as $base_id) {
try {
$collections[] = \collection::getByBaseId($app, $base_id);
} catch (\Exception $e) {
throw new \InvalidArgumentException('Invalid collection option');
}
}
$checkerObj->setIgnoreCollections($collections);
}
$registeredCheckers[] = $checkerObj; $registeredCheckers[] = $checkerObj;
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$app['monolog']->error( $app['monolog']->error(

View File

@@ -82,6 +82,34 @@ class LegacyRecordRepository implements RecordRepository
return $this->mapRecordsFromResultSet($result); return $this->mapRecordsFromResultSet($result);
} }
public function findBySha256WithExcludedCollIds($sha256, $excludedCollIds = [])
{
static $sql;
$params = [];
$types = [];
if (!$sql) {
$qb = $this->createSelectBuilder()
->where('sha256 = :sha256');
$params['sha256'] = $sha256;
if (!empty($excludedCollIds)) {
$qb->andWhere($qb->expr()->notIn('coll_id', ':coll_id'));
$params['coll_id'] = $excludedCollIds;
$types[':coll_id'] = Connection::PARAM_INT_ARRAY;
}
$sql = $qb->getSQL();
}
$result = $this->databox->get_connection()->fetchAll($sql, $params, $types);
return $this->mapRecordsFromResultSet($result);
}
/** /**
* @param string $uuid * @param string $uuid
* @return \record_adapter[] * @return \record_adapter[]
@@ -99,6 +127,39 @@ class LegacyRecordRepository implements RecordRepository
return $this->mapRecordsFromResultSet($result); return $this->mapRecordsFromResultSet($result);
} }
/**
* @param string $uuid
* @param array $excludedCollIds
* @return \record_adapter[]
*/
public function findByUuidWithExcludedCollIds($uuid, $excludedCollIds = [])
{
static $sql;
$params = ['uuid' => $uuid];
$types = [];
if (!$sql) {
$qb = $this->createSelectBuilder()
->where('uuid = :uuid');
$params['uuid'] = $uuid;
if (!empty($excludedCollIds)) {
$qb->andWhere($qb->expr()->notIn('coll_id', ':coll_id'));
$params['coll_id'] = $excludedCollIds;
$types[':coll_id'] = Connection::PARAM_INT_ARRAY;
}
$sql = $qb->getSQL();
}
$result = $this->databox->get_connection()->fetchAll($sql, $params, $types);
return $this->mapRecordsFromResultSet($result);
}
public function findByRecordIds(array $recordIds) public function findByRecordIds(array $recordIds)
{ {
static $sql; static $sql;

View File

@@ -26,12 +26,26 @@ interface RecordRepository
*/ */
public function findBySha256($sha256); public function findBySha256($sha256);
/**
* @param string $sha256
* @param array $excludedCollIds
* @return \record_adapter[]
*/
public function findBySha256WithExcludedCollIds($sha256, $excludedCollIds = []);
/** /**
* @param string $uuid * @param string $uuid
* @return \record_adapter[] * @return \record_adapter[]
*/ */
public function findByUuid($uuid); public function findByUuid($uuid);
/**
* @param string $uuid
* @param array $excludedCollIds
* @return \record_adapter[]
*/
public function findByUuidWithExcludedCollIds($uuid, $excludedCollIds = []);
/** /**
* @param array $recordIds * @param array $recordIds
* @return \record_adapter[] * @return \record_adapter[]

View File

@@ -1673,6 +1673,43 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
return $records; return $records;
} }
public static function getRecordsByOriginalnameWithExcludedCollIds(databox $databox, $original_name, $caseSensitive = false, $offset_start = 0, $how_many = 10, $excludedCollIds = [])
{
$offset_start = max(0, (int)$offset_start);
$how_many = max(1, (int)$how_many);
$collate = $caseSensitive ? 'utf8_bin' : 'utf8_unicode_ci';
$qb = $databox->get_connection()->createQueryBuilder()
->select('record_id')
->from('record')
->where('originalname = :original_name COLLATE :collate')
;
$params = ['original_name' => $original_name, 'collate' => $collate];
$types = [];
if (!empty($excludedCollIds)) {
$qb->andWhere($qb->expr()->notIn('coll_id', ':coll_id'));
$params['coll_id'] = $excludedCollIds;
$types[':coll_id'] = Connection::PARAM_INT_ARRAY;
}
$sql = $qb->setFirstResult($offset_start)
->setMaxResults($how_many)
->getSQL()
;
$rs = $databox->get_connection()->fetchAll($sql, $params, $types);
$records = [];
foreach ($rs as $row) {
$records[] = $databox->get_record($row['record_id']);
}
return $records;
}
/** /**
* @return set_selection|record_adapter[] * @return set_selection|record_adapter[]
* @throws Exception * @throws Exception