[s3] allow sync to skip files that exist

This commit is contained in:
Shish 2024-01-06 22:57:00 +00:00
parent b4f61efbfc
commit e401dd9e52

View file

@ -40,9 +40,12 @@ class S3 extends Extension
$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 @@ class S3 extends Extension
}
// 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 @@ class S3 extends Extension
$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)