Skip to content

Commit

Permalink
Merge pull request #105 from uploadcare/feature/bulk_download
Browse files Browse the repository at this point in the history
Added bulk download files from the server
  • Loading branch information
rsedykh authored Jun 14, 2024
2 parents 2a8e2b2 + a5a6bb6 commit ee22085
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 565 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Fixed:
* Fixed behaviour when uploading and downloading current images to and from Uploadcare storage.
* Autotests support.

Added:
* Bulk download files from the server.

Compatibility:
* Tested in WordPress up to version 6.4.3

Expand Down
142 changes: 88 additions & 54 deletions admin/UcAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,36 @@ public function transferMultiplyUp(): void {
\wp_die();
}

private function transferPostUp( int $postId ): array {
if ( ( $uuid = \get_post_meta( $postId, 'uploadcare_uuid', true ) ) ) {
try {
$this->api->file()->fileInfo( $uuid );
/**
* @return void
* @throws Exception
*/
public function transferMultiplyDown(): void {
$posts = $_POST['posts'] ?? null;
if ( $posts === null ) {
\wp_die( __( 'Required parameter is not set', 'uploadcare' ), '', 400 );
}
// JS sends array as `posts=1,2,3`
$posts = \explode( ',', $posts );

$result = [];
foreach ( $posts as $post_id ) {
$post_id = intval( sanitize_text_field( $post_id ) );
$image_id = strval( get_post_meta( $post_id, 'uploadcare_uuid', true ) );

if ( ! $image_id ) {
continue;
}
$result[] = $this->transfer_post_down( $post_id, $image_id );
}
echo \wp_json_encode( $result );
\wp_die();
}

private function transferPostUp( int $postId ): array {
if ( ( $uuid = \get_post_meta( $postId, 'uploadcare_uuid', true ) ) ) {
try {
$this->api->file()->fileInfo( $uuid );

return array(
'file_url' => \wp_get_attachment_image_src( $postId ),
Expand Down Expand Up @@ -323,35 +349,45 @@ private function transferPostUp( int $postId ): array {
);
}

/**
* Calls on `wp_ajax_{$action}` (in this case — `wp_ajax_uploadcare_down`).
*
* @throws Exception
*/
public function transferDown(): void {
if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce(
sanitize_text_field(
wp_unslash( $_REQUEST['nonce'] )
),
'media-nonce'
)
) {
return;
/**
* Calls on `wp_ajax_{$action}` (in this case — `wp_ajax_uploadcare_down`).
* @throws Exception
*/
public function transferDown(): void {
$post_id = null;

if ( isset( $_POST['postId'] ) ) {
$post_id = intval( sanitize_text_field( wp_unslash( $_POST['postId'] ) ) );
}
$postId = $_POST['postId'] ?? null;
$uuid = $_POST['uuid'] ?? null;
if ( $postId === null || $uuid === null ) {
\wp_die( __( 'Required parameter is not set', 'uploadcare' ), '', 400 );
}
$image_id = strval( get_post_meta( $post_id, 'uploadcare_uuid', true ) );

$post = \get_post( $postId );
if ( ! $post instanceof \WP_Post ) {
\wp_die( __( 'Post not found', 'uploadcare' ), '', 400 );
}
if ( $post_id === null || ! $image_id ) {
\wp_die( __( 'Required parameter is not set', 'uploadcare' ), '', 400 );
}

$image_id = \get_post_meta( $postId, 'uploadcare_uuid', true );
$result = $this->transfer_post_down( $post_id, $image_id );

echo \wp_json_encode( $result );
\wp_die();

}

/**
* Download file from UC server
*
* @param int $post_id
* @param string $image_id
*
* @return array
* @throws Exception
*/
private function transfer_post_down( int $post_id, string $image_id ): array {
$post = \get_post( $post_id );
if ( ! $post instanceof \WP_Post ) {
\wp_die( __( 'Post not found', 'uploadcare' ), '', 400 );
}

$uc_file_model = new UCFileModel( $image_id, $post->ID );
$uc_file_model = new UCFileModel( $image_id, $post->ID );

if ( ! $uc_file_model->is_file_valid() ) {
wp_die( __( 'Unable to get file from uploadcare', 'uploadcare' ), '', 400 );
Expand All @@ -370,34 +406,32 @@ public function transferDown(): void {
$localFilePath = \rtrim( $uploadDirData['path'], '/' ) . '/' . $original_file_name;
\file_put_contents( $localFilePath, $uc_file_content );

$subdir = \ltrim( ( $uploadDirData['subdir'] ?? '' ), '/' );
\update_post_meta( $postId, '_wp_attached_file', sprintf( '%s/%s', $subdir, $original_file_name ) );
\update_post_meta( $postId, '_wp_attachment_metadata', \wp_read_image_metadata( $localFilePath ) );
\delete_post_meta( $postId, 'uploadcare_url' );
\delete_post_meta( $postId, 'uploadcare_uuid' );
\delete_post_meta( $postId, 'uploadcare_url_modifiers' );
$subdir = \ltrim( ( $uploadDirData['subdir'] ?? '' ), '/' );
\update_post_meta( $post_id, '_wp_attached_file', sprintf( '%s/%s', $subdir, $original_file_name ) );
\update_post_meta( $post_id, '_wp_attachment_metadata', \wp_read_image_metadata( $localFilePath ) );
\delete_post_meta( $post_id, 'uploadcare_url' );
\delete_post_meta( $post_id, 'uploadcare_uuid' );
\delete_post_meta( $post_id, 'uploadcare_url_modifiers' );

$post->guid = $uploadDirData['url'] . '/' . $original_file_name;
\wp_update_post( $post );

$this->makeDefaultImageSizes( $post );

$fileUrl = \wp_attachment_is_image( $postId ) ? \wp_get_attachment_image_url( $postId ) : \get_attached_file( $postId, true );
if ( ! \is_string( $fileUrl ) ) {
\wp_die( __( 'Something wrong with upload to WordPress', 'uploadcare' ), '', 400 );
}

$result = array(
'fileUrl' => $fileUrl,
'uploadcare_url_modifiers' => '',
'postId' => $postId,
'uploadcare_uuid' => false,
);
$fileUrl = \wp_attachment_is_image( $post_id ) ? \wp_get_attachment_image_url( $post_id ) : \get_attached_file( $post_id, true );
if ( ! \is_string( $fileUrl ) ) {
\wp_die( __( 'Something wrong with upload to Wordpress', 'uploadcare' ), '', 400 );
}

echo \wp_json_encode( $result );
$result = [
'fileUrl' => $fileUrl,
'uploadcare_url_modifiers' => '',
'postId' => $post_id,
'uploadcare_uuid' => false,
];

\wp_die();
}
return $result;
}

private function makeDefaultImageSizes( WP_Post $post ): void {
if ( ! \wp_attachment_is_image( $post ) ) {
Expand All @@ -420,11 +454,11 @@ private function makeDefaultImageSizes( WP_Post $post ): void {
\wp_create_image_subsizes( $file, $post->ID );
}

public function loadPostByUuid( string $uuid ): ?WP_Post {
global $wpdb;
$query = 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE meta_value=\'%s\' AND meta_key=\'uploadcare_uuid\'';
$query = $wpdb->prepare( $query, $uuid );
$result = $wpdb->get_results( $query, ARRAY_A );
public function loadPostByUuid( string $uuid ): ?WP_Post {
global $wpdb;
$query = 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE meta_value=\'%s\' AND meta_key=\'uploadcare_uuid\'';
$query = $wpdb->prepare( $query, $uuid );
$result = $wpdb->get_results( $query, ARRAY_A );

if ( ( $postId = ( $result[0]['post_id'] ?? null ) ) === null ) {
return null;
Expand Down
1 change: 1 addition & 0 deletions includes/UploadcareMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private function define_admin_hooks(): void {
$this->loader->add_action( 'wp_ajax_uploadcare_transfer', $plugin_admin, 'transferUp' );
$this->loader->add_action( 'wp_ajax_uploadcare_down', $plugin_admin, 'transferDown' );
$this->loader->add_action( 'wp_ajax_uploadcare_upload_multiply', $plugin_admin, 'transferMultiplyUp' );
$this->loader->add_action( 'wp_ajax_uploadcare_download_multiply', $plugin_admin, 'transferMultiplyDown' );
$this->loader->add_action( 'post-upload-ui', $plugin_admin, 'uploadcare_media_upload' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'uploadcare_settings_actions' );
$this->loader->add_action( 'delete_attachment', $plugin_admin, 'attachmentDelete', 10, 2 );
Expand Down
5 changes: 3 additions & 2 deletions jsx/UcUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class UcUploader {
const initFile = mediaUrl ? [uploadcare.fileFrom('uploaded', mediaUrl)] : []
try {
const data = await uploadcare.openDialog(initFile, null, dialogPreferences).done();
data.nonce = this.config.nonce;
data.nonce = dialogPreferences.nonce;
return await this.storeImage(data);
} catch (err) {
if (err === 'upload') {
Expand Down Expand Up @@ -75,9 +75,10 @@ export default class UcUploader {

public storeImage(file: FileInfoResponse): Promise<FileInfoResponse> {
const data = new FormData();
let nonce = this.config.nonce;
data.append('action', 'uploadcare_handle');
data.append('file_url', file.originalUrl as string);
data.append('nonce', file.nonce);
data.append('nonce', nonce);
data.append('uploadcare_url_modifiers', file.cdnUrlModifiers as string);

return window.fetch(this.config.ajaxurl, {
Expand Down
2 changes: 1 addition & 1 deletion jsx/interfaces/UcConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export default interface UcConfig {
imagesOnly: boolean;
multiple: boolean;
effects: Array<string>;
nonce: Array<string>;
nonce: string;
}
Loading

0 comments on commit ee22085

Please sign in to comment.