From 56e8cd063701de3cba082ba8b91c290eb57d854b Mon Sep 17 00:00:00 2001 From: David Cramer Date: Mon, 7 Sep 2020 19:32:30 +0200 Subject: [PATCH 1/3] use storage to determine if a download should happen --- .../php/sync/class-storage.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index f2bb63527..f6a4a2039 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -157,7 +157,15 @@ public function validate_file_folder_sync( $attachment_id ) { * @return string */ public function generate_signature( $attachment_id ) { - return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + $file_exsts = true; + if ( $this->settings['offload'] !== 'cld' ) { + $attachment_file = get_attached_file( $attachment_id ); + if ( ! file_exists( $attachment_file ) ) { + $file_exsts = $attachment_file; + } + } + + return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exsts; } /** @@ -184,7 +192,8 @@ public function sync( $attachment_id ) { $url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, true ); break; case 'dual_full': - if ( ! empty( $previous_state ) && 'dual_full' !== $previous_state ) { + $exists = get_attached_file( $attachment_id ); + if ( ! empty( $previous_state ) && ! file_exists( $exists ) ) { // Only do this is it's changing a state. $transformations = $this->media->get_transformation_from_meta( $attachment_id ); $url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, false ); From c4ef6237c1f0a07031464920d1ca822e20a5128f Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 8 Sep 2020 11:18:33 +0200 Subject: [PATCH 2/3] use original folders --- .../php/sync/class-download-sync.php | 9 +++++---- .../php/sync/class-storage.php | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index d5ab87c82..0e9f17673 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -185,12 +185,13 @@ function ( $val ) use ( $media ) { /** * Download an attachment source to the file system. * - * @param int $attachment_id The attachment ID. - * @param string $source The optional source to download. + * @param int $attachment_id The attachment ID. + * @param string $source The optional source to download. + * @param string|null $date The date of the attachment to set storage folders. * * @return array|\WP_Error */ - public function download_asset( $attachment_id, $source = null ) { + public function download_asset( $attachment_id, $source = null, $date = null ) { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; if ( empty( $source ) ) { @@ -200,7 +201,7 @@ public function download_asset( $attachment_id, $source = null ) { $file_name = basename( $source ); try { // Prime a file to stream to. - $upload = wp_upload_bits( $file_name, null, 'temp' ); + $upload = wp_upload_bits( $file_name, null, 'temp', $date ); if ( ! empty( $upload['error'] ) ) { return new \WP_Error( 'download_error', $upload['error'] ); } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index f6a4a2039..5cc6a4df1 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -207,7 +207,8 @@ public function sync( $attachment_id ) { if ( 'cld' !== $previous_state ) { $this->remove_local_assets( $attachment_id ); } - $this->download->download_asset( $attachment_id, $url ); + $date = get_post_datetime( $attachment_id ); + $this->download->download_asset( $attachment_id, $url, $date->format('Y/m') ); } $this->sync->set_signature_item( $attachment_id, 'storage' ); From 9ea44d6e4ba2d9332678a772ce89c63870ef5bd8 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 8 Sep 2020 11:19:39 +0200 Subject: [PATCH 3/3] formatting --- .../php/sync/class-storage.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index 5cc6a4df1..9cfa6cd96 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -157,15 +157,15 @@ public function validate_file_folder_sync( $attachment_id ) { * @return string */ public function generate_signature( $attachment_id ) { - $file_exsts = true; + $file_exists = true; if ( $this->settings['offload'] !== 'cld' ) { $attachment_file = get_attached_file( $attachment_id ); if ( ! file_exists( $attachment_file ) ) { - $file_exsts = $attachment_file; + $file_exists = $attachment_file; } } - return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exsts; + return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exists; } /** @@ -208,7 +208,7 @@ public function sync( $attachment_id ) { $this->remove_local_assets( $attachment_id ); } $date = get_post_datetime( $attachment_id ); - $this->download->download_asset( $attachment_id, $url, $date->format('Y/m') ); + $this->download->download_asset( $attachment_id, $url, $date->format( 'Y/m' ) ); } $this->sync->set_signature_item( $attachment_id, 'storage' );