Newer
Older
<?php
/*
* Copyright (C) 2015 IRSTEA
* All rights reserved.
*/
namespace Irstea\FileUploadBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gaufrette\Filesystem;
use Gaufrette\StreamMode;
use InvalidArgumentException;
use Rhumsaa\Uuid\Uuid;
/**
* @ORM\Entity(repositoryClass="Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository")
* @ORM\EntityListeners({"Irstea\FileUploadBundle\Listener\UploadedFileListener"})
*/
class UploadedFile
{
Guillaume Perréal
committed
// Taille de bloc utilisé pour les copies
static public $copyBlockSize = 8192;
const ETAT_EN_COURS = 'en-cours';
const ETAT_ORPHELIN = 'orphelin';
const ETAT_NORMAL = 'normal';
const ETAT_CORROMPU = 'corrompu';
const ETAT_MANQUANT = 'manquant';
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
/**
* @ORM\Id
* @ORM\Column(type="guid")
* @var string
*/
private $id;
/**
* @ORM\Column(type="string", length=1024)
* @var string
*/
private $originalFilename;
/**
* @ORM\Column(type="string", length=1024)
* @var string
*/
private $path;
/**
*
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $mimeType;
/**
* @ORM\Column(type="integer", nullable=true)
* @var int
*/
private $size;
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @var string
*/
private $checksum;
/**
* @ORM\Column(type="string", length=10)
* @var string
private $etat = self::ETAT_EN_COURS;
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
/**
* @ORM\Column(type="datetime")
* @var DateTime
*/
private $createdAt;
/**
* @ORM\Column(type="json_array", nullable=true)
* @var array
*/
private $metadata;
/**
* @var Filesystem
*/
private $filesystem;
/**
*
*/
public function __construct()
{
$this->id = Uuid::uuid4()->toString();
$this->path = "orphan/".$this->id;
$this->createdAt = new DateTime('now');
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set originalFilename
*
* @param string $originalFilename
* @return UploadedFile
*/
public function setOriginalFilename($originalFilename)
{
$this->originalFilename = $originalFilename;
return $this;
}
/**
* Get originalFilename
*
* @return string
*/
public function getOriginalFilename()
{
return $this->originalFilename;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set path
*
* @param string $path
* @return UploadedFile
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Set mimeType
*
* @param string $mimeType
* @return UploadedFile
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* Get mimeType
*
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* Set size
*
* @param integer $size
* @return UploadedFile
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size
*
* @return integer
*/
public function getSize()
{
return $this->size;
}
/**
* Set checksum
*
* @param string $checksum
* @return UploadedFile
*/
public function setChecksum($checksum)
{
$this->checksum = $checksum;
return $this;
}
/**
* Get checksum
*
* @return string
*/
public function getChecksum()
{
return $this->checksum;
}
/**
* Set etat
* @param string $etat
* @return UploadedFile
*/
public function setEtat($etat)
if(!in_array($etat, [self::ETAT_CORROMPU, self::ETAT_EN_COURS, self::ETAT_MANQUANT, self::ETAT_NORMAL, self::ETAT_ORPHELIN])) {
throw new InvalidArgumentException(sprintf("Etat invalide: '%s'", (string)$etat));
}
$this->etat = $etat;
return $this;
}
/**
* Get etat
* @return string
public function getEtat()
return $this->etat;
}
/**
* Get createdAt
*
* @return \DateTime
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
307
308
309
310
311
312
313
314
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set metadata
*
* @param array $metadata
* @return UploadedFile
*/
public function setMetadata(array $metadata = null)
{
$this->metadata = $metadata;
return $this;
}
/**
* Get metadata
*
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* @return string
*/
public function __toString()
{
$unit = "";
$size = $this->size ?: 0;
if($size >= 10240) {
$size /= 1024;
$unit = "k";
if($size >= 10240) {
$size /= 1024;
$unit = "m";
}
}
return sprintf("%s (%s, %d%so)", $this->originalFilename, $this->mimeType ?: '?/?', $size, $unit);
}
/**
* @param Filesystem $filesystem
* @return self
*/
public function setFilesystem(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
return $this;
}
public function validate()
{
if (self::ETAT_EN_COURS !== $this->getEtat()) {
return;
}
$fs = $this->filesystem;
$path = $this->getPath();
if (!$fs->has($path)) {
$this->setEtat(self::ETAT_MANQUANT);
return;
}
if ($fs->size($path) !== $this->size || $fs->checksum($path) !== $this->checksum) {
$this->setEtat(self::ETAT_CORROMPU);
return;
}
}
* @return boolean
*/
public function isValid()
{
return $this->getEtat() === self::ETAT_ORPHELIN || $this->getEtat() === self::ETAT_NORMAL;
}
/**
*
* @return boolean
*/
public function isOrphelin()
{
return $this->getEtat() === self::ETAT_ORPHELIN;
Guillaume Perréal
committed
/** Retourne la date de dernière modification dans le filesystem.
Guillaume Perréal
committed
* @return \DateTime
Guillaume Perréal
committed
public function getLastModified()
Guillaume Perréal
committed
return new \DateTime(sprintf('@%d', $this->filesystem->mtime($this->getPath())));
Guillaume Perréal
committed
/** Retourne le contenu du fichier.
*
Guillaume Perréal
committed
* @return string Une chaîne si $asResource vaut faux,
*/
public function getContent()
{
return $this->filesystem->read($this->getPath());
}
Guillaume Perréal
committed
/** Ecrit dans le fichier.
*
* @param string $content
*/
Guillaume Perréal
committed
public function setContent($content)
{
Guillaume Perréal
committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
return $this->filesystem->write($this->getPath(), $content, true);
}
/** Ecrit dans le fichier depuis un descripteur de fichier.
*
* @param resource $source
* @param int $maxlen
* @param int $writeOffset
* @return int
*/
public function copyFrom($source, $maxlen = PHP_INT_MAX, $writeOffset = 0)
{
if($maxlen === 0) {
return 0;
}
$stream = $this->filesystem->createStream($this->getPath(), new StreamMode('cb'));
$stream->seek($writeOffset);
if(false !== $fileHandler = $stream->cast(STREAM_CAST_AS_STREAM)) {
// Utilise stream_copy_to_stream si le Gaufrette\Stream peut nous retourner un filehandle
$copied = $this->stream_copy_to_stream($source, $fileHandler, $maxlen);
} else {
// Sinon fait une copie par blocs (moins performant)
$copied = 0;
while(!$this->feof($source) && $copied <= $maxlen) {
$copied += $stream->write($this->fread($source, min(static::$copyBlockSize, $maxlen - $copied)));
}
}
$stream->close();
return $copied;
}
/** Envoie le contenu du fichier dans un descripteur de fichier.
Guillaume Perréal
committed
*
Guillaume Perréal
committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
* @param int $maxlen
* @param int $readOffset
* @return int
*/
public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0)
{
$actualLength = min($maxlen, $this->getSize() - $readOffset);
if (0 <= $actualLength) {
return 0;
}
$stream = $this->filesystem->createStream($this->getPath(), new StreamMode('rb'));
$stream->seek($readOffset);
if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) {
// Utilise stream_copy_to_stream si le Stream nous renvoie un filehandle
$copied = $this->stream_copy_to_stream($fileHandle, $dest, $actualLength);
} else {
// Sinon, on fait ça à la main par blocs de 8ko
$copied = 0;
while(!$stream->eof() && $copied < $actualLength) {
$copied += $this->fwrite($dest, $stream->read(min(static::$copyBlockSize, $actualLength - $copied)));
}
}
$stream->close();
return $copied;
}
/** Wrapper de stream_copy_to_stream
*
* @param resource $source
* @param resource $dest
* @param int $maxlen
* @param int $offset
* @return int
*/
protected function stream_copy_to_stream($source, $dest, $maxlen = -1, $offset = 0)
{
return stream_copy_to_stream($source, $dest, $maxlen, $offset);
}
/** Wrapper de feof
*
* @param resource $fh
* @return boolean
*/
protected function feof($fh)
{
return feof($fh);
}
/** Wrapper de fread
*
* @param resource $fh
* @param int $maxlen
* @return int|boolean
*/
protected function fread($fh, $maxlen = -1)
{
return fread($fh, $maxlen);
}
/** Wrapper de fwrite
*
* @param resource $fh
* @param int $maxlen
* @return int|boolean
*/
protected function fwrite($fh, $maxlen = -1)
{
return fwrite($fh, $maxlen);
}