Skip to content

Commit

Permalink
[s3] allow sync to skip files that exist
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Jan 6, 2024
1 parent b4f61ef commit e401dd9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions ext/s3/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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);

Expand All @@ -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)
Expand Down

0 comments on commit e401dd9

Please sign in to comment.