Skip to content

Commit

Permalink
Example App
Browse files Browse the repository at this point in the history
  • Loading branch information
rullzer committed Jan 29, 2016
1 parent d581d1f commit 29b2cec
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
26 changes: 26 additions & 0 deletions apps/files_checksum_store/appinfo/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @author Roeland Jago Douma <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Checksum_Store\AppInfo;

$app = new Application();
$c = $app->getContainer();

\OCA\Files_Checksum_Store\Provider::register($c->getServer()->getChecksumManager(), $c);
40 changes: 40 additions & 0 deletions apps/files_checksum_store/appinfo/application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @author Roeland Jago Douma <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Checksum_Store\AppInfo;

use OCP\AppFramework\App;
use OCA\Files_Checksum_Store\Provider;
use OCP\IContainer;

class Application extends App {

public function __construct (array $urlParams = array()) {
parent::__construct('files_checksum_store', $urlParams);
$container = $this->getContainer();

$container->registerService('Provider', function(IContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new Provider($server->getDatabaseConnection());
});
}

}
52 changes: 52 additions & 0 deletions apps/files_checksum_store/appinfo/database.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<database>
<name>*dbname*</name>
<create>true</create>
<overwrite>false</overwrite>
<charset>utf8</charset>
<table>
<name>*dbprefix*checksum</name>
<declaration>
<field>
<name>file_id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>4</length>
</field>
<field>
<name>checksum_id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>4</length>
</field>
<field>
<name>checksum</name>
<type>text</type>
<default>0</default>
<notnull>true</notnull>
<length>255</length>
</field>

<index>
<name>checksum_fileid_checksumid</name>
<primary>true</primary>
<unique>true</unique>
<field>
<name>file_id</name>
</field>
<field>
<name>checksum_id</name>
</field>
</index>

<index>
<name>checksum_fileid</name>
<field>
<name>file_id</name>
</field>
</index>
</declaration>
</table>
</database>
18 changes: 18 additions & 0 deletions apps/files_checksum_store/appinfo/info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<info>
<id>files_checksum_store</id>
<name>Store Checksums</name>
<description>
On upload clients can store a checksum which will be returned on downloads and propfinds.
</description>
<licence>AGPL</licence>
<author>Roeland Jago Douma</author>
<default_enable/>
<version>0.0.1</version>
<types>
<filesystem/>
</types>
<dependencies>
<owncloud min-version="9.0" max-version="9.0" />
</dependencies>
</info>
102 changes: 102 additions & 0 deletions apps/files_checksum_store/lib/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* @author Roeland Jago Douma <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Checksum_Store;

use OCP\Files\Checksum\IProvider;
use OCP\Files\Checksum\IManager;
use OCP\Files\Checksum\UnsupportedChecksumTypeException;
use OCP\AppFramework\IAppContainer;
use OCP\Files\File;
use OCP\IDBConnection;

class Provider implements IProvider {


/** @var IDBConnection */
private $connection;

public function __construct(\OCP\IDBConnection $connection) {
$this->connection = $connection;
}

public static function register(IManager $cm, IAppContainer $container) {
$cm->registerProvider(function() use ($container) {
return $container->query('Provider');
});
}

public static function supportedChecksums() {
return [
'MD5' => 1,
'SHA-1' => 2,
'ADLER-32' => 3,
];
}

public function addChecksum(File $file, $type, $checksum) {
$type = strtoupper($type);

if (!in_array($type, array_keys($this->supportedChecksums()))) {
throw new UnsupportedChecksumTypeException();
}

$qb = $this->connection->getQueryBuilder();

$qb->insert('checksum')
->setValue('file_id', $qb->createNamedParameter($file->getId()))
->setValue('checksum_id', $qb->createNamedParameter($this->supportedChecksums()[$type]))
->setValue('checksum', $qb->createNamedParameter($checksum))
->execute();

return true;
}

public function getChecksums(File $file) {
$supportedChecksums = array_flip($this->supportedChecksums());

$qb = $this->connection->getQueryBuilder();
$stmt = $qb->select('checksum_id', 'checksum')
->from('checksum')
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId())))
->execute();

$checksums = [];
while($data = $stmt->fetch()) {
$type = (int)$data['checksum_id'];
if (!in_array($type, array_keys($supportedChecksums))) {
continue;
}

$checksums[$supportedChecksums[$type]] = $data['checksum'];
}
$stmt->closeCursor();

return $checksums;
}

public function clearChecksums(File $file) {
$qb = $this->connection->getQueryBuilder();

$qb->delete('checksum')
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId())))
->execute();
}
}

0 comments on commit 29b2cec

Please sign in to comment.