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
|
<?php
namespace Drupal\file;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
/**
* Provides a file entity repository.
*/
class FileRepository implements FileRepositoryInterface {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The file usage service.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* FileRepository constructor.
*
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The file system.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $streamWrapperManager
* The stream wrapper manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
* @param \Drupal\file\FileUsage\FileUsageInterface $fileUsage
* The file usage service.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
*/
public function __construct(FileSystemInterface $fileSystem, StreamWrapperManagerInterface $streamWrapperManager, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, FileUsageInterface $fileUsage, AccountInterface $currentUser) {
$this->fileSystem = $fileSystem;
$this->streamWrapperManager = $streamWrapperManager;
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->fileUsage = $fileUsage;
$this->currentUser = $currentUser;
}
/**
* {@inheritdoc}
*/
public function writeData(string $data, string $destination, FileExists|int $fileExists = FileExists::Rename): FileInterface {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore staticMethod.deprecated
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
}
if (!$this->streamWrapperManager->isValidUri($destination)) {
throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
}
$uri = $this->fileSystem->saveData($data, $destination, $fileExists);
return $this->createOrUpdate($uri, $destination, $fileExists === FileExists::Rename);
}
/**
* Create a file entity or update if it exists.
*
* @param string $uri
* The file URI.
* @param string $destination
* The destination URI.
* @param bool $rename
* Whether to rename the file.
*
* @return \Drupal\file\Entity\File|\Drupal\file\FileInterface
* The file entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown when there is an error saving the file.
*/
protected function createOrUpdate(string $uri, string $destination, bool $rename): FileInterface {
$file = $this->loadByUri($uri);
if ($file === NULL) {
$file = File::create(['uri' => $uri]);
$file->setOwnerId($this->currentUser->id());
}
if ($rename && is_file($destination)) {
$file->setFilename($this->fileSystem->basename($destination));
}
$file->setPermanent();
$file->save();
return $file;
}
/**
* {@inheritdoc}
*/
public function copy(FileInterface $source, string $destination, FileExists|int $fileExists = FileExists::Rename): FileInterface {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore staticMethod.deprecated
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
}
if (!$this->streamWrapperManager->isValidUri($destination)) {
throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
}
$uri = $this->fileSystem->copy($source->getFileUri(), $destination, $fileExists);
// If we are replacing an existing file, load it.
if ($fileExists === FileExists::Replace && $existing = $this->loadByUri($uri)) {
$file = $existing;
}
else {
$file = $source->createDuplicate();
$file->setFileUri($uri);
// If we are renaming around an existing file (rather than a directory),
// use its basename for the filename.
if ($fileExists === FileExists::Rename && is_file($destination)) {
$file->setFilename($this->fileSystem->basename($destination));
}
else {
$file->setFilename($this->fileSystem->basename($uri));
}
}
$file->save();
// Inform modules that the file has been copied.
$this->moduleHandler->invokeAll('file_copy', [$file, $source]);
return $file;
}
/**
* {@inheritdoc}
*/
public function move(FileInterface $source, string $destination, FileExists|int $fileExists = FileExists::Rename): FileInterface {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore staticMethod.deprecated
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
}
if (!$this->streamWrapperManager->isValidUri($destination)) {
throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
}
$uri = $this->fileSystem->move($source->getFileUri(), $destination, $fileExists);
$delete_source = FALSE;
$file = clone $source;
$file->setFileUri($uri);
// If we are replacing an existing file re-use its database record.
if ($fileExists === FileExists::Replace) {
if ($existing = $this->loadByUri($uri)) {
$delete_source = TRUE;
$file->fid = $existing->id();
$file->uuid = $existing->uuid();
}
}
// If we are renaming around an existing file (rather than a directory),
// use its basename for the filename.
elseif ($fileExists === FileExists::Rename && is_file($destination)) {
$file->setFilename($this->fileSystem->basename($destination));
}
$file->save();
// Inform modules that the file has been moved.
$this->moduleHandler->invokeAll('file_move', [$file, $source]);
// Delete the original if it's not in use elsewhere.
if ($delete_source && !$this->fileUsage->listUsage($source)) {
$source->delete();
}
return $file;
}
/**
* {@inheritdoc}
*/
public function loadByUri(string $uri): ?FileInterface {
$fileStorage = $this->entityTypeManager->getStorage('file');
/** @var \Drupal\file\FileInterface[] $files */
$files = $fileStorage->loadByProperties(['uri' => $uri]);
if (count($files)) {
foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive
// comparison by default, double check that the filename is an exact
// match.
if ($item->getFileUri() === $uri) {
return $item;
}
}
}
return NULL;
}
}
|