-
Notifications
You must be signed in to change notification settings - Fork 2
/
dfg_3dviewer.module
executable file
·333 lines (321 loc) · 12.4 KB
/
dfg_3dviewer.module
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
257
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Archiver\Zip;
use Drupal\Core\Archiver\ArchiverException;
const ENTITY_BUNDLE="bd3d7baa74856d141bcff7b4193fa128";
const VIEWER_FILE_UPLOAD="fbf95bddee5160d515b982b3fd2e05f7";
const IMAGE_GENERATION="f605dc6b727a1099b9e52b3ccbdf5673";
const VIEWER_FILE_NAME="faa602a0be629324806aef22892cdbe5";
const FIELD_DF="field_df";
const MAIN_URL="https://3d-repository.hs-mainz.de";
/**
* Implements hook_help().
*/
function dfg_3dviewer_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.wisski_iip_image':
return '<p>' . t('This is the WissKI module for the integration ' .
'of IIP (https://iipimage.sourceforge.io/).') . '</p>';
}
}
function in_arrayi($needle, $haystack)
{
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
function array_searchi($needle, $haystack)
{
return array_search(strtolower($needle), array_map('strtolower', $haystack));
}
function url_exists ($url) {
return str_contains(get_headers($url)[0], "200 OK");
}
function semi_automatic_path ($prefix, $filename, $filenameArchive, &$autoPath, $extension) {
$allowedFormats = array('abc', 'obj', 'fbx', 'ply', 'dae', 'ifc', 'stl', 'xyz', 'pcd', 'json', '3ds', 'blend', 'zip', 'rar', 'xz', 'gz', 'gml');
$SPATH=\Drupal::service('file_system')->realpath(\Drupal::service('module_handler')->getModule('dfg_3dviewer')->getPath());
$SPATH = str_replace('/modules/dfg_3dviewer', '', $SPATH);
$prefix = $SPATH . $prefix;
$autoPath = '';
if ($filenameArchive === '') $filenameArchive = $filename;
if (in_arrayi($extension, $allowedFormats)) {
if(!file_exists($prefix . "/gltf/" . $filename . ".glb")) {
if(!file_exists($prefix . "/gltf/" . $filenameArchive . ".glb")) {
foreach ($allowedFormats as $value) {
$autoPath = $prefix . "/" . $filenameArchive . "." . $value;
if(!file_exists($autoPath)) {
$autoPath = '';
}
else {
return;
}
}
}
else
{
$autoPath = $prefix . "/gltf/" . $filenameArchive . ".glb";
}
}
else
{
$autoPath = $prefix . "/gltf/" . $filename . ".glb";
}
if ($autoPath === '') {
foreach ($allowedFormats as $value) {
$autoPath = $prefix . "/" . $filenameArchive . "." . $value;
if(!file_exists($autoPath)) {
$autoPath = '';
}
else {
return;
}
}
}
}
else {
$autoPath = $prefix . "/" . $filenameArchive . "." . $extension;
if(!file_exists($autoPath)) {
$autoPath = '';
}
else {
return;
}
}
if ($autoPath === '') {
$autoPath = $prefix . "/" . $filenameArchive . "." . $extension;
if(!file_exists($autoPath)) {
$autoPath = '';
}
else {
return;
}
}
}
/**
* Implements hook_file_validate().
*/
function dfg_3dviewer_hook_file_validate(Drupal\file\FileInterface $file) {
$errors = [];
$filename = $file->getFilename();
if (!$filename) {
$errors[] = t("The file's name is empty. Give a name to the file.");
}
if (strlen($filename) > 255) {
$errors[] = t("The file's name exceeds the 255 characters limit. Rename the file and try again.");
}
if (preg_match('/\s/', $filename)) {
$errors[] = t("Whitespaces are not allowed in filenames");
}
return $errors;
}
/**
* Implements hook_file_validate().
*/
function dfg_3dviewer_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
$DEFAULT_CONFIG = \Drupal::config('dfg_3dviewer.settings');
$SETTINGS = Array();
$SETTINGS['entity_bundle']=ENTITY_BUNDLE;
$SETTINGS['viewer_file_upload']=VIEWER_FILE_UPLOAD;
$SETTINGS['image_generation']=IMAGE_GENERATION;
$SETTINGS['viewer_file_name']=VIEWER_FILE_NAME;
$SETTINGS['field_df']=FIELD_DF;
$SETTINGS['main_url']=MAIN_URL;
if (isset($DEFAULT_CONFIG)) {
$SETTINGS['entity_bundle']=$DEFAULT_CONFIG->get('entity_bundle');
$SETTINGS['viewer_file_upload']=$DEFAULT_CONFIG->get('viewer_file_upload');
$SETTINGS['image_generation']=$DEFAULT_CONFIG->get('image_generation');
$SETTINGS['viewer_file_name']=$DEFAULT_CONFIG->get('viewer_file_name');
$SETTINGS['field_df']=$DEFAULT_CONFIG->get('field_df');
$SETTINGS['main_url']=$DEFAULT_CONFIG->get('main_url');
}
//\Drupal::messenger()->addMessage(serialize($entity));
$SPATH = \Drupal::service('file_system')->realpath(\Drupal::service('module_handler')->getModule('dfg_3dviewer')->getPath());
$XPATH = dirname(dirname($SPATH));
$archives = array("zip", "rar", "tar", "xz", "gz");
$base = \Drupal::request()->getSchemeAndHttpHost();
$output = null;
$retval = null;
$autoPath = '';
if ($entity->bundle() == "file") {
$file = $entity->toArray();
$realpath = \Drupal::service('file_system')->realpath($file['uri'][0]['value']);
$parts = pathinfo($realpath);
$extension = strtoupper($parts['extension']);
$newFile = $parts['dirname'] . "/gltf/" . $parts['filename'] . ".glb";
$newDir = $parts['dirname'] . "/" . $parts['filename'] . "_" . $extension . "/gltf/";
$isArchive = false;
if (!file_exists($newFile) && !is_dir($newDir) && !file_exists($newFile."off")) {
if (!in_arrayi($extension, $archives)) {
$output = exec ($SPATH . '/scripts/convert.sh -c true -l 3 -b true -i "' . "$realpath" . '" -f true 2>&1', $output, $retval);
}
elseif (in_arrayi($extension, $archives)) {
try {
$isArchive = true;
$extractPath = $parts['dirname'] . "/" . $parts['filename'] . "_" . $extension . "/";
if ($extension == 'ZIP') {
$zip = new Zip($realpath);
$zip->extract($extractPath);
$zip->remove($realpath);
$archiveFiles = scandir ($extractPath);
foreach ($archiveFiles as $zipFile) {
$partsZip = pathinfo($zipFile);
switch (strtolower($partsZip["extension"])) {
case "obj":
case "fbx":
case "ply":
case "dae":
case "abc":
case "blend":
case "stl":
case "wrl":
case "x3d":
case "ifc":
case "glb":
case "gltf":
case "gml":
exec ($SPATH . '/scripts/convert.sh -c true -l 3 -b true -i "' . $extractPath . $zipFile . '" -o "' . $extractPath . '" -f true -a false', $output, $retval);
break;
}
}
}
elseif ($extension == 'RAR') {
$output = shell_exec($SPATH . '/scripts/uncompress.sh -t rar -i "' . $realpath . '" -o "' . $extractPath . '" -n "' . $parts['filename'] . '"');
}
elseif ($extension == 'TAR' || $extension == 'GZ' ) {
$output = shell_exec($SPATH . '/scripts/uncompress.sh -t tar -i "' . $realpath . '" -o "' . $extractPath . '" -n "' . $parts['filename'] . '"');
}
elseif ($extension == 'XZ') {
$output = shell_exec($SPATH . '/scripts/uncompress.sh -t xz -i "' . $realpath . '" -o "' . $extractPath . '" -n "' . $parts['filename'] . '"');
}
}
catch (ArchiverException $exception) {
\Drupal::messenger()->addMessage("Exception while handling archive: " . serialize($exception), 'status');
}
}
else {
\Drupal::messenger()->addMessage($extension . " is not supported yet", 'warning');
}
#semi-automatic path change
//$entity->set($SETTINGS['viewer_file_name'], $autoPath);
if ($retval === null) {
\Drupal::messenger()->addMessage($output, 'status');
}
else if ($retval === 1) {
\Drupal::messenger()->addMessage($output, 'warning');
}
else if ($retval === 2) {
\Drupal::messenger()->addMessage($output, 'error');
}
}
}
if($entity->bundle() == $SETTINGS['entity_bundle']) {
#\Drupal::messenger()->addMessage(serialize($entity->eid->value));
//$file_info = $entity->label->getValue();
$file_info = $entity->{$SETTINGS['viewer_file_upload']}->getValue();
$file_id = $file_info[0]['target_id'];
$entity_id = $entity->eid->value;
$file = \Drupal\file\Entity\File::load($file_id);
$imgSuffix = "_side45.png";
clearstatcache();
if (!is_null($file)) {
$fileName = $file->getFilename();
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$fileBase = str_replace(".$extension", "", $fileName);
$isArchive = false;
if (in_arrayi($extension, $archives)) {
$isArchive = true;
}
$file_url = \Drupal::service('file_url_generator')->generate($file->getFileUri());
$viewBase = "";
$path = "";
#is archive
if (in_arrayi($extension, $archives)) {
$path = str_replace($fileName, "", $file_url->toString());
$subPath = substr($path, 0, strrpos( $path, '/') );
$viewBase = $base . $subPath . "/" . $fileBase . "_" . strtoupper($extension) . "/views/" . $fileBase;
$fileBaseArchive = preg_replace('/_[0-9]+$/', '', $fileBase, 1);
if (!url_exists($viewBase . $imgSuffix)) {
$subPath = substr($path, 0, strrpos( $path, '/') );
$viewBase = $base . $subPath . "/" . $fileBase . "_" . strtoupper($extension) . "/views/" . $fileBaseArchive;
}
}
else
{
$path = str_replace($fileName, "views/" . $fileName, $file_url->toString());
$viewBase = $base . $path;
if (!url_exists($viewBase . $imgSuffix)) {
$subPath = substr($path, 0, strrpos( $path, '/') );
$viewBase = $base . $subPath . "/" . $fileBase;
if (!url_exists($viewBase . $imgSuffix)) {
$viewBase = $base . $subPath . "/views/" . $fileBase;
}
}
}
$viewBase = str_replace(' ', '%20', $viewBase);
clearstatcache();
$imagesTemp = array($viewBase . $imgSuffix, $viewBase . "_side0.png", $viewBase . "_side90.png", $viewBase . "_side135.png", $viewBase . "_side180.png", $viewBase . "_side225.png", $viewBase . "_side270.png", $viewBase . "_side315.png", $viewBase . "_top.png");
$imagesPaths = [];
foreach ($imagesTemp as $value) {
if (url_exists($value) && getimagesize($value) !== false) {
array_push($imagesPaths, $value);
}
}
if (count($imagesPaths) > 0) {
\Drupal::messenger()->addMessage("Renderings were added to the entity");
$entity->set($SETTINGS['image_generation'], $imagesPaths);
}
$fpath = $file_url->toString();
$fpath = str_replace($fileName, "", $fpath);
$subPath = substr($fpath, 0, strrpos( $fpath, '/') );
//$fileBase = $parts['filename'];
$fileBaseArchive = preg_replace('/_[0-9]+$/', '', $fileBase, 1);
if (in_arrayi($extension, $archives)) {
semi_automatic_path ($subPath . '/' . $fileBase . "_" . strtoupper($extension), $fileBase, $fileBaseArchive, $autoPath, $extension);
}
else {
semi_automatic_path ($subPath, $fileBase, '', $autoPath, $extension);
}
$origPath = $subPath . "/gltf/" . $fileBase . ".glb";
$origArchPath = $subPath . '/' . $fileBase . "_" . strtoupper($extension) . "/gltf/" . $fileBase . ".glb";
if ($autoPath != "" && ((!$isArchive && strcmp($origPath, $autoPath) !== 0) || ($isArchive && strcmp($origArchPath, $autoPath) !== 0))) {
// \Drupal::messenger()->addMessage("GLTF or file with given name (possible archive/filename mismatch) representation not found, semi-automatic path applied\n", 'warning');
}
elseif ($autoPath)
{
//Check last time if it really exists
if ($isArchive) {
if (file_exists($origArchPath)) {
$autoPath = $origArchPath;
}
else {
$autoPath = $origPath;
#\Drupal::messenger()->addMessage(serialize($subPath . '/' . $fileBase . "_" . strtoupper($extension) . '/' . $fileBase));
}
}
else {
$autoPath = $origPath;
}
}
$autoPath = str_replace($XPATH, $SETTINGS['main_url'], $autoPath);
//\Drupal::messenger()->addMessage($fileBase, 'status');
/*\Drupal::messenger()->addMessage($file_url->toString());
\Drupal::messenger()->addMessage($fileName);
\Drupal::messenger()->addMessage($path);*/
$entity->set($SETTINGS['viewer_file_name'], $autoPath);
}
require ('php/build_xml.php');
build_xml ($entity_id);
#$entity->set('$SETTINGS['field_df']', 'https://3dtest.dfg-viewer.de/viewer?tx_dlf[id]=https://3d-repository.hs-mainz.de/sites/default/files/xml_structure/' . $entity_id . '.xml&no_cache=1');
$xml_struc = 'public://xml_structure/' . $entity_id . '.xml&no_cache=1';
$xml_wrapper = \Drupal::service('stream_wrapper_manager')->getViaUri($xml_struc);
$xml_uri = $xml_wrapper->getExternalURL();
global $base_url;
$uri = $base_url . "/viewer?tx_dlf[id]=" . $xml_uri . "&no_cache=1&modelviewer=1";
$entity->set($SETTINGS['field_df'], $uri);
}
#$file = $entity->fbda64e9ba217e59e5fc5f99244f4821;
//$compression = $entity->field_3d_compression_level;
//\Drupal::messenger()->addMessage(serialize($entity), 'status');
}