|
| 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 | +} |
0 commit comments