-
Notifications
You must be signed in to change notification settings - Fork 3
/
Module.php
256 lines (228 loc) · 9 KB
/
Module.php
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
<?php declare(strict_types=1);
/*
* Archive Repertory
*
* Keeps original names of files and put them in a hierarchical structure.
*
* Copyright Daniel Berthereau 2012-2024
* Copyright BibLibre, 2016
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
* redistribute the software under the terms of the CeCILL license as circulated
* by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify
* and redistribute granted by the license, users are provided only with a
* limited warranty and the software's author, the holder of the economic
* rights, and the successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with
* loading, using, modifying and/or developing or reproducing the software by
* the user in light of its specific status of free software, that may mean that
* it is complicated to manipulate, and that also therefore means that it is
* reserved for developers and experienced professionals having in-depth
* computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling
* the security of their systems and/or data to be ensured and, more generally,
* to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
namespace ArchiveRepertory;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use ArchiveRepertory\Form\ConfigForm;
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Entity\Media;
use Omeka\Module\AbstractModule;
/**
* Archive Repertory.
*
* @copyright Daniel Berthereau, 2012-2024
* @copyright BibLibre, 2016
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.57')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.57'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.post',
[$this, 'afterSaveItem'],
100
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.post',
[$this, 'afterSaveItem'],
100
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.delete.post',
[$this, 'afterDeleteItem'],
100
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.create.post',
[$this, 'afterUploadMedia'],
100
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.update.post',
[$this, 'afterUploadMedia'],
100
);
}
public function getConfigForm(PhpRenderer $renderer)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$data = [];
$defaultSettings = $config['archiverepertory']['config'];
foreach ($defaultSettings as $name => $value) {
$data[$name] = $settings->get($name, $value);
}
$form->init();
$form->setData($data);
$html = $renderer->render('archive-repertory/module/config', [
'form' => $form,
]);
return $html;
}
/**
* Manage folders for attached files of items.
*/
public function afterSaveItem(Event $event): void
{
$item = $event->getParam('response')->getContent();
foreach ($item->getMedia() as $media) {
$this->afterSaveMedia($media);
}
}
/**
* Manage folders for media.
*/
public function afterUploadMedia(Event $event): void
{
$media = $event->getParam('response')->getContent();
$this->afterSaveMedia($media);
}
/**
* Set medias at the right place.
*
* @param Media $media
*/
protected function afterSaveMedia(Media $media): void
{
/**
* @var \ArchiveRepertory\File\FileManager $fileManager
* @var \ArchiveRepertory\File\FileWriter $fileWriter
* @var \Omeka\Mvc\Controller\Plugin\Messenger $messenger
*/
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$config = $services->get('Config');
$fileManager = $services->get('ArchiveRepertory\FileManager');
$fileWriter = $services->get('ArchiveRepertory\FileWriter');
$ingesters = $config['archiverepertory']['ingesters'];
$ingester = $media->getIngester();
if (!isset($ingesters[$ingester])) {
return;
}
// Check if the file should be moved (so change its storage id).
$currentStorageId = $media->getStorageId();
$newStorageId = $fileManager->getStorageId($media);
if ($currentStorageId == $newStorageId) {
return;
}
$extension = $media->getExtension();
$newFilename = $extension ? $newStorageId . '.' . $extension : $newStorageId;
// Check if the original file exists, else this is an undetected
// error during the convert process.
$path = $fileManager->getFullArchivePath('original');
$filepath = $fileManager->concatWithSeparator($path, $media->getFilename());
if (!$fileWriter->fileExists($filepath)) {
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$messenger->addError(new PsrMessage(
'This file is not present in the original directory: {filepath}. There was an undetected error before storage, probably during the convert process.', // @translate
['filepath' => $filepath]
));
return;
}
$result = $fileManager->moveFilesInArchiveFolders(
$media->getFilename(),
$newFilename
);
if (!$result) {
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$messenger->addError(new PsrMessage(
'Cannot move files inside archive directory.' // @translate
));
return;
}
// Update file in Omeka database immediately for each file.
// Because this is not a file hook, the file is not automatically saved,
// so persist and flush are required now.
$media->setStorageId($newStorageId);
$entityManager->persist($media);
$entityManager->flush();
}
/**
* Remove folders for attached files of items.
*
* The default files are removed with the standard process.
*/
public function afterDeleteItem(Event $event): void
{
/** @var \ArchiveRepertory\File\FileManager $fileManager */
$services = $this->getServiceLocator();
$config = $services->get('Config');
$fileManager = $services->get('ArchiveRepertory\FileManager');
$item = $event->getParam('response')->getContent();
$ingesters = $config['archiverepertory']['ingesters'];
// Check if a folder was added without checking settings, because they
// could change.
foreach ($item->getMedia() as $media) {
$ingester = $media->getIngester();
if (!isset($ingesters[$ingester])) {
continue;
}
// Check if there is a directory to remove. Note: only the "/" is
// used during the saving.
$filename = (string) $media->getFilename();
if (strpos($filename, '/') === false) {
continue;
}
$storageDir = dirname($filename);
$fileManager->removeArchiveFolders($storageDir);
// Whatever the result, continue the other medias.
}
}
}