|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\FileException; |
|
8
|
|
|
use Application\Repository\FileRepository; |
|
9
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
10
|
|
|
use Ecodev\Felix\Model\Traits\HasName; |
|
11
|
|
|
use GraphQL\Doctrine\Attribute as API; |
|
12
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
13
|
|
|
use Throwable; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* An uploaded file that is related to a card (pdf/docx/xlsx/etc.). |
|
17
|
|
|
*/ |
|
18
|
|
|
#[ORM\UniqueConstraint(name: 'unique_name', columns: ['filename'])] |
|
19
|
|
|
#[ORM\HasLifecycleCallbacks] |
|
20
|
|
|
#[ORM\Entity(FileRepository::class)] |
|
21
|
|
|
class File extends AbstractModel implements \Ecodev\Felix\Model\File |
|
22
|
|
|
{ |
|
23
|
|
|
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] |
|
24
|
|
|
#[ORM\ManyToOne(targetEntity: Card::class)] |
|
25
|
|
|
private ?Card $card = null; |
|
26
|
|
|
|
|
27
|
|
|
use \Ecodev\Felix\Model\Traits\File { |
|
28
|
|
|
setFile as traitSetFile; |
|
29
|
|
|
} |
|
30
|
|
|
use HasName; |
|
31
|
|
|
|
|
32
|
|
|
public function setCard(Card $card): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->card = $card; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getCard(): Card |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->card; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set the file. |
|
44
|
|
|
*/ |
|
45
|
|
|
#[API\Input(type: '?GraphQL\Upload\UploadType')] |
|
46
|
|
|
public function setFile(UploadedFileInterface $file): void |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$this->traitSetFile($file); |
|
50
|
|
|
|
|
51
|
|
|
// Default to filename as name |
|
52
|
|
|
if (!$this->getName()) { |
|
53
|
|
|
$this->setName($file->getClientFilename()); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} catch (Throwable $e) { |
|
56
|
|
|
throw new FileException($file, $e); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getAcceptedMimeTypes(): array |
|
61
|
|
|
{ |
|
62
|
|
|
// This list should be kept in sync with FilesComponent.accept |
|
63
|
|
|
return [ |
|
64
|
|
|
'application/msword', |
|
65
|
|
|
'application/pdf', |
|
66
|
|
|
'application/x-pdf', |
|
67
|
|
|
'application/vnd.ms-excel', |
|
68
|
|
|
'application/vnd.oasis.opendocument.spreadsheet', |
|
69
|
|
|
'application/vnd.oasis.opendocument.text', |
|
70
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
71
|
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|