public static function move_from_file_tmp(Model_FileTmp $file_tmp, $new_filename_prefix = '', $upload_type = 'img') { $file = static::forge(); $file->name = $file_tmp->name; if ($new_filename_prefix) { $file->name = Site_Upload::change_filename_prefix($file_tmp->name, $new_filename_prefix); } $file->filesize = $file_tmp->filesize; $file->original_filename = $file_tmp->original_filename; $file->type = $file_tmp->type; $file->member_id = $file_tmp->member_id; $file->user_type = $file_tmp->user_type; if (!is_null($file_tmp->exif)) { $file->exif = $file_tmp->exif; } if (!empty($file_tmp->shot_at)) { $file->shot_at = $file_tmp->shot_at; } $file->save(); if (conf('upload.storageType') == 'db') { $file_bin = Model_FileBin::get4name($file_tmp->name); $file_bin->name = $file->name; $file_bin->save(); } elseif (conf('upload.storageType') == 'S3') { Site_S3::move($file_tmp->name, $file->name, $upload_type); } $file_tmp->delete(); return $file; }
protected static function set_s3_instanse() { if (self::$s3_instantse) { return; } if (!(self::$s3_instantse = S3Client::factory(array('key' => FBD_AWS_ACCESS_KEY, 'secret' => FBD_AWS_SECRET_KEY)))) { throw new FuelException('S3Client factory failed.'); } }
protected function delete_stored_file(\Orm\Model $obj) { if (conf('upload.storageType') == 'normal') { return; } if ($this->_is_tmp && \Model_File::get4name($obj->name)) { return; } if (conf('upload.storageType') == 'S3') { return \Site_S3::delete($obj->name, 'img') && \Site_S3::delete($obj->name, 'file'); } if (!($file_bin = \Model_FileBin::get4name($obj->name))) { return; } return $file_bin->delete(); }
protected function save_file_bin($tmp_file_path) { switch ($this->options['storage_type']) { case 'db': $storage_save_result = (bool) Model_FileBin::save_from_file_path($tmp_file_path, $this->file->name); break; case 'S3': $storage_save_result = (bool) Site_S3::save($tmp_file_path, null, $this->options['upload_type']); break; case 'normal': default: $storage_save_result = true; break; } if (!$storage_save_result) { Util_File::remove($tmp_file_path); throw new FuelException('Save raw file error to storage.'); } if (!($result = (bool) Util_file::move($tmp_file_path, $this->file->file_path))) { throw new FuelException('Save raw file error.'); } return $storage_save_result && $result; }
public static function get_bin_from_storage($filename, $strage_type = null, $upload_type = 'img') { if (!$strage_type) { $strage_type = conf('upload.storageType'); } if (!in_array($strage_type, array('db', 'S3'))) { throw new InvalidArgumentException('Second parameter is invalid.'); } return $strage_type == 'db' ? Model_FileBin::get_bin4name($filename) : file_get_contents(Site_S3::get_url($filename, $upload_type)); }
protected function delete_file($filename, $storage_type = 'normal', $is_delete_raw_file_only = false, $is_delete_with_storage_data = true) { $filename_excluded_prefix = str_replace($this->options['filename_prefix'], '', $filename); $file_path = $this->get_upload_path($filename_excluded_prefix); $success = is_file($file_path) && unlink($file_path); if ($success && !$is_delete_raw_file_only) { foreach ($this->options['image_versions'] as $version => $options) { if (!empty($version)) { $varsion_file = $this->get_upload_path($filename_excluded_prefix, $version); if (is_file($varsion_file)) { unlink($varsion_file); } } } } if (!$is_delete_with_storage_data) { return $success; } if ($storage_type == 'db') { if ($file_bin = Model_FileBin::get4name($filename)) { $success = (bool) $file_bin->delete(); } } elseif ($storage_type == 'S3') { $success = (bool) Site_S3::delete($filename, $this->options['upload_type']); } return $success; }