Add basic gltf format checking

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2024-11-20 09:08:35 -07:00
parent 7daaf81280
commit f08977da3c
4 changed files with 41 additions and 4 deletions

View File

@@ -20,7 +20,8 @@
"BeforePageDisplay": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onBeforePageDisplay",
"AlternateEditPreview": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onAlternateEditPreview",
"UploadForm:BeforeProcessing": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onUploadFormBeforeProcessing",
"ParserFirstCallInit": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onParserFirstCallInit"
"ParserFirstCallInit": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onParserFirstCallInit",
"UploadVerifyUpload": "MediaWiki\\Extension\\GlModelViewer\\GlModelHooks::onUploadVerifyUpload"
},
"ExtensionMessagesFiles": {
"GlModelHandlerMagic": "GlModelHandler.i18n.magic.php"

View File

@@ -3,5 +3,6 @@
"exif-glmv-textures": "Number of textures",
"exif-glmv-version": "GLTF version",
"exif-glmv-generator": "Model generator",
"exif-glmv-metadata": "GLMV metadata version"
"exif-glmv-metadata": "GLMV metadata version",
"glmv-load-error-format": "File $1 is improperly formatted"
}

View File

@@ -166,8 +166,8 @@ class GlModelHandler extends ImageHandler {
$glMeta = json_decode(fread($gltf,$glMetaLength['int']),true);
fclose($gltf);
$newMeta = array(
'glmv-meshes' => count($glMeta['meshes']),
'glmv-textures' => count($glMeta['textures'])
'glmv-meshes' => count($glMeta['meshes'] ?? []),
'glmv-textures' => count($glMeta['textures'] ?? [])
);
foreach($glMeta['asset'] as $key => $value) {
switch ($key) {

View File

@@ -124,6 +124,41 @@ class GlModelHooks {
$out->addModules('ext.glmv.upl');
}
/**
* Perform upload verification, based on both file properties like MIME type
* (same as UploadVerifyFile) and the information entered by the user (upload
* comment, file page contents etc.).
*
* @param UploadBase $upload Instance of UploadBase, with all info about the upload
* @param User $user User uploading this file
* @param array | null $props File properties, as returned by MWFileProps::getPropsFromPath()
* @param string $comment Upload log comment (also used as edit summary)
* @param string | false $pageText File description page text (only used for new uploads)
* @param array | MessageSpecifier | null &$error Output: If the file upload should be prevented, set this to the reason
* @return bool|void True or no return value to continue or false to abort
*/
public static function onUploadVerifyUpload ($upload, $user, ?array $props, $comment, $pageText, &$error) {
if ($props['mime'] != 'model/gltf-binary') {
return true;
} else {
$path = $upload->getTempPath();
$gltf = fopen($path, 'r');
$glHeader = fread($gltf, 4);
fclose($gltf);
if ($glHeader == 'glTF') {
return true;
} else {
$error = array(
"glmv-load-error-format",
$upload->getTitle()->getRootText()
);
return false;
}
}
}
/**
* MWHook: Called just before the upload data, like wpUploadDescription, are processed, so extensions get a chance to manipulate them.
*