|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 Jonathan Bouzekri. All rights reserved. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright 2014 Jonathan Bouzekri <[email protected]> |
|
7
|
|
|
* @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE |
|
8
|
|
|
* @link https://github.com/jbouzekri/FileUploaderBundle |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jb\Bundle\FileUploaderBundle\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Jb\Bundle\FileUploaderBundle\Entity\FileHistory; |
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
16
|
|
|
use Jb\Bundle\FileUploaderBundle\Service\ResolverChain; |
|
17
|
|
|
use Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* FileHistoryManager |
|
21
|
|
|
* |
|
22
|
|
|
* @author jobou |
|
23
|
|
|
*/ |
|
24
|
|
|
class FileHistoryManager implements FileHistoryManagerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $em; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var TokenStorageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $tokenStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\ResolverChain |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $resolvers; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $configuration; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param ObjectManager $em |
|
50
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
51
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\ResolverChain $resolvers |
|
52
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration $configuration |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
ObjectManager $em, |
|
56
|
|
|
TokenStorageInterface $tokenStorage, |
|
57
|
|
|
ResolverChain $resolvers, |
|
58
|
|
|
EndpointConfiguration $configuration |
|
59
|
|
|
) { |
|
60
|
|
|
$this->em = $em; |
|
61
|
|
|
$this->tokenStorage = $tokenStorage; |
|
62
|
|
|
$this->resolvers = $resolvers; |
|
63
|
|
|
$this->configuration = $configuration; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function createAndSave($fileName, $originalName, $type, $userId = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$fileHistory = $this->create($fileName, $originalName, $type, $userId); |
|
72
|
|
|
|
|
73
|
|
|
$this->em->persist($fileHistory); |
|
74
|
|
|
$this->em->flush(); |
|
75
|
|
|
|
|
76
|
|
|
return $fileHistory; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritDoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function create($fileName, $originalName, $type, $userId) |
|
83
|
|
|
{ |
|
84
|
|
|
$fileHistory = new FileHistory(); |
|
85
|
|
|
$fileHistory->setFileName($fileName); |
|
86
|
|
|
$fileHistory->setOriginalName($originalName); |
|
87
|
|
|
$fileHistory->setType($type); |
|
88
|
|
|
if ($userId === null) { |
|
89
|
|
|
$fileHistory->setUserId($this->getAuthUserId()); |
|
90
|
|
|
} else { |
|
91
|
|
|
$fileHistory->setUserId($userId); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $fileHistory; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritDoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function findOneByFileName($fileName) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->em->getRepository('JbFileUploaderBundle:FileHistory')->find($fileName); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritDoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getUrl(FileHistory $fileHistory, $resolverType = 'upload_resolver') |
|
109
|
|
|
{ |
|
110
|
|
|
// Add file path to response |
|
111
|
|
|
$resolver = $this->resolvers->getResolver( |
|
112
|
|
|
$this->configuration->getValue($fileHistory->getType(), $resolverType) |
|
113
|
|
|
); |
|
114
|
|
|
return $resolver->getUrl($fileHistory->getFilename()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get authenticated user id |
|
119
|
|
|
* |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getAuthUserId() |
|
123
|
|
|
{ |
|
124
|
|
|
$token = $this->tokenStorage->getToken(); |
|
125
|
|
|
if (null === $token) { |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$user = $token->getUser(); |
|
130
|
|
|
if (!is_object($user)) { |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $user->getId(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|