diff --git a/ext/s3/main.php b/ext/s3/main.php index 41cbab02d..bef6e8ad5 100644 --- a/ext/s3/main.php +++ b/ext/s3/main.php @@ -40,9 +40,12 @@ public function onCommand(CommandEvent $event) $end = $start; } foreach(Search::find_images_iterable(tags: ["order=id", "id>=$start", "id<=$end"]) as $image) { - print("{$image->id}: {$image->hash}\n"); + if($this->sync_post($image)) { + print("{$image->id}: {$image->hash}\n"); + } else { + print("{$image->id}: {$image->hash} (skipped)\n"); + } ob_flush(); - $this->sync_post($image); } } if ($event->cmd == "s3-rm") { @@ -133,20 +136,20 @@ private function hash_to_path(string $hash) } // underlying s3 interaction functions - private function sync_post(Image $image, ?array $new_tags = null) + private function sync_post(Image $image, ?array $new_tags = null, bool $overwrite = true): bool { global $config; // multiple events can trigger a sync, // let's only do one per request if(in_array($image->id, self::$synced)) { - return; + return false; } self::$synced[] = $image->id; $client = $this->get_client(); if(is_null($client)) { - return; + return false; } $image_bucket = $config->get_string(S3Config::IMAGE_BUCKET); @@ -159,14 +162,20 @@ private function sync_post(Image $image, ?array $new_tags = null) $image->tag_array = $_orig_tags; } + $key = $this->hash_to_path($image->hash); + if(!$overwrite && $client->doesObjectExist($image_bucket, $key)) { + return false; + } + $client->putObject([ 'Bucket' => $image_bucket, - 'Key' => $this->hash_to_path($image->hash), + 'Key' => $key, 'Body' => file_get_contents($image->get_image_filename()), 'ACL' => 'public-read', 'ContentType' => $image->get_mime(), 'ContentDisposition' => "inline; filename=\"$friendly\"", ]); + return true; } private function remove_file(string $hash)