dedupe create_image_from_data
This commit is contained in:
		
							parent
							
								
									394e57103c
								
							
						
					
					
						commit
						674d3fc6fa
					
				| @ -455,9 +455,28 @@ abstract class DataHandlerExtension extends Extension | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     protected function create_image_from_data(string $filename, array $metadata): Image | ||||||
|  |     { | ||||||
|  |         global $config; | ||||||
|  | 
 | ||||||
|  |         $image = new Image(); | ||||||
|  | 
 | ||||||
|  |         $image->filesize = $metadata['size']; | ||||||
|  |         $image->hash = $metadata['hash']; | ||||||
|  |         $image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; | ||||||
|  |         if ($config->get_bool("upload_use_mime")) { | ||||||
|  |             $image->ext = get_extension(getMimeType($filename)); | ||||||
|  |         } else { | ||||||
|  |             $image->ext = (($pos = strpos($metadata['extension'], '?')) !== false) ? substr($metadata['extension'], 0, $pos) : $metadata['extension']; | ||||||
|  |         } | ||||||
|  |         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); | ||||||
|  |         $image->source = $metadata['source']; | ||||||
|  | 
 | ||||||
|  |         return $image; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     abstract protected function supported_ext(string $ext): bool; |     abstract protected function supported_ext(string $ext): bool; | ||||||
|     abstract protected function check_contents(string $tmpname): bool; |     abstract protected function check_contents(string $tmpname): bool; | ||||||
|     abstract protected function create_image_from_data(string $filename, array $metadata); |  | ||||||
|     abstract protected function create_thumb(string $hash, string $type): bool; |     abstract protected function create_thumb(string $hash, string $type): bool; | ||||||
| 
 | 
 | ||||||
|     public static function get_all_supported_exts(): array |     public static function get_all_supported_exts(): array | ||||||
|  | |||||||
| @ -55,26 +55,6 @@ function add_image(string $tmpname, string $filename, string $tags): void | |||||||
|     send_event(new DataUploadEvent($tmpname, $metadata)); |     send_event(new DataUploadEvent($tmpname, $metadata)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** |  | ||||||
|  * Gets an the extension defined in MIME_TYPE_MAP for a file. |  | ||||||
|  * |  | ||||||
|  * @param String $file_path |  | ||||||
|  * @return String The extension that was found. |  | ||||||
|  * @throws UploadException if the mimetype could not be determined, or if an extension for hte mimetype could not be found. |  | ||||||
|  */ |  | ||||||
| function get_extension_from_mime(String $file_path): String |  | ||||||
| { |  | ||||||
|     $mime = mime_content_type($file_path); |  | ||||||
|     if (!empty($mime)) { |  | ||||||
|         $ext = get_extension($mime); |  | ||||||
|         if (!empty($ext)) { |  | ||||||
|             return $ext; |  | ||||||
|         } |  | ||||||
|         throw new UploadException("Could not determine extension for mimetype ".$mime); |  | ||||||
|     } |  | ||||||
|     throw new UploadException("Could not determine file mime type: ".$file_path); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Given a full size pair of dimensions, return a pair scaled down to fit |  * Given a full size pair of dimensions, return a pair scaled down to fit | ||||||
|  * into the configured thumbnail square, with ratio intact. |  * into the configured thumbnail square, with ratio intact. | ||||||
|  | |||||||
| @ -36,22 +36,6 @@ class FlashFileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), $exts); |         return in_array(strtolower($ext), $exts); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata) |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         $image->filesize  = $metadata['size']; |  | ||||||
|         $image->hash	  = $metadata['hash']; |  | ||||||
|         $image->filename  = $metadata['filename']; |  | ||||||
|         $image->ext       = $metadata['extension']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source    = $metadata['source']; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $tmpname): bool |     protected function check_contents(string $tmpname): bool | ||||||
|     { |     { | ||||||
|         $fp = fopen($tmpname, "r"); |         $fp = fopen($tmpname, "r"); | ||||||
|  | |||||||
| @ -32,20 +32,6 @@ class IcoFileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); |         return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata) |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         $image->filesize = $metadata['size']; |  | ||||||
|         $image->hash = $metadata['hash']; |  | ||||||
|         $image->filename = $metadata['filename']; |  | ||||||
|         $image->ext = $metadata['extension']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source = $metadata['source']; |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $file): bool |     protected function check_contents(string $file): bool | ||||||
|     { |     { | ||||||
|         $fp = fopen($file, "r"); |         $fp = fopen($file, "r"); | ||||||
|  | |||||||
| @ -10,6 +10,8 @@ class MP3FileHandler extends DataHandlerExtension | |||||||
|                 $event->image->video = false; |                 $event->image->video = false; | ||||||
|                 $event->image->lossless = false; |                 $event->image->lossless = false; | ||||||
|                 $event->image->image = false; |                 $event->image->image = false; | ||||||
|  |                 $event->image->width = 0; | ||||||
|  |                 $event->image->height = 0; | ||||||
|                 break; |                 break; | ||||||
|         } |         } | ||||||
|         // TODO: Buff out audio format support, length scanning
 |         // TODO: Buff out audio format support, length scanning
 | ||||||
| @ -27,28 +29,6 @@ class MP3FileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), $exts); |         return in_array(strtolower($ext), $exts); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata) |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         //NOTE: No need to set width/height as we don't use it.
 |  | ||||||
|         $image->width  = 1; |  | ||||||
|         $image->height = 1; |  | ||||||
| 
 |  | ||||||
|         $image->filesize  = $metadata['size']; |  | ||||||
|         $image->hash      = $metadata['hash']; |  | ||||||
| 
 |  | ||||||
|         //Filename is renamed to "artist - title.mp3" when the user requests download by using the download attribute & jsmediatags.js
 |  | ||||||
|         $image->filename = $metadata['filename']; |  | ||||||
| 
 |  | ||||||
|         $image->ext       = $metadata['extension']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source    = $metadata['source']; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $tmpname): bool |     protected function check_contents(string $tmpname): bool | ||||||
|     { |     { | ||||||
|         return getMimeType($tmpname) == 'audio/mpeg'; |         return getMimeType($tmpname) == 'audio/mpeg'; | ||||||
|  | |||||||
| @ -46,20 +46,6 @@ class PixelFileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); |         return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata) |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         $image->filesize  = $metadata['size']; |  | ||||||
|         $image->hash      = $metadata['hash']; |  | ||||||
|         $image->filename  = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; |  | ||||||
|         $image->ext       = (($pos = strpos($metadata['extension'], '?')) !== false) ? substr($metadata['extension'], 0, $pos) : $metadata['extension']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source    = $metadata['source']; |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $tmpname): bool |     protected function check_contents(string $tmpname): bool | ||||||
|     { |     { | ||||||
|         $valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP]; |         $valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP]; | ||||||
|  | |||||||
| @ -82,20 +82,6 @@ class SVGFileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), $exts); |         return in_array(strtolower($ext), $exts); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata): Image |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         $image->filesize  = $metadata['size']; |  | ||||||
|         $image->hash      = $metadata['hash']; |  | ||||||
|         $image->filename  = $metadata['filename']; |  | ||||||
|         $image->ext       = $metadata['extension']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source    = $metadata['source']; |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $file): bool |     protected function check_contents(string $file): bool | ||||||
|     { |     { | ||||||
|         $msp = new MiniSVGParser($file); |         $msp = new MiniSVGParser($file); | ||||||
|  | |||||||
| @ -95,35 +95,6 @@ class VideoFileHandler extends DataHandlerExtension | |||||||
|         return in_array(strtolower($ext), self::SUPPORTED_EXT); |         return in_array(strtolower($ext), self::SUPPORTED_EXT); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected function create_image_from_data(string $filename, array $metadata): Image |  | ||||||
|     { |  | ||||||
|         $image = new Image(); |  | ||||||
| 
 |  | ||||||
|         switch (getMimeType($filename)) { |  | ||||||
|             case "video/webm": |  | ||||||
|                 $image->ext = "webm"; |  | ||||||
|                 break; |  | ||||||
|             case "video/mp4": |  | ||||||
|                 $image->ext = "mp4"; |  | ||||||
|                 break; |  | ||||||
|             case "video/ogg": |  | ||||||
|                 $image->ext = "ogv"; |  | ||||||
|                 break; |  | ||||||
|             case "video/flv": |  | ||||||
|             case "video/x-flv": |  | ||||||
|                 $image->ext = "flv"; |  | ||||||
|                 break; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         $image->filesize  = $metadata['size']; |  | ||||||
|         $image->hash      = $metadata['hash']; |  | ||||||
|         $image->filename  = $metadata['filename']; |  | ||||||
|         $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); |  | ||||||
|         $image->source    = $metadata['source']; |  | ||||||
| 
 |  | ||||||
|         return $image; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     protected function check_contents(string $tmpname): bool |     protected function check_contents(string $tmpname): bool | ||||||
|     { |     { | ||||||
|         return in_array(getMimeType($tmpname), self::SUPPORTED_MIME); |         return in_array(getMimeType($tmpname), self::SUPPORTED_MIME); | ||||||
|  | |||||||
| @ -43,7 +43,7 @@ class DataUploadEvent extends Event | |||||||
|         $this->set_tmpname($tmpname); |         $this->set_tmpname($tmpname); | ||||||
| 
 | 
 | ||||||
|         if ($config->get_bool("upload_use_mime")) { |         if ($config->get_bool("upload_use_mime")) { | ||||||
|             $this->set_type(get_extension_from_mime($tmpname)); |             $this->set_type(get_extension(getMimeType($tmpname))); | ||||||
|         } else { |         } else { | ||||||
|             if (array_key_exists('extension', $metadata) && !empty($metadata['extension'])) { |             if (array_key_exists('extension', $metadata) && !empty($metadata['extension'])) { | ||||||
|                 $this->type = strtolower($metadata['extension']); |                 $this->type = strtolower($metadata['extension']); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user