public function transer_dir($image_id)
 {
     $this->EE->load->helper('file');
     // Grab image info
     $query = $this->EE->db->select('field_id, entry_id, filename, extension')->from('exp_channel_images')->where('image_id', $image_id)->limit(1)->get();
     $field_id = $query->row('field_id');
     // Grab settings
     $settings = $this->EE->image_helper->grabFieldSettings($field_id);
     $filename = $query->row('filename');
     $extension = '.' . substr(strrchr($filename, '.'), 1);
     $entry_id = $query->row('entry_id');
     // -----------------------------------------
     // Load Location
     // -----------------------------------------
     $location_type = $settings['upload_location'];
     $location_class = 'CI_Location_' . $location_type;
     // Load Settings
     if (isset($settings['locations'][$location_type]) == FALSE) {
         $o['body'] = $this->EE->lang->line('ci:location_settings_failure');
         exit($this->EE->image_helper->generate_json($o));
     }
     $location_settings = $settings['locations'][$location_type];
     // Load Main Class
     if (class_exists('Image_Location') == FALSE) {
         require PATH_THIRD . 'channel_images/locations/image_location.php';
     }
     // Try to load Location Class
     if (class_exists($location_class) == FALSE) {
         $location_file = PATH_THIRD . 'channel_images/locations/' . $location_type . '/' . $location_type . '.php';
         if (file_exists($location_file) == FALSE) {
             $o['body'] = $this->EE->lang->line('ci:location_load_failure');
             exit($this->EE->image_helper->generate_json($o));
         }
         require $location_file;
     }
     // Init
     $LOC = new $location_class($location_settings);
     // Temp Dir
     @mkdir($this->EE->channel_images->cache_path . 'channel_images/', 0777);
     @mkdir($this->EE->channel_images->cache_path . 'channel_images/transfer/', 0777);
     @mkdir($this->EE->channel_images->cache_path . 'channel_images/transfer/' . $image_id . '/', 0777);
     $temp_dir = $this->EE->channel_images->cache_path . 'channel_images/transfer/' . $image_id . '/';
     // -----------------------------------------
     // Copy Image to temp location
     // -----------------------------------------
     $LOC->download_file($entry_id, $filename, $temp_dir);
     //if ($response !== TRUE) exit($response);
     foreach ($settings['action_groups'] as $group) {
         $size_name = $group['group_name'];
         $size_filename = str_replace($extension, "__{$size_name}{$extension}", $filename);
         $LOC->download_file($entry_id, $size_filename, $temp_dir);
     }
     // -----------------------------------------
     // Init Amazon
     // -----------------------------------------
     if ($_POST['transfer']['to'] == 's3') {
         if (class_exists('CFRuntime') == FALSE) {
             // Include the SDK
             require_once PATH_THIRD . 'channel_images/locations/s3/sdk/sdk.class.php';
         }
         // Just to be sure
         if (class_exists('AmazonS3') == FALSE) {
             include PATH_THIRD . 'channel_images/locations/s3/sdk/services/s3.class.php';
         }
         // Instantiate the AmazonS3 class
         $S3 = new AmazonS3(array('key' => trim($_POST['s3']['key']), 'secret' => trim($_POST['s3']['secret_key'])));
         $S3->ssl_verification = FALSE;
         // Init Configs
         $temp = $this->EE->config->item('ci_s3_storage');
         $s3_storage = constant('AmazonS3::' . $temp[$_POST['s3']['storage']]);
         $temp = $this->EE->config->item('ci_s3_acl');
         $s3_acl = constant('AmazonS3::' . $temp[$_POST['s3']['acl']]);
         $s3_directory = trim($_POST['s3']['directory']);
         $s3_bucket = $_POST['s3']['bucket'];
         $s3_subdir = '';
         if ($s3_directory) {
             $s3_subdir = $s3_directory . '/';
         }
         $s3_headers = $this->EE->config->item('ci_s3_headers');
         // Test it
         $resp = $S3->get_bucket_headers($s3_bucket);
         if (!$resp->isOK()) {
             if (isset($resp->body->Message)) {
                 exit('ERROR_S3: ' . $resp->body->Message);
             } else {
                 exit('ERROR_S3: Bucket error');
             }
         }
     } else {
         // Include the SDK
         if (class_exists('CF_Authentication') == FALSE) {
             require_once PATH_THIRD . 'channel_images/locations/cloudfiles/sdk/cloudfiles.php';
         }
         // Which Region?
         if ($_POST['cloudfiles']['region'] == 'uk') {
             $_POST['cloudfiles']['region'] = constant('UK_AUTHURL');
         } else {
             $_POST['cloudfiles']['region'] = constant('US_AUTHURL');
         }
         // Instantiate the Cloudfiles class
         $CF_AUTH = new CF_Authentication($_POST['cloudfiles']['username'], $_POST['cloudfiles']['api'], NULL, $_POST['cloudfiles']['region']);
         try {
             $CF_AUTH->ssl_use_cabundle();
             $CF_AUTH->authenticate();
         } catch (AuthenticationException $e) {
             exit('ERROR_CLOUDFILES:' . $e->getMessage());
         }
         $CF_CONN = new CF_Connection($CF_AUTH);
         $CF_CONN->ssl_use_cabundle();
         $CF_CONT = $CF_CONN->get_container($_POST['cloudfiles']['container']);
     }
     // -----------------------------------------
     // Loop over all dirs
     // -----------------------------------------
     $files = scandir($temp_dir);
     foreach ($files as $file) {
         $full_path = $temp_dir . $file;
         if (is_file($full_path) == false) {
             continue;
         }
         $extension = substr(strrchr($file, '.'), 1);
         // Mime type
         if ($extension == 'jpg') {
             $filemime = 'image/jpeg';
         } elseif ($extension == 'jpeg') {
             $filemime = 'image/jpeg';
         } elseif ($extension == 'png') {
             $filemime = 'image/png';
         } elseif ($extension == 'gif') {
             $filemime = 'image/gif';
         } else {
             continue;
         }
         if (isset($S3) == true) {
             $upload_arr = array();
             $upload_arr['fileUpload'] = $full_path;
             $upload_arr['contentType'] = $filemime;
             $upload_arr['acl'] = $s3_acl;
             $upload_arr['storage'] = $s3_storage;
             $upload_arr['headers'] = array();
             if ($s3_headers != FALSE && is_array($s3_headers) === TRUE) {
                 $upload_arr['headers'] = $s3_headers;
             }
             $response = $S3->create_object($s3_bucket, $s3_subdir . $entry_id . '/' . $file, $upload_arr);
             // Success?
             if (!$response->isOK()) {
                 exit((string) $response->body->Message);
             }
         } else {
             $OBJECT = $CF_CONT->create_object($entry_id . '/' . $file);
             $OBJECT->content_type = $filemime;
             try {
                 $OBJECT->load_from_filename($full_path);
             } catch (Exception $e) {
                 exit($e->getMessage());
             }
         }
         //@unlink($temp_dir.$file);
     }
     @delete_files($temp_dir, true);
     @rmdir($temp_dir);
     $o = array('success' => 'yes');
     exit($this->EE->image_helper->generate_json($o));
 }