/** * Upload a file into the folder with the id. * * @param int $folder_id * @param string $local_path if set, will simulate an upload from local path * @param string $file_name filename to use for simulated uploads * * @throws Exception * @return array|mixed */ public function upload_file($folder_id, $local_path = "", $file_name = "") { try { $folder_row = $this->EE->assets_lib->get_folder_row_by_id($folder_id); } catch (Exception $error) { return array('error' => $error->getMessage()); } $server_path = $this->get_folder_server_path($folder_row->full_path, $folder_row); if (!$server_path) { return array('error' => lang('invalid_filedir_path')); } if (empty($local_path)) { // upload the file and drop it in the temporary folder $uploader = new qqFileUploader(); // make sure a file was uploaded if (!$uploader->file) { return array('error' => lang('no_files')); } $size = $uploader->file->getSize(); // make sure the file isn't empty if (!$size) { return array('error' => lang('empty_file')); } $file_name = $uploader->file->getName(); if (!Assets_helper::is_allowed_file_name($file_name)) { throw new Exception(lang('invalid_file_name')); } $file_path = Assets_helper::get_temp_file(pathinfo($file_name, PATHINFO_EXTENSION)); $uploader->file->save($file_path); } else { if (!Assets_helper::is_allowed_file_name($file_name)) { throw new Exception(lang('invalid_file_name')); } $file_path = $local_path; } // the file is being saved in a temporary location, so that the workflow here is manageable // if we didn't do this, that would mean that all sources must implement their own uploader as well // which would have been an overkill. $result = $this->_do_upload_in_folder($folder_row, $file_path, $file_name); $return_prompt = FALSE; // naming conflict. create the new filename and ask user what to do if (isset($result['prompt'])) { $new_file_name = $this->get_name_replacement($folder_row, $uploader->file->getName()); $return_prompt = $result; $result = $this->_do_upload_in_folder($folder_row, $file_path, $new_file_name); } if (isset($result['success'])) { $filename = pathinfo($result['path'], PATHINFO_BASENAME); $data = array('folder_id' => $folder_id, 'source_type' => $folder_row->source_type, 'source_id' => $folder_row->source_id, 'filedir_id' => $folder_row->filedir_id, 'file_name' => $filename, 'kind' => Assets_helper::get_kind($filename)); $this->EE->db->insert('assets_files', $data); $file_id = $this->EE->db->insert_id(); $file = $this->get_file($file_id); // For EE files, the dimensions might have changed due to filedir restrictions, so we have to use the returned path if ($file instanceof Assets_ee_file) { $this->update_file_info($file, $result['path']); } else { $this->update_file_info($file, $file_path); } @unlink($file_path); $this->EE->assets_lib->update_file_search_keywords($file_id); if (!$return_prompt) { return array('success' => TRUE, 'file_id' => $file_id); } else { $return_prompt['additional_info'] = $folder_id . ':' . $file_id; $return_prompt['new_file_id'] = $file_id; return $return_prompt; } } else { @unlink($file_path); return $result; } }
/** * Returns a local copy of the file * * @return mixed * @throws Exception */ public function get_local_copy() { $location = Assets_helper::get_temp_file(); $prefix = !empty($this->_source_settings->subfolder) ? rtrim($this->_source_settings->subfolder, '/') . '/' : ''; $this->source->s3->setEndpoint($this->source->get_endpoint_by_location($this->_source_settings->location)); @$this->source->s3->getObject($this->_source_settings->bucket, $prefix . $this->subpath, $location); if (!filesize($location)) { throw new Exception(lang('couldnt_download')); } return $location; }
/** * Perform image actions - resize and save dimensions. If no bucket name provided, $uri is treated as filesystem path * * @param $uri * @param $file_id * @param mixed $bucket false for uri to be treated as a filesystem path * @return bool */ private function _perform_image_actions($uri, $file_id, $bucket = FALSE) { $this->EE->load->library('filemanager'); $cache_path = 'assets/s3_sources'; $cache_path = Assets_helper::ensure_cache_path($cache_path); if ($bucket) { $target = Assets_helper::get_temp_file(); $this->_s3_set_creds($this->settings()->access_key_id, $this->settings()->secret_access_key); $this->s3->getObject($bucket, $this->_get_path_prefix() . $uri, $target); $uri = $target; } list($width, $height) = getimagesize($uri); $data = array('width' => $width, 'height' => $height); $this->_update_file($data, $file_id); if (strtolower($this->EE->config->item('assets_cache_remote_images')) !== "no") { $target_path = $cache_path . $file_id . '.jpg'; $this->EE->assets_lib->resize_image($uri, $target_path, self::IMAGE_SOURCE_SIZE); } @unlink($uri); }
/** * Perform image actions - resize and save dimensions. If no bucket name provided, $uri is treated as filesystem path * * @param $uri * @param $file_id * @param mixed $download_copy if set to true will download a new copy * @return bool */ private function _perform_image_actions($uri, $file_id, $download_copy = null) { $this->EE->load->library('filemanager'); $cache_path = 'assets/rs_sources'; $cache_path = Assets_helper::ensure_cache_path($cache_path); if ($download_copy) { $target = Assets_helper::get_temp_file(); $this->download_file($uri, $target); $uri = $target; } list($width, $height) = getimagesize($uri); $data = array('width' => $width, 'height' => $height); $this->_update_file($data, $file_id); if (strtolower($this->EE->config->item('assets_cache_remote_images')) !== "no") { $target_path = $cache_path . $file_id . '.jpg'; $this->EE->assets_lib->resize_image($uri, $target_path, self::IMAGE_SOURCE_SIZE); } @unlink($uri); }
/** * Returns a local copy of the file * * @return mixed */ public function get_local_copy() { $location = Assets_helper::get_temp_file(); $this->source->download_file($this->subpath, $location); return $location; }
/** * Returns a local copy of the file * * @return mixed */ public function get_local_copy() { $location = Assets_helper::get_temp_file(); copy($this->server_path, $location); clearstatcache(); return $location; }