public function deleteAsset(Event $event) { $settings = craft()->plugins->getPlugin('npCloudinary')->getSettings(); if ($settings['removeIfDeleted']) { try { $api = new \Cloudinary\Api(); $api->delete_resources([$this->getAssetHandle($event->params['asset'])]); return true; } catch (\Cloudinary\Error $e) { $this->handleError($e->getMessage()); } catch (\Cloudinary\Api\AuthorizationRequired $e) { $this->handleError($e->getMessage()); } } }
if ($photo->getError() !== UPLOAD_ERR_OK) { return $memberData; } $allowedTypes = ['image/jpeg', 'image/png']; if (!in_array($photo->getClientMediaType(), $allowedTypes)) { throw new InvalidArgumentException('We only accept jpg and png image'); } $ext = strtolower(pathinfo($photo->getClientFilename(), PATHINFO_EXTENSION)); $cdnTargetPath = 'phpindonesia/' . $settings['mode'] . '/'; $newFileName = $session->get('user_id') . '-' . date('YmdHis'); Cloudinary\Uploader::upload($photo->file, ['public_id' => $cdnTargetPath . $newFileName, 'tags' => ['user-avatar']]); $memberData['photo'] = $newFileName . '.' . $ext; if ($session->get('photo')) { $api = new Cloudinary\Api(); $publicId = str_replace('.' . $ext, '', $session->get('photo')); $api->delete_resources($cdnTargetPath . $publicId, ['public_id' => $cdnTargetPath . $newFileName, 'tags' => ['user-avatar']]); $session->set('photo', $memberData['photo']); } return $memberData; }; }; /** * Setup mailer container * * TODO: will replaced with PHPMailer */ $container['mailer'] = function ($container) { $smtp_account = $container->get('settings')['smtp']; $transport = null; if ($smtp_account['ssl']) { $transport = Swift_SmtpTransport::newInstance($smtp_account['host'], $smtp_account['port'], 'ssl');
function test_overwrite_upload() { $api = new \Cloudinary\Api(); $public_id = "api_test_overwrite"; $api->delete_resources($public_id); $resource = \Cloudinary\Uploader::upload("tests/logo.png", array("public_id" => $public_id)); $this->assertArrayHasKey("etag", $resource, "Should return an etag when uploading a new resource"); $resource = \Cloudinary\Uploader::upload("tests/logo.png", array("public_id" => $public_id, "overwrite" => false)); $this->assertArrayNotHasKey("etag", $resource, "Should not return an etag when uploading a existing resource with overwrite=false"); $this->assertArrayHasKey("existing", $resource, "Should return 'existing' when uploading a existing resource with overwrite=false"); $resource = \Cloudinary\Uploader::upload("tests/logo.png", array("public_id" => $public_id, "overwrite" => true)); $this->assertArrayHasKey("etag", $resource, "Should return an etag when uploading an existing resource with overwrite=true"); $api->delete_resources($public_id); }
/** * Test image deletion * * @return void * @author Andrew Lowther <*****@*****.**> **/ public function testCloudinaryImageDeletion() { $api = new \Cloudinary\Api(); $result = $api->delete_resources(array('test.png')); $this->assertTrue(is_a($result, 'Cloudinary\\Api\\Response')); }
finfo_close($finfo); $ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION)); $env_mode = $this->getContainer()->get('settings')['mode']; $cdn_upload_path = 'phpindonesia/' . $env_mode . '/'; $new_fname = $_SESSION['MembershipAuth']['user_id'] . '-' . date('YmdHis'); $options = ['public_id' => $cdn_upload_path . $new_fname, 'tags' => ['user-avatar']]; if (in_array($mime_type, ['image/jpeg', 'image/png']) && $ext != 'php') { // Upload photo to CDN Cloudinary $photo = \Cloudinary\Uploader::upload($_FILES['photo']['tmp_name'], $options); $members_profiles['photo'] = $new_fname . '.' . $ext; // Delete old photo if ($_SESSION['MembershipAuth']['photo'] != null) { $api = new \Cloudinary\Api(); $public_id = str_replace('.' . $ext, '', $_SESSION['MembershipAuth']['photo']); $options = ['public_id' => $cdn_upload_path . $new_fname, 'tags' => ['user-avatar']]; $api->delete_resources($cdn_upload_path . $public_id, $options); $_SESSION['MembershipAuth']['photo'] = $members_profiles['photo']; } } } // Update profile data record $db->update('members_profiles', $members_profiles, array('user_id' => $_SESSION['MembershipAuth']['user_id'])); $db->update('users', array('email' => trim($_POST['email']), 'province_id' => $_POST['province_id'], 'city_id' => $_POST['city_id'], 'area' => $area, 'modified' => date('Y-m-d H:i:s'), 'modified_by' => $_SESSION['MembershipAuth']['user_id']), array('user_id' => $_SESSION['MembershipAuth']['user_id'])); // Handle social medias if (isset($_POST['socmeds']) && !empty($_POST['socmeds'])) { foreach ($_POST['socmeds'] as $item) { $row = array('user_id' => $_SESSION['MembershipAuth']['user_id'], 'socmed_type' => filter_var(trim($item['socmed_type']), FILTER_SANITIZE_STRING), 'account_name' => filter_var(trim($item['account_name']), FILTER_SANITIZE_STRING), 'account_url' => filter_var(trim($item['account_url']), FILTER_SANITIZE_STRING), 'created' => date('Y-m-d H:i:s')); if ($item['member_socmed_id'] == 0) { $db->insert('members_socmeds', $row); } else { unset($row['created']);