Skip to content

Commit

Permalink
Merge pull request #26 from ZNick1982/fix_file_duplication
Browse files Browse the repository at this point in the history
Fix of files duplication
  • Loading branch information
ZNick1982 committed Feb 22, 2018
2 parents d40e8a7 + 9ec8ecf commit 58ec0ad
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 2.5.3 ([Download](http://downloads.wordpress.org/plugin/uploadcare.2.5.3.zip))
- Fixed file duplication on upload
- Fixed widget effects tab settings

### 2.5.2 ([Download](http://downloads.wordpress.org/plugin/uploadcare.2.5.2.zip))
- Fixed missed "Fine tuning" setup for widget
- Test up to Wordpress 4.9.4
Expand Down
42 changes: 28 additions & 14 deletions inc/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,15 @@ function uploadcare_attach($file) {
'post_status' => 'inherit',
'post_mime_type' => $file->data['mime_type'],
);

$isImage = $file->data['is_image'];
$attachment_id = wp_insert_post($attachment, true);

$meta = array('width' => $file->data['image_info']->width,
'height' => $file->data['image_info']->height);
$meta = $isImage ? uploadcare_get_final_dim($file) : array('width' => null, 'height' => null);

if (get_option('uploadcare_download_to_server')) {
$attached_file = uploadcare_download($file);
add_post_meta($attachment_id, '_uc_is_local_file', true, true);
} else {
$attached_file = $file->data['original_file_url'];
$attached_file = $file->getUrl(!$isImage ? $filename : null);
add_post_meta($attachment_id, 'uploadcare_url', $attached_file, true);
}

Expand All @@ -134,11 +132,11 @@ function uploadcare_attach($file) {
*/
function uploadcare_download(Uploadcare\File $file) {
// downloading contents of image
$contents = wp_remote_get($file);
$contents = wp_remote_get($file->getUrl());

$dirInfo = wp_upload_dir();
$absPath = $dirInfo['basedir'] . '/';
$localFilename = 'uploadcare' . $dirInfo['subdir'] . '/' . basename($file) . '.jpg';
$localFilename = 'uploadcare' . $dirInfo['subdir'] . '/' . $file->getUuid().'.'. $file->data['original_filename'];

// creating folders tree
wp_mkdir_p($absPath . dirname($localFilename));
Expand All @@ -150,19 +148,15 @@ function uploadcare_download(Uploadcare\File $file) {
}

/**
* Add ajax upload handler
* Ajax upload handler - registers the new images in the Wordpress media library
*/
add_action('wp_ajax_uploadcare_handle', 'uploadcare_handle');
function uploadcare_handle() {
// store file
$api = uploadcare_api();
$file_id = $_POST['file_id'];
$file_url = $_POST['file_url'];
if($file_url) {
$file = $api->uploader->fromUrl($file_url);
} else {
$file = $api->getFile($file_id);
}

$file = $api->getFile($file_url);
$file->store();
$attachment_id = uploadcare_attach($file);
$fileUrl = get_post_meta($attachment_id, '_wp_attached_file', true);
Expand Down Expand Up @@ -290,3 +284,23 @@ function uploadcare_settings() {
function uploadcare_settings_actions() {
add_options_page('Uploadcare', 'Uploadcare', 'upload_files', 'uploadcare', 'uploadcare_settings');
}

/*
* Calculate final image dimensions
*/
function uploadcare_get_final_dim($file) {
if(!$file->default_effects) {
return array('width' => $file->data['image_info']->width,
'height' => $file->data['image_info']->height);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file->getUrl().'-/json/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
return array('width' => $res['width'], 'height' => $res['height']);
}
2 changes: 0 additions & 2 deletions inc/uploadcare_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@
update_option('uploadcare_finetuning', $uploadcare_finetuning);
$uploadcare_source_tabs = $_POST['uploadcare_source_tabs'];
update_option('uploadcare_source_tabs', $uploadcare_source_tabs);

$uploadcare_tab_effects = $_POST['uploadcare_tab_effects'];
update_option('uploadcare_tab_effects', $uploadcare_tab_effects);

$saved = true;
} else {
$uploadcare_public = get_option('uploadcare_public');
Expand Down
7 changes: 4 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

uploadcare.registerTab('preview', uploadcareTabEffects);
function ucEditFile(file_id) {
try {
tb_remove();
Expand All @@ -16,7 +15,6 @@ function uploadcareMediaButton() {
function ucStoreImg(fileInfo, callback) {
var data = {
'action': 'uploadcare_handle',
'file_id': fileInfo.uuid,
'file_url': fileInfo.cdnUrl,
};
uploadcare.jQuery.post(ajaxurl, data, function(response) {
Expand All @@ -28,7 +26,6 @@ function ucStoreImg(fileInfo, callback) {

function ucAddImg(fileInfo) {
ucStoreImg(fileInfo, function(response) {
console.log(response);
var obj = uploadcare.jQuery.parseJSON(response);
var fileUrl = obj.fileUrl;
if (fileInfo.isImage) {
Expand Down Expand Up @@ -140,6 +137,10 @@ function ucPostUploadUiBtn() {
}

uploadcare.jQuery(function() {
if(WP_UC_PARAMS.effects) {
uploadcare.registerTab('preview', uploadcareTabEffects);
}

// add button to all inputs with .uploadcare-url-field
uploadcare.jQuery('input.uploadcare-url-field').each(function() {
var input = uploadcare.jQuery(this);
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: grayhound1, dmitry-mukhin
Tags: media upload, file handling, cdn, storage, facebook, dropbox, instagram, google drive, vk, evernote, box, images, flickr
Requires at least: 3.5+
Tested up to: 4.9.4
Stable tag: 2.5.2
Stable tag: 2.5.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Donate link: https://uploadcare.com/pricing/
Expand Down Expand Up @@ -115,6 +115,10 @@ Access all files in your Uploadcare account via Media Library.

== Changelog ==

= 2.5.3 =
* Fixed file duplication on upload
* Fixed widget effects tab settings

= 2.5.2 =
* Fixed missed "Fine tuning" setup for widget
* Test up to Wordpress 4.9.4
Expand Down
4 changes: 2 additions & 2 deletions uploadcare.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Uploadcare
Plugin URI: http://github.com/uploadcare/uploadcare-wordpress
Description: Uploadcare let's you upload anything from anywhere (Instagram, Facebook, Dropbox, etc.)
Version: 2.5.2
Version: 2.5.3
Author: Uploadcare
Author URI: https://uploadcare.com/
License: GPL2
Expand All @@ -14,7 +14,7 @@
exit("Uploadcare plugin requires PHP version <b>5.3+</b>, you've got <b>" . PHP_VERSION . "</b>");
}

define('UPLOADCARE_PLUGIN_VERSION', '2.5.2');
define('UPLOADCARE_PLUGIN_VERSION', '2.5.3');
define('UPLOADCARE_WIDGET_VERSION', '3.x');
define('UPLOADCARE_TAB_EFFECTS_VERSION', '1.x');

Expand Down

0 comments on commit 58ec0ad

Please sign in to comment.