Revert "Merge tag 'v2.10.6'"

This reverts commit 122ea4ab9e, reversing
changes made to c54a11e250.
This commit is contained in:
2024-02-16 23:06:09 -06:00
parent 122ea4ab9e
commit 6c08ee9675
521 changed files with 12363 additions and 14503 deletions

View File

@@ -50,7 +50,7 @@ class TranscodeImage extends Extension
}
public function onInitExt(InitExtEvent $event): void
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_bool(TranscodeConfig::ENABLED, true);
@@ -72,20 +72,17 @@ class TranscodeImage extends Extension
return TranscodeConfig::UPLOAD_PREFIX.$mime;
}
private static function get_mapping(string $mime): ?string
private static function get_mapping(String $mime): ?string
{
global $config;
return $config->get_string(self::get_mapping_name($mime));
}
private static function set_mapping(string $from_mime, ?string $to_mime): void
private static function set_mapping(String $from_mime, ?String $to_mime): void
{
global $config;
$config->set_string(self::get_mapping_name($from_mime), $to_mime);
}
/**
* @return string[]
*/
public static function get_enabled_mimes(): array
{
$output = [];
@@ -98,10 +95,10 @@ class TranscodeImage extends Extension
return $output;
}
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
if ($this->get_version(TranscodeConfig::VERSION) < 1) {
$old_extensions = [];
$old_extensions =[];
foreach (array_values(self::INPUT_MIMES) as $mime) {
$old_extensions = array_merge($old_extensions, FileExtension::get_all_for_mime($mime));
}
@@ -129,7 +126,7 @@ class TranscodeImage extends Extension
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
global $user, $config;
@@ -142,7 +139,7 @@ class TranscodeImage extends Extension
}
}
public function onSetupBuilding(SetupBuildingEvent $event): void
public function onSetupBuilding(SetupBuildingEvent $event)
{
global $config;
@@ -155,7 +152,7 @@ class TranscodeImage extends Extension
$sb->add_bool_option(TranscodeConfig::GET_ENABLED, "Enable GET args", true);
$sb->add_bool_option(TranscodeConfig::UPLOAD, "Transcode on upload", true);
$sb->add_choice_option(TranscodeConfig::ENGINE, MediaEngine::IMAGE_ENGINES, "Engine", true);
foreach (self::INPUT_MIMES as $display => $mime) {
foreach (self::INPUT_MIMES as $display=> $mime) {
if (MediaEngine::is_input_supported($engine, $mime)) {
$outputs = $this->get_supported_output_mimes($engine, $mime);
$sb->add_choice_option(self::get_mapping_name($mime), $outputs, "$display", true);
@@ -166,7 +163,7 @@ class TranscodeImage extends Extension
$sb->end_table();
}
public function onDataUpload(DataUploadEvent $event): void
public function onDataUpload(DataUploadEvent $event)
{
global $config;
@@ -183,7 +180,7 @@ class TranscodeImage extends Extension
}
if ($config->get_bool(TranscodeConfig::UPLOAD) == true) {
if ($event->mime === MimeType::GIF && MimeType::is_animated_gif($event->tmpname)) {
if ($event->mime === MimeType::GIF&&MimeType::is_animated_gif($event->tmpname)) {
return;
}
@@ -206,7 +203,7 @@ class TranscodeImage extends Extension
public function onPageRequest(PageRequestEvent $event): void
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user;
@@ -235,7 +232,7 @@ class TranscodeImage extends Extension
}
}
public function onImageDownloading(ImageDownloadingEvent $event): void
public function onImageDownloading(ImageDownloadingEvent $event)
{
global $config, $user;
@@ -256,10 +253,10 @@ class TranscodeImage extends Extension
$source_mime = $event->image->get_mime();
if ($source_mime != $target_mime) {
if ($source_mime!=$target_mime) {
$tmp_filename = $this->transcode_image($event->path, $source_mime, $target_mime);
if ($event->file_modified === true && $event->path != $event->image->get_image_filename()) {
if ($event->file_modified===true&&$event->path!=$event->image->get_image_filename()) {
// This means that we're dealing with a temp file that will need cleaned up
unlink($event->path);
}
@@ -271,7 +268,7 @@ class TranscodeImage extends Extension
}
}
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event): void
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
{
global $user, $config;
@@ -282,7 +279,7 @@ class TranscodeImage extends Extension
}
}
public function onBulkAction(BulkActionEvent $event): void
public function onBulkAction(BulkActionEvent $event)
{
global $user, $database, $page;
@@ -297,23 +294,30 @@ class TranscodeImage extends Extension
$size_difference = 0;
foreach ($event->items as $image) {
try {
$before_size = $image->filesize;
$database->with_savepoint(function () use ($image, $mime) {
$this->transcode_and_replace_image($image, $mime);
});
$database->begin_transaction();
$before_size = $image->filesize;
$new_image = $this->transcode_and_replace_image($image, $mime);
// If a subsequent transcode fails, the database needs to have everything about the previous
// transcodes recorded already, otherwise the image entries will be stuck pointing to
// missing image files
$database->commit();
$total++;
$size_difference += ($before_size - $image->filesize);
$size_difference += ($before_size - $new_image->filesize);
} catch (\Exception $e) {
log_error("transcode", "Error while bulk transcode on item {$image->id} to $mime: ".$e->getMessage());
try {
$database->rollback();
} catch (\Exception $e) {
// is this safe? o.o
}
}
}
if ($size_difference > 0) {
if ($size_difference>0) {
$page->flash("Transcoded $total items, reduced size by ".human_filesize($size_difference));
} elseif ($size_difference < 0) {
$page->flash("Transcoded $total items, increased size by ".human_filesize(-1 * $size_difference));
} elseif ($size_difference<0) {
$page->flash("Transcoded $total items, increased size by ".human_filesize(-1*$size_difference));
} else {
$page->flash("Transcoded $total items, no size difference");
}
@@ -323,25 +327,24 @@ class TranscodeImage extends Extension
}
private function can_convert_mime(string $engine, string $mime): bool
private function can_convert_mime($engine, $mime): bool
{
return MediaEngine::is_input_supported($engine, $mime);
}
/**
* @return array<string, string>
*/
private function get_supported_output_mimes(string $engine, ?string $omit_mime = null): array
private function get_supported_output_mimes($engine, ?String $omit_mime = null): array
{
$output = [];
foreach (self::OUTPUT_MIMES as $key => $value) {
if ($value == "") {
foreach (self::OUTPUT_MIMES as $key=> $value) {
if ($value=="") {
$output[$key] = $value;
continue;
}
if (MediaEngine::is_output_supported($engine, $value)
&& (empty($omit_mime) || $omit_mime != $value)) {
&&(empty($omit_mime)||$omit_mime!=$value)) {
$output[$key] = $value;
}
}
@@ -350,24 +353,46 @@ class TranscodeImage extends Extension
private function transcode_and_replace_image(Image $image, string $target_mime): void
private function transcode_and_replace_image(Image $image_obj, String $target_mime): Image
{
$original_file = warehouse_path(Image::IMAGE_DIR, $image->hash);
$tmp_filename = $this->transcode_image($original_file, $image->get_mime(), $target_mime);
send_event(new ImageReplaceEvent($image, $tmp_filename));
$original_file = warehouse_path(Image::IMAGE_DIR, $image_obj->hash);
$tmp_filename = $this->transcode_image($original_file, $image_obj->get_mime(), $target_mime);
$new_image = new Image();
$new_image->hash = md5_file($tmp_filename);
$new_image->filesize = filesize($tmp_filename);
$new_image->filename = $image_obj->filename;
$new_image->width = $image_obj->width;
$new_image->height = $image_obj->height;
/* Move the new image into the main storage location */
$target = warehouse_path(Image::IMAGE_DIR, $new_image->hash);
if (!@copy($tmp_filename, $target)) {
throw new ImageTranscodeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)");
}
/* Remove temporary file */
@unlink($tmp_filename);
send_event(new ImageReplaceEvent($image_obj->id, $new_image));
return $new_image;
}
private function transcode_image(string $source_name, string $source_mime, string $target_mime): string
private function transcode_image(String $source_name, String $source_mime, string $target_mime): string
{
global $config;
if ($source_mime == $target_mime) {
if ($source_mime==$target_mime) {
throw new ImageTranscodeException("Source and target MIMEs are the same: ".$source_mime);
}
$engine = $config->get_string(TranscodeConfig::ENGINE);
if (!$this->can_convert_mime($engine, $source_mime)) {
throw new ImageTranscodeException("Engine $engine does not support input MIME $source_mime");
}
@@ -385,15 +410,15 @@ class TranscodeImage extends Extension
}
}
private function transcode_image_gd(string $source_name, string $source_mime, string $target_mime): string
private function transcode_image_gd(String $source_name, String $source_mime, string $target_mime): string
{
global $config;
$q = $config->get_int(TranscodeConfig::QUALITY);
$tmp_name = shm_tempnam("transcode");
$tmp_name = tempnam(sys_get_temp_dir(), "shimmie_transcode");
$image = false_throws(imagecreatefromstring(file_get_contents_ex($source_name)));
$image = imagecreatefromstring(file_get_contents($source_name));
try {
$result = false;
switch ($target_mime) {
@@ -408,15 +433,18 @@ class TranscodeImage extends Extension
$width = imagesx($image);
$height = imagesy($image);
$new_image = imagecreatetruecolor($width, $height);
if ($new_image === false) {
if ($new_image===false) {
throw new ImageTranscodeException("Could not create image with dimensions $width x $height");
}
try {
$background_color = Media::hex_color_allocate($new_image, $config->get_string(TranscodeConfig::ALPHA_COLOR));
if (imagefilledrectangle($new_image, 0, 0, $width, $height, $background_color) === false) {
if ($background_color===false) {
throw new ImageTranscodeException("Could not allocate background color");
}
if (imagefilledrectangle($new_image, 0, 0, $width, $height, $background_color)===false) {
throw new ImageTranscodeException("Could not fill background color");
}
if (imagecopy($new_image, $image, 0, 0, 0, 0, $width, $height) === false) {
if (imagecopy($new_image, $image, 0, 0, 0, 0, $width, $height)===false) {
throw new ImageTranscodeException("Could not copy source image to new image");
}
$result = imagejpeg($new_image, $tmp_name, $q);
@@ -428,13 +456,13 @@ class TranscodeImage extends Extension
} finally {
imagedestroy($image);
}
if ($result === false) {
if ($result===false) {
throw new ImageTranscodeException("Error while transcoding ".$source_name." to ".$target_mime);
}
return $tmp_name;
}
private function transcode_image_convert(string $source_name, string $source_mime, string $target_mime): string
private function transcode_image_convert(String $source_name, String $source_mime, string $target_mime): string
{
global $config;
@@ -467,7 +495,7 @@ class TranscodeImage extends Extension
break;
}
$tmp_name = shm_tempnam("transcode");
$tmp_name = tempnam(sys_get_temp_dir(), "shimmie_transcode");
$source_type = FileExtension::get_for_mime($source_mime);
@@ -479,7 +507,7 @@ class TranscodeImage extends Extension
log_debug('transcode', "Transcoding with command `$cmd`, returns $ret");
if ($ret !== 0) {
if ($ret!==0) {
throw new ImageTranscodeException("Transcoding failed with command ".$cmd.", returning ".implode("\r\n", $output));
}