Skip to content

Commit 80e30d2

Browse files
authored
Merge pull request #194 from cloudinary/feature/storage-ensure-file-exists
use storage to determine if a download should happen
2 parents 6371d36 + 9ea44d6 commit 80e30d2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,13 @@ function ( $val ) use ( $media ) {
185185
/**
186186
* Download an attachment source to the file system.
187187
*
188-
* @param int $attachment_id The attachment ID.
189-
* @param string $source The optional source to download.
188+
* @param int $attachment_id The attachment ID.
189+
* @param string $source The optional source to download.
190+
* @param string|null $date The date of the attachment to set storage folders.
190191
*
191192
* @return array|\WP_Error
192193
*/
193-
public function download_asset( $attachment_id, $source = null ) {
194+
public function download_asset( $attachment_id, $source = null, $date = null ) {
194195
require_once ABSPATH . 'wp-admin/includes/image.php';
195196
require_once ABSPATH . 'wp-admin/includes/media.php';
196197
if ( empty( $source ) ) {
@@ -200,7 +201,7 @@ public function download_asset( $attachment_id, $source = null ) {
200201
$file_name = basename( $source );
201202
try {
202203
// Prime a file to stream to.
203-
$upload = wp_upload_bits( $file_name, null, 'temp' );
204+
$upload = wp_upload_bits( $file_name, null, 'temp', $date );
204205
if ( ! empty( $upload['error'] ) ) {
205206
return new \WP_Error( 'download_error', $upload['error'] );
206207
}

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ public function validate_file_folder_sync( $attachment_id ) {
157157
* @return string
158158
*/
159159
public function generate_signature( $attachment_id ) {
160-
return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
160+
$file_exists = true;
161+
if ( $this->settings['offload'] !== 'cld' ) {
162+
$attachment_file = get_attached_file( $attachment_id );
163+
if ( ! file_exists( $attachment_file ) ) {
164+
$file_exists = $attachment_file;
165+
}
166+
}
167+
168+
return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exists;
161169
}
162170

163171
/**
@@ -184,7 +192,8 @@ public function sync( $attachment_id ) {
184192
$url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, true );
185193
break;
186194
case 'dual_full':
187-
if ( ! empty( $previous_state ) && 'dual_full' !== $previous_state ) {
195+
$exists = get_attached_file( $attachment_id );
196+
if ( ! empty( $previous_state ) && ! file_exists( $exists ) ) {
188197
// Only do this is it's changing a state.
189198
$transformations = $this->media->get_transformation_from_meta( $attachment_id );
190199
$url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, false );
@@ -198,7 +207,8 @@ public function sync( $attachment_id ) {
198207
if ( 'cld' !== $previous_state ) {
199208
$this->remove_local_assets( $attachment_id );
200209
}
201-
$this->download->download_asset( $attachment_id, $url );
210+
$date = get_post_datetime( $attachment_id );
211+
$this->download->download_asset( $attachment_id, $url, $date->format( 'Y/m' ) );
202212
}
203213

204214
$this->sync->set_signature_item( $attachment_id, 'storage' );

0 commit comments

Comments
 (0)