[s3] allow sync to skip files that exist
This commit is contained in:
parent
b4f61efbfc
commit
e401dd9e52
1 changed files with 15 additions and 6 deletions
|
@ -40,9 +40,12 @@ class S3 extends Extension
|
||||||
$end = $start;
|
$end = $start;
|
||||||
}
|
}
|
||||||
foreach(Search::find_images_iterable(tags: ["order=id", "id>=$start", "id<=$end"]) as $image) {
|
foreach(Search::find_images_iterable(tags: ["order=id", "id>=$start", "id<=$end"]) as $image) {
|
||||||
|
if($this->sync_post($image)) {
|
||||||
print("{$image->id}: {$image->hash}\n");
|
print("{$image->id}: {$image->hash}\n");
|
||||||
|
} else {
|
||||||
|
print("{$image->id}: {$image->hash} (skipped)\n");
|
||||||
|
}
|
||||||
ob_flush();
|
ob_flush();
|
||||||
$this->sync_post($image);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($event->cmd == "s3-rm") {
|
if ($event->cmd == "s3-rm") {
|
||||||
|
@ -133,20 +136,20 @@ class S3 extends Extension
|
||||||
}
|
}
|
||||||
|
|
||||||
// underlying s3 interaction functions
|
// 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;
|
global $config;
|
||||||
|
|
||||||
// multiple events can trigger a sync,
|
// multiple events can trigger a sync,
|
||||||
// let's only do one per request
|
// let's only do one per request
|
||||||
if(in_array($image->id, self::$synced)) {
|
if(in_array($image->id, self::$synced)) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
self::$synced[] = $image->id;
|
self::$synced[] = $image->id;
|
||||||
|
|
||||||
$client = $this->get_client();
|
$client = $this->get_client();
|
||||||
if(is_null($client)) {
|
if(is_null($client)) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
$image_bucket = $config->get_string(S3Config::IMAGE_BUCKET);
|
$image_bucket = $config->get_string(S3Config::IMAGE_BUCKET);
|
||||||
|
|
||||||
|
@ -159,14 +162,20 @@ class S3 extends Extension
|
||||||
$image->tag_array = $_orig_tags;
|
$image->tag_array = $_orig_tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$key = $this->hash_to_path($image->hash);
|
||||||
|
if(!$overwrite && $client->doesObjectExist($image_bucket, $key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$client->putObject([
|
$client->putObject([
|
||||||
'Bucket' => $image_bucket,
|
'Bucket' => $image_bucket,
|
||||||
'Key' => $this->hash_to_path($image->hash),
|
'Key' => $key,
|
||||||
'Body' => file_get_contents($image->get_image_filename()),
|
'Body' => file_get_contents($image->get_image_filename()),
|
||||||
'ACL' => 'public-read',
|
'ACL' => 'public-read',
|
||||||
'ContentType' => $image->get_mime(),
|
'ContentType' => $image->get_mime(),
|
||||||
'ContentDisposition' => "inline; filename=\"$friendly\"",
|
'ContentDisposition' => "inline; filename=\"$friendly\"",
|
||||||
]);
|
]);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function remove_file(string $hash)
|
private function remove_file(string $hash)
|
||||||
|
|
Reference in a new issue