Skip to content

Commit 94facc1

Browse files
committed
First commit
0 parents  commit 94facc1

11 files changed

Lines changed: 324 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zip

.travis.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
language: php
2+
sudo: false
3+
4+
php:
5+
- 7.0
6+
- 5.6
7+
- 5.5
8+
- 5.4
9+
- 5.3
10+
11+
env:
12+
global:
13+
- PLUGIN=DatabaseStorage
14+
- KANBOARD_REPO=https://github.com/fguillot/kanboard.git
15+
matrix:
16+
- DB=sqlite
17+
- DB=mysql
18+
- DB=postgres
19+
20+
matrix:
21+
fast_finish: true
22+
23+
install:
24+
- git clone --depth 1 $KANBOARD_REPO
25+
- ln -s $TRAVIS_BUILD_DIR kanboard/plugins/$PLUGIN
26+
27+
before_script:
28+
- cd kanboard
29+
- phpenv config-add tests/php.ini
30+
- composer install
31+
- ls -la plugins/
32+
33+
script:
34+
- phpunit -c tests/units.$DB.xml plugins/$PLUGIN/Test/

DatabaseObjectStorage.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage;
4+
5+
use Kanboard\Core\ObjectStorage\ObjectStorageException;
6+
use Kanboard\Core\ObjectStorage\ObjectStorageInterface;
7+
use PicoDb\Database;
8+
9+
/**
10+
* Database Object Storage
11+
*
12+
* @package DatabaseStorage
13+
* @author Frederic Guillot
14+
*/
15+
class DatabaseObjectStorage implements ObjectStorageInterface
16+
{
17+
const TABLE = 'object_storage';
18+
19+
/**
20+
* @var Database
21+
*/
22+
protected $db;
23+
24+
/**
25+
* Constructor
26+
*
27+
* @access public
28+
*/
29+
public function __construct(Database $db)
30+
{
31+
$this->db = $db;
32+
}
33+
34+
/**
35+
* Fetch object contents
36+
*
37+
* @access public
38+
* @throws ObjectStorageException
39+
* @param string $key
40+
* @return string
41+
*/
42+
public function get($key)
43+
{
44+
$contents = $this->db
45+
->largeObject(self::TABLE)->eq('object_key', $key)
46+
->findOneColumnAsString('object_data');
47+
48+
if ($contents === false) {
49+
throw new ObjectStorageException('Object not found: '.$key);
50+
}
51+
52+
return $contents;
53+
}
54+
55+
/**
56+
* Save object
57+
*
58+
* @access public
59+
* @throws ObjectStorageException
60+
* @param string $key
61+
* @param string $blob
62+
*/
63+
public function put($key, &$blob)
64+
{
65+
$result = $this->db->largeObject(self::TABLE)
66+
->insertFromString('object_data', $blob, array('object_key' => $key));
67+
68+
if ($result === false) {
69+
throw new ObjectStorageException('Unable to save object: '.$key);
70+
}
71+
}
72+
73+
/**
74+
* Output directly object content
75+
*
76+
* @access public
77+
* @param string $key
78+
*/
79+
public function output($key)
80+
{
81+
$fd = $this->db->largeObject(self::TABLE)->eq('object_key', $key)->findOneColumnAsStream('object_data');
82+
83+
if (is_string($fd)) {
84+
echo $fd;
85+
} else {
86+
fpassthru($fd);
87+
}
88+
}
89+
90+
/**
91+
* Move local file to object storage
92+
*
93+
* @access public
94+
* @throws ObjectStorageException
95+
* @param string $src_filename
96+
* @param string $key
97+
* @return boolean
98+
*/
99+
public function moveFile($src_filename, $key)
100+
{
101+
$result = $this->db->largeObject(self::TABLE)
102+
->insertFromFile('object_data', $src_filename, array('object_key' => $key));
103+
104+
if ($result === false) {
105+
throw new ObjectStorageException('Unable to move this file: '.$src_filename);
106+
}
107+
108+
return true;
109+
}
110+
111+
/**
112+
* Move uploaded file to object storage
113+
*
114+
* @access public
115+
* @param string $filename
116+
* @param string $key
117+
* @return boolean
118+
*/
119+
public function moveUploadedFile($filename, $key)
120+
{
121+
return $this->moveFile($filename, $key);
122+
}
123+
124+
/**
125+
* Remove object
126+
*
127+
* @access public
128+
* @param string $key
129+
* @return boolean
130+
*/
131+
public function remove($key)
132+
{
133+
return $this->db->table(self::TABLE)->eq('object_key', $key)->remove();
134+
}
135+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Frédéric Guillot
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
all:
2+
@ echo "Build archive for plugin ${plugin} version=${version}"
3+
@ git archive HEAD --prefix=${plugin}/ --format=zip -o ${plugin}-${version}.zip

Plugin.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage;
4+
5+
use Kanboard\Core\Plugin\Base;
6+
7+
class Plugin extends Base
8+
{
9+
public function initialize()
10+
{
11+
$this->container['objectStorage'] = function() {
12+
return new DatabaseObjectStorage($this->db);
13+
};
14+
}
15+
16+
public function getPluginName()
17+
{
18+
return 'Database Storage';
19+
}
20+
21+
public function getPluginDescription()
22+
{
23+
return 'Use the database to store attachments';
24+
}
25+
26+
public function getPluginAuthor()
27+
{
28+
return 'Frédéric Guillot';
29+
}
30+
31+
public function getPluginVersion()
32+
{
33+
return '1.0.0';
34+
}
35+
36+
public function getPluginHomepage()
37+
{
38+
return 'https://github.com/kanboard/plugin-database-storage';
39+
}
40+
}

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Database Object Storage
2+
=======================
3+
4+
[![Build Status](https://travis-ci.org/kanboard/plugin-database-storage.svg?branch=master)](https://travis-ci.org/kanboard/plugin-database-storage)
5+
6+
This plugin stores uploaded files into the database instead of using the local filesystem.
7+
8+
Author
9+
------
10+
11+
- Frederic Guillot
12+
- License MIT
13+
14+
Requirements
15+
------------
16+
17+
- Postgres is recommended
18+
19+
Notes
20+
-----
21+
22+
- Obviously the database size will increase if you store large files
23+
- Run the command `VACUUM` to free up disk space used by removed files
24+
- With Mysql, you may need to change the value of `max_allowed_packet`, the default is 1MB

Schema/Mysql.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage\Schema;
4+
5+
use PDO;
6+
7+
const VERSION = 1;
8+
9+
function version_1(PDO $pdo)
10+
{
11+
$pdo->exec('CREATE TABLE IF NOT EXISTS `object_storage` (
12+
`object_key` VARCHAR(255) NOT NULL,
13+
`object_data` LONGBLOB NOT NULL,
14+
PRIMARY KEY (`object_key`)
15+
) ENGINE=InnoDB
16+
');
17+
}

Schema/Postgres.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage\Schema;
4+
5+
use PDO;
6+
7+
const VERSION = 1;
8+
9+
function version_1(PDO $pdo)
10+
{
11+
$pdo->exec('CREATE TABLE IF NOT EXISTS object_storage (
12+
"object_key" VARCHAR(255) PRIMARY KEY,
13+
"object_data" BYTEA NOT NULL
14+
)');
15+
}

Schema/Sqlite.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage\Schema;
4+
5+
use PDO;
6+
7+
const VERSION = 1;
8+
9+
function version_1(PDO $pdo)
10+
{
11+
$pdo->exec('CREATE TABLE IF NOT EXISTS object_storage (
12+
"object_key" VARCHAR(255) PRIMARY KEY,
13+
"object_data" BYTEA NOT NULL
14+
)');
15+
}

0 commit comments

Comments
 (0)