Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| MediaModerationFileProcessor | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fileHasAllowedMediaType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fileHasAllowedMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fileCanBeRendered | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| canScanFile | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| insertFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace MediaWiki\Extension\MediaModeration\Services; |
| 5 | |
| 6 | use Error; |
| 7 | use MediaWiki\FileRepo\File\ArchivedFile; |
| 8 | use MediaWiki\FileRepo\File\File; |
| 9 | use MediaWiki\Media\MediaHandlerFactory; |
| 10 | use Psr\Log\LoggerInterface; |
| 11 | |
| 12 | class MediaModerationFileProcessor { |
| 13 | /** @var string[] An array of mime types that are supported by PhotoDNA. */ |
| 14 | public const ALLOWED_MIME_TYPES = [ |
| 15 | 'image/gif', |
| 16 | 'image/jpeg', |
| 17 | 'image/png', |
| 18 | 'image/bmp', |
| 19 | 'image/tiff', |
| 20 | ]; |
| 21 | |
| 22 | /** @var array An array of mediatypes that can be properly converted to an accepted mime type for PhotoDNA. */ |
| 23 | private const ALLOWED_MEDIA_TYPES = [ |
| 24 | MEDIATYPE_BITMAP, |
| 25 | MEDIATYPE_DRAWING, |
| 26 | ]; |
| 27 | |
| 28 | public function __construct( |
| 29 | private readonly MediaModerationDatabaseManager $mediaModerationDatabaseManager, |
| 30 | private readonly MediaHandlerFactory $mediaHandlerFactory, |
| 31 | private readonly LoggerInterface $logger, |
| 32 | ) { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns whether a file has an allowed media type. |
| 37 | * This check is needed because some files may be |
| 38 | * renderable but not in a supported format (T352234). |
| 39 | * |
| 40 | * @param File|ArchivedFile $file |
| 41 | * @return bool |
| 42 | */ |
| 43 | private function fileHasAllowedMediaType( $file ): bool { |
| 44 | return in_array( $file->getMediaType(), self::ALLOWED_MEDIA_TYPES, true ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns whether a file has an allowed mime type |
| 49 | * and therefore could be sent directly to PhotoDNA |
| 50 | * without having to convert the file type. |
| 51 | * |
| 52 | * @param File|ArchivedFile $file |
| 53 | * @return bool |
| 54 | */ |
| 55 | private function fileHasAllowedMimeType( $file ): bool { |
| 56 | return in_array( $file->getMimeType(), self::ALLOWED_MIME_TYPES, true ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns whether a file can be likely rendered, |
| 61 | * which is the result of File::canRender. The behaviour |
| 62 | * is similar for ArchivedFile objects. |
| 63 | * |
| 64 | * @param File|ArchivedFile $file |
| 65 | * @return bool |
| 66 | */ |
| 67 | private function fileCanBeRendered( $file ): bool { |
| 68 | if ( $file instanceof File ) { |
| 69 | return $file->canRender(); |
| 70 | } |
| 71 | $mediaHandler = $this->mediaHandlerFactory->getHandler( $file->getMimeType() ); |
| 72 | if ( $mediaHandler ) { |
| 73 | try { |
| 74 | // This suppression and passing of ArchivedFile to a MediaHandler method |
| 75 | // which expects a File object is in the same way as ArchivedFile::pageCount. |
| 76 | // TODO: Fix me if ArchivedFile ever extends File. |
| 77 | // @phan-suppress-next-line PhanTypeMismatchArgument |
| 78 | $fileCanBeRendered = $mediaHandler->canRender( $file ); |
| 79 | } catch ( Error $e ) { |
| 80 | // All errors need to be caught, because a method not existing |
| 81 | // will raise the generic in-built Error exception. |
| 82 | // If the MediaHandler raises an exception for any reason the |
| 83 | // result of this method will be false, and no further actions |
| 84 | // would be taken for this file. |
| 85 | $this->logger->error( |
| 86 | 'Call to MediaHandler::canRender with an ArchivedFile did not work ' . |
| 87 | 'for handler {handlerclass}', |
| 88 | [ |
| 89 | 'handlerclass' => get_class( $mediaHandler ), |
| 90 | 'exception' => $e |
| 91 | ] |
| 92 | ); |
| 93 | return false; |
| 94 | } |
| 95 | // The ArchivedFile::exists check is done to make this similar to File::canRender. |
| 96 | return $fileCanBeRendered && $file->exists(); |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns whether a file can be scanned by PhotoDNA. |
| 103 | * |
| 104 | * This currently is limited to whether the file has |
| 105 | * a MIME type that is supported or can be rendered |
| 106 | * into a thumbnail of a supported MIME type. |
| 107 | * |
| 108 | * This is to be used to determine whether an image |
| 109 | * should be added to the scan table and should be |
| 110 | * used before attempting to scan the file. |
| 111 | * |
| 112 | * @param File|ArchivedFile $file |
| 113 | * @return bool |
| 114 | */ |
| 115 | public function canScanFile( $file ): bool { |
| 116 | $canScanFile = $this->fileHasAllowedMediaType( $file ) && |
| 117 | ( |
| 118 | $this->fileHasAllowedMimeType( $file ) || |
| 119 | $this->fileCanBeRendered( $file ) |
| 120 | ); |
| 121 | if ( !$canScanFile ) { |
| 122 | $this->logger->debug( |
| 123 | 'File with SHA-1 {sha1} cannot be scanned by PhotoDNA', |
| 124 | [ 'sha1' => $file->getSha1() ] |
| 125 | ); |
| 126 | } |
| 127 | return $canScanFile; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Should be called when a file has been created in the |
| 132 | * 'image' table, or when backfilling entries from the |
| 133 | * image, oldimage, and filearchive tables. |
| 134 | * |
| 135 | * @param File|ArchivedFile $file |
| 136 | * @return void |
| 137 | */ |
| 138 | public function insertFile( $file ): void { |
| 139 | if ( $this->canScanFile( $file ) ) { |
| 140 | $this->mediaModerationDatabaseManager->insertFileToScanTable( $file ); |
| 141 | } |
| 142 | } |
| 143 | } |