Skip to content

Commit

Permalink
Return cached URL if image is already cached
Browse files Browse the repository at this point in the history
  • Loading branch information
itowhid06 committed Aug 17, 2020
1 parent 18aa75a commit 3e1164b
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions includes/classes/class-rtcamp-google-embeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function __construct() {
/**
* Receives the converted token after user logs in via Google account.
*
* @param array $token Converted access token.
* @param array $user_array User information fetched from token.
* @param object $client Google_Client object in use.
* @param array $token Converted access token.
* @param array $user_info User information fetched from token.
* @param object $client Google_Client object in use.
*
* @return void
*/
Expand Down Expand Up @@ -212,9 +212,9 @@ public function register_embeds() {
* Render preview for provided URL.
*
* @param array $matches The RegEx matches from the provided regex when calling
* wp_embed_register_handler().
* @param array $attr Embed attributes.
* @param string $url The original URL that was matched by the regex.
* wp_embed_register_handler().
* @param array $attr Embed attributes.
* @param string $url The original URL that was matched by the regex.
*
* @return false|string
*/
Expand All @@ -225,32 +225,20 @@ public function wpdocs_embed_handler_google_drive( $matches, $attr, $url ) {
return '';
}

$matches = array();
$file_id = '';
$basedir = '';
$baseurl = '';

preg_match( '/[-\w]{25,}/', $url, $matches );
if ( ! empty( $matches[0] ) ) {
$file_id = $matches[0];
}

$upload_dir = wp_upload_dir();
$uploadpath = path_join( $upload_dir['basedir'], 'cache/wp-google-drive' );
$basedir = '';
$baseurl = '';

$cached_file = path_join( $uploadpath, "{$file_id}.png" );
$cached_url = esc_url_raw( $upload_dir['baseurl'] . "/cache/wp-google-drive/{$file_id}.png" );

var_dump( $cached_file );
var_dump( $cached_url );
$file_id = $this->get_file_id_from_url( $url );
$cached_data = $this->get_cached_data_from_file_id( $file_id );

return $this->render_embed(
'google-drive-file',
array(
'drive_file_url' => $url,
'thumbnail_url' => $thumbnail_url,
'cached_file' => $cached_file,
'cached_url' => $cached_url,
'cached_file' => $cached_data['cached_file'],
'cached_url' => $cached_data['cached_url'],
)
);
}
Expand Down Expand Up @@ -288,9 +276,17 @@ private function get_thumbnail_url( $file_id, $user_id = false ) {
return false;
}

// Check if a preview file is already cached and return the cached url.
$cached_data = $this->get_cached_data_from_file_id( $file_id );

if ( file_exists( $cached_data['cached_file'] ) ) {
return $cached_data['cached_url'];
}

// Check if a preview exists for supplied file id.
$thumbnail_url = sprintf( 'https://drive.google.com/thumbnail?id=%s&sz=w400-h400', $file_id );
$response = wp_remote_get( $thumbnail_url );
$response = wp_remote_get( $thumbnail_url );

if ( ! is_wp_error( $response ) ) {
// Check if retrieved content is image and not google sign up page.
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
Expand Down Expand Up @@ -319,7 +315,8 @@ private function get_thumbnail_url( $file_id, $user_id = false ) {
}

// Set API url.
$url = sprintf( 'https://www.googleapis.com/drive/v2/files/%s?key=%s', $file_id, WP_GOOGLE_DRIVE_API_KEY );
$url = sprintf( 'https://www.googleapis.com/drive/v2/files/%s?key=%s', $file_id, WP_GOOGLE_DRIVE_API_KEY );

// Set headers.
$args = array(
'headers' => array(
Expand Down Expand Up @@ -472,6 +469,7 @@ public function get_thumb_preview( \WP_REST_Request $request ) {
}

$data['preview_url'] = $this->get_thumbnail_url( $file_id );

return new \WP_REST_Response( $data, 200 );
}

Expand Down Expand Up @@ -501,6 +499,25 @@ public function save_thumbnail( $file_id, $contents ) {

file_put_contents( $uploadfile, $contents ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents -- Safe to write image in the cache directory.
}

/**
* Return corresponding path and url of a file
*
* @param string $file_id File ID.
*
* @return array
*/
public function get_cached_data_from_file_id( $file_id ) {
$result = array();

$upload_dir = wp_upload_dir();
$uploadpath = path_join( $upload_dir['basedir'], 'cache/wp-google-drive' );

$result['cached_file'] = path_join( $uploadpath, "{$file_id}.png" );
$result['cached_url'] = esc_url_raw( $upload_dir['baseurl'] . "/cache/wp-google-drive/{$file_id}.png" );

return $result;
}
}

// Initialize the class.
Expand Down

0 comments on commit 3e1164b

Please sign in to comment.