Exemplo n.º 1
0
 public function save($uploaded_file_path = null)
 {
     try {
         if (!$uploaded_file_path) {
             $uploaded_file_path = $this->upload_file();
         }
         $this->set_file_properties($uploaded_file_path);
         if (!Site_Upload::check_and_make_uploaded_dir($this->options['upload_dir'], null, $this->options['path_chmod'])) {
             throw new FuelException('ディレクトリの作成に失敗しました。');
         }
         $tmp_file_path = APPPATH . 'tmp/' . $this->file->name;
         if (!Util_file::move($uploaded_file_path, $tmp_file_path)) {
             throw new FuelException('Save raw file error.');
         }
         // exif データの取得
         $exif = array();
         if ($this->options['is_save_exif_to_db'] && $this->file->type == 'image/jpeg') {
             $exif = \Util_Exif::get_exif($tmp_file_path, $this->options['exif_accept_tags'], $this->options['exif_ignore_tags']);
         }
         $is_resaved = false;
         // 回転状態の補正
         if ($this->options['auto_orient'] && !empty($exif['Orientation'])) {
             Util_file::correct_orientation($tmp_file_path, $exif['Orientation']);
             $is_resaved = true;
         }
         // 大きすぎる場合はリサイズ & 保存ファイルから exif 情報削除
         $file_size_before = $this->file->size;
         if ($max_size = Site_Upload::get_accepted_max_size($this->options['member_id'])) {
             $this->file->size = Site_Upload::check_max_size_and_resize($tmp_file_path, $max_size);
             $is_resaved = $this->file->size != $file_size_before;
         }
         if ($this->options['is_clear_exif_on_file'] && !$is_resaved) {
             Site_Upload::clear_exif($tmp_file_path);
             $this->file->size = File::get_size($tmp_file_path);
         }
         $this->save_model_file($exif);
         $this->save_file_bin($tmp_file_path);
     } catch (\FuelException $e) {
         if (isset($this->file->file_path) && file_exists($this->file->file_path)) {
             Util_file::remove($this->file->file_path);
             $this->file->size = filesize($this->file->file_path);
         }
         $this->file->error = $e->getMessage();
     }
     return $this->file;
 }
Exemplo n.º 2
0
 public static function save_image_from_url($image_url, $save_file_path, $max_size = 0, $old_file_path = null)
 {
     if (!($data = file_get_contents($image_url))) {
         throw new FuelException('Get image from url failed.');
     }
     if (!file_put_contents($save_file_path, $data)) {
         throw new FuelException('Failed to save image.');
     }
     unset($data);
     if ($max_size) {
         Site_Upload::check_max_size_and_resize($save_file_path, $max_size);
     }
     // if exists old_file_path, compare data. if data is same, delete new file and return false;
     if ($old_file_path) {
         $new_data = file_get_contents($save_file_path);
         $old_data = file_get_contents($old_file_path);
         if ($new_data == $old_data) {
             return false;
         }
     }
     unset($new_data, $old_data);
     return true;
 }
Exemplo n.º 3
0
 protected function handle_file_upload($uploaded_file, $original_name, $size, $type, $error, $index = null, $content_range = null)
 {
     $file = new \stdClass();
     $file->is_tmp = $this->options['is_tmp'];
     $file->original_name = $original_name;
     $file->size = $this->fix_integer_overflow(intval($size));
     $file->type = $type;
     if (!($extention = Util_file::check_file_type($uploaded_file, \Site_Upload::get_accept_format($this->options['upload_type']), $type, $this->options['upload_type']))) {
         $file->error = $this->get_error_message('accept_file_types');
         return $file;
     }
     if (!($filename_with_prefix = Site_Upload::make_unique_filename($extention, $this->options['filename_prefix'], $original_name))) {
         $file->error = 'ファイル名の作成に失敗しました。';
         return $file;
     }
     $file->name = $this->remove_filename_prefix($filename_with_prefix);
     $file->name_prefix = $this->options['filename_prefix'];
     if (!\Site_Upload::check_and_make_uploaded_dir($this->options['upload_dir'], null, $this->options['mkdir_mode'])) {
         $file->error = 'ディレクトリの作成に失敗しました。';
         return $file;
     }
     if (!$this->validate($uploaded_file, $file, $error, $index)) {
         return $file;
     }
     if ($this->options['upload_type'] == 'img') {
         $file->thumbnail_uri = $this->options['image_versions']['thumbnail']['upload_url'] . $file->name;
     }
     $this->handle_form_data($file, $index);
     $upload_dir = $this->get_upload_path();
     $file_path = $this->get_upload_path($file->name);
     $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path);
     if ($uploaded_file && is_uploaded_file($uploaded_file)) {
         // multipart/formdata uploads (POST method uploads)
         if ($append_file) {
             file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND);
         } else {
             $res = move_uploaded_file($uploaded_file, $file_path);
         }
     } else {
         // Non-multipart uploads (PUT method support)
         file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0);
     }
     $file_size = $this->get_file_size($file_path, $append_file);
     if ($file_size === $file->size) {
         $file->url = $this->get_download_url($file->name);
         if ($this->is_valid_image_file($file_path)) {
             $this->handle_image_file($file_path, $file);
         }
     } else {
         $file->size = $file_size;
         if (!$content_range && $this->options['discard_aborted_uploads']) {
             $this->delete_file($filename_with_prefix, $this->options['storage_type']);
             $file->error = 'abort';
         }
     }
     $this->set_additional_file_properties($file);
     // exif データの取得
     $exif = array();
     if ($this->options['is_save_exif_to_db'] && $extention == 'jpg') {
         $exif = \Util_Exif::get_exif($file_path, $this->options['exif_accept_tags'], $this->options['exif_ignore_tags']);
     }
     if ($this->options['upload_type'] == 'img') {
         // 大きすぎる場合はリサイズ & 保存ファイルから exif 情報削除
         $file_size_before = $file->size;
         if ($this->options['member_id'] && $this->options['user_type'] === 0 && ($max_size = Site_Upload::get_accepted_max_size($this->options['member_id']))) {
             $file->size = Site_Upload::check_max_size_and_resize($file_path, $max_size);
         }
         // Exif情報の削除
         $is_resaved = $file->size != $file_size_before;
         if ($this->options['is_clear_exif_on_file'] && !$is_resaved) {
             Site_Upload::clear_exif($file_path);
             $file->size = File::get_size($file_path);
         }
         if (!empty($this->options['accept_sizes'])) {
             $file->accept_sizes = $this->options['accept_sizes'];
         }
     }
     try {
         if ($this->options['storage_type'] != 'normal') {
             $this->save_file2storage($file_path, $filename_with_prefix);
             $this->delete_file($filename_with_prefix, $this->options['storage_type'], false, false);
         }
         $file->id = $this->save_file($file, $exif);
     } catch (\Exception $e) {
         if ($this->options['is_output_log_save_error']) {
             \Util_Toolkit::log_error(sprintf('file save error: %s', $e->getMessage()));
         }
         $this->delete_file($filename_with_prefix, $this->options['storage_type']);
         $file->error = 'ファイルの保存に失敗しました。';
     }
     return $file;
 }