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