Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache preview images of docs locally & Fix showing doc preview issues #6

Open
wants to merge 5 commits into
base: custom-oembed-provider
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions includes/classes/class-rtcamp-google-embeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function wp_google_login_scopes( $scopes ) {

/**
* Register custom oembed provider for google drive urls.
*
*
* @param array $providers Default providers.
*
*
* @return array Modified providers.
*/
public function oembed_providers( $providers ) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public function oembed_providers( $providers ) {

/**
* Define required plugin constants.
*
*
* @return void
*/
private function add_plugin_constants() {
Expand All @@ -137,7 +137,7 @@ private function add_plugin_constants() {

/**
* Loads plugin textdomain.
*
*
* @return void
*/
public function load_textdomain() {
Expand All @@ -146,7 +146,7 @@ public function load_textdomain() {

/**
* Registers all supported embeds.
*
*
* @return void
*/
public function register_embeds() {
Expand Down Expand Up @@ -261,15 +261,17 @@ private function get_thumbnail_url( $file_id, $user_id = false ) {
// 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 );
$contents = wp_remote_retrieve_body( $response );
itowhid06 marked this conversation as resolved.
Show resolved Hide resolved
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' );
if ( false !== strpos( $content_type, 'image/' ) ) {

// Check if retrieved http code is 200.
$status_code = wp_remote_retrieve_response_code( $response );
if ( 200 === $status_code ) {
// Save the thumbnail.
$this->save_thumbnail( $file_id, $contents );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to cache image which is being fetched on line 322 of this file too. And test once if it's working fine even after token is expired

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try implementing this? Current save_thumbnail call will only work for public files, not for private files. In order for this to work with private files, you'll need to do what I've suggested above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also get_thumbnail_url function should return cached URL if network request fails. If all the network requests fails, then at the end you can check whether the file of this ID is already cached, if it is then send that URL.


return $thumbnail_url;
}
}
Expand Down Expand Up @@ -316,7 +318,7 @@ private function get_thumbnail_url( $file_id, $user_id = false ) {

/**
* Register endpoints.
*
*
* @return void
*/
public function register_routes() {
Expand All @@ -333,7 +335,7 @@ public function register_routes() {
],
]
);

// Route for custom oembed provider for google drive.
register_rest_route(
'rt-google-embed/v1',
Expand Down Expand Up @@ -442,6 +444,32 @@ 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 );
}

/**
* Save thumbnail of a doc.
*
* @param string $file_id File ID.
* @param mixed $contents Contents of file.
*
* @return void
*/
public function save_thumbnail( $file_id, $contents ) {
$uploaddir = wp_upload_dir();

$uploadpath = $uploaddir['basedir'] . '/cache/wp-google-drive/';
SID177 marked this conversation as resolved.
Show resolved Hide resolved

if ( ! file_exists( $uploadpath ) ) {
mkdir( $uploadpath, 0755, true );
}

$uploadfile = $uploaddir['basedir'] . "/cache/wp-google-drive/{$file_id}.png";

if ( file_exists( $uploadfile ) ) {
return;
}

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

// Initialize the class.
Expand Down
26 changes: 19 additions & 7 deletions templates/embeds/google-drive-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@
// prevent direct access to this file.
exit;
}
?>
<div style="border: 1px solid #000; text-align: center;">
<a href="<?php echo esc_url( $drive_file_url ); ?>" title="<?php esc_attr_e( 'Open the Shared Document', 'rt-google-embeds' ); ?>" target="_blank" rel="noopener noreferrer" style="color: #cd2653;">
<?php esc_html_e( 'Open Shared Document.', 'rt-google-embeds' ); ?>
</a>
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" />
</div>

$matches = array();
preg_match( '/[-\w]{25,}/', $drive_file_url, $matches );
$file_id = $matches[0];
SID177 marked this conversation as resolved.
Show resolved Hide resolved

$uploaddir = wp_upload_dir();

$uploadfile = $uploaddir['basedir'] . "/cache/wp-google-drive/{$file_id}.png";
SID177 marked this conversation as resolved.
Show resolved Hide resolved

if ( file_exists( $uploadfile ) ) : ?>
<img src="<?php echo esc_url( $uploaddir['baseurl'] . "/cache/wp-google-drive/{$file_id}.png" ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview from Cache', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" />
SID177 marked this conversation as resolved.
Show resolved Hide resolved
<?php else : ?>
<div style="border: 1px solid #000; text-align: center;">
<a href="<?php echo esc_url( $drive_file_url ); ?>" title="<?php esc_attr_e( 'Open the Shared Document', 'rt-google-embeds' ); ?>" target="_blank" rel="noopener noreferrer" style="color: #cd2653;">
<?php esc_html_e( 'Open Shared Document.', 'rt-google-embeds' ); ?>
</a>
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" />
</div>
<?php endif; ?>