forked from Cavemanon/cavepaintings
Added option to detect file type based on header bytes
This commit is contained in:
@@ -53,21 +53,34 @@ function add_image(string $tmpname, string $filename, string $tags): void
|
||||
assert(file_exists($tmpname));
|
||||
|
||||
$pathinfo = pathinfo($filename);
|
||||
if (!array_key_exists('extension', $pathinfo)) {
|
||||
throw new UploadException("File has no extension");
|
||||
}
|
||||
$metadata = [];
|
||||
$metadata['filename'] = $pathinfo['basename'];
|
||||
$metadata['extension'] = $pathinfo['extension'];
|
||||
if (array_key_exists('extension', $pathinfo)) {
|
||||
$metadata['extension'] = $pathinfo['extension'];
|
||||
}
|
||||
|
||||
$metadata['tags'] = Tag::explode($tags);
|
||||
$metadata['source'] = null;
|
||||
$event = new DataUploadEvent($tmpname, $metadata);
|
||||
send_event($event);
|
||||
if ($event->image_id == -1) {
|
||||
throw new UploadException("File type not recognised");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function get_extension_from_mime(String $file_path): ?String
|
||||
{
|
||||
global $config;
|
||||
$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
|
||||
* into the configured thumbnail square, with ratio intact
|
||||
@@ -410,4 +423,4 @@ function image_resize_gd(String $image_filename, array $info, int $new_width, in
|
||||
throw new ImageResizeException("Failed to save the new image, function returned false when saving type: $output_type");
|
||||
}
|
||||
imagedestroy($image_resized);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user