Maslosoft Mangan API
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Model;
use finfo;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\EmbeddedDocument;
use Maslosoft\Mangan\EntityManager;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Exceptions\FileNotFoundException;
use Maslosoft\Mangan\Helpers\IdHelper;
use Maslosoft\Mangan\Interfaces\FileInterface;
use Maslosoft\Mangan\Mangan;
use MongoDB;
use MongoGridFSFile;
use MongoId;
/**
* Class for storing embedded files
* @since 2.0.1
* @author Piotr
*/
class File extends EmbeddedDocument
{
const TmpPrefix = 'tmp';
/**
* NOTE: This is also in gridfs, here is added to avoid querying gridfs just to get filename
* @var string
*/
public $filename = '';
/**
* Size in bytes NOTE: @see $filename
* @var int
*/
public $size = 0;
/**
* Root document class
* @var string Class name
*/
public $rootClass = '';
/**
* Root document ID
* @var MongoId
*/
public $rootId = '';
/**
* NOTE: Same not as for $filename field
* @var string
*/
public $contentType = '';
/**
* Mongo DB instance
* @var MongoDB
*/
private $_db = null;
public function __construct($scenario = 'insert', $lang = '')
{
parent::__construct($scenario, $lang);
$this->_id = new MongoId;
$mangan = Mangan::fromModel($this);
$this->_db = $mangan->getDbInstance();
}
public function setOwner(AnnotatedInterface $owner = null)
{
parent::setOwner($owner);
$root = $this->getRoot();
$onAfterDelete = function()
{
$this->_onAfterDelete();
};
$onAfterDelete->bindTo($this);
Event::on($root, EntityManager::EventAfterDelete, $onAfterDelete);
}
public function getId()
{
if (!$this->_id instanceof MongoId)
{
$this->_id = new MongoId($this->_id);
}
return $this->_id;
}
/**
* Get file from mongo grid
* @return MongoGridFSFile
*/
public function get()
{
return $this->_get();
}
/**
* Send file to browser
*/
public function send()
{
$this->_send($this->_get());
}
/**
* Stream file to browser
*/
public function stream()
{
$this->_stream($this->_get());
}
/**
* Set file data
* @param FileInterface|string $file
*/
public function set($file)
{
if ($file instanceof FileInterface)
{
$tempName = $file->getTempName();
$fileName = $file->getFileName();
}
else
{
$tempName = $file;
$fileName = $file;
}
$this->_set($tempName, $fileName);
}
/**
* Get file with optional criteria params
* @param mixed[] $params
* @return MongoGridFSFile
*/
protected function _get($params = [])
{
$criteria = [
'parentId' => $this->_id,
'isTemp' => false
];
$conditions = array_merge($criteria, $params);
if (!$conditions['isTemp'])
{
return $this->_db->getGridFS()->findOne($conditions);
}
else
{
return $this->_db->getGridFS(self::TmpPrefix)->findOne($conditions);
}
}
/**
* Send file to the browser
* @param MongoGridFSFile $file
*/
protected function _send(MongoGridFSFile $file = null)
{
if (null === $file)
{
throw new FileNotFoundException('File not found');
}
$meta = (object) $file->file;
header(sprintf('Content-Length: %d', $file->getSize()));
header(sprintf('Content-Type: %s', $meta->contentType));
header(sprintf('ETag: %s', $meta->md5));
header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \G\M\T', $meta->uploadDate->sec)));
header(sprintf('Content-Disposition: filename="%s"', basename($meta->filename)));
// Cache it
header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
echo $file->getBytes();
// Exit application after sending file
exit;
}
protected function _stream(MongoGridFSFile $file)
{
$meta = (object) $file->file;
if (ob_get_length())
{
ob_end_clean();
}
header(sprintf('Content-Length: %d', $file->getSize()));
header(sprintf('Content-Type: %s', $meta->contentType));
header(sprintf('ETag: %s', $meta->md5));
header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \G\M\T', $meta->uploadDate->sec)));
header(sprintf('Content-Disposition: filename="%s"', basename($meta->filename)));
// Cache it
header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
$stream = $file->getResource();
while (!feof($stream))
{
echo fread($stream, 8192);
if (ob_get_length())
{
ob_flush();
}
flush();
}
// Exit application after stream completed
exit;
}
/**
* Set file with optional criteria params
* @param string $tempName
* @param string $fileName
* @param mixed[] $params
*/
protected function _set($tempName, $fileName, $params = [])
{
$info = new finfo(FILEINFO_MIME);
$mime = $info->file($tempName);
/**
* TODO Check if root data is saved corectly
*/
if (!$this->getRoot()->_id instanceof MongoId)
{
// Assume string id
if (IdHelper::isId($this->getRoot()->_id))
{
// Convert existing string id to MongoId
$this->getRoot()->_id = new MongoId((string) $this->getRoot()->_id);
}
else
{
// Set new id now
$this->getRoot()->_id = new MongoId;
}
}
$rootId = $this->getRoot()->_id;
$data = [
'_id' => new MongoId(),
'parentId' => $this->_id,
'rootClass' => get_class($this->getRoot()),
'rootId' => $rootId,
'filename' => $fileName,
'contentType' => $mime,
'isTemp' => false
];
$this->filename = $fileName;
$this->contentType = $mime;
$this->size = filesize($tempName);
$params = array_merge($data, $params);
// Replace existing file, remove previous
if (!$params['isTemp'])
{
$oldFiles = [
'parentId' => $this->_id
];
$this->_db->getGridFS()->remove($oldFiles);
$this->_db->getGridFS(self::TmpPrefix)->remove($oldFiles);
}
// Store new file
if (!$params['isTemp'])
{
// In main storage
$this->_db->getGridFS()->put($tempName, $params);
}
else
{
// In temporary gfs
$this->_db->getGridFS(self::TmpPrefix)->put($tempName, $params);
}
}
/**
* This is fired after delete to remove chunks from gridfs
*/
protected function _onAfterDelete()
{
$criteria = [
'parentId' => $this->_id
];
$this->_db->getGridFS()->remove($criteria);
$this->_db->getGridFS(self::TmpPrefix)->remove($criteria);
}
}
API documentation generated by ApiGen