123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- class UCloud_Engine_Oss {
-
- private $rootPath;
- private $fileType;
-
- private $error = '';
- private $config = array(
- 'access_id' => '',
- 'access_key' => '',
- 'bucket' => '',
- 'host' => '',
- 'timeout' => 90,
- );
-
- public function __construct($root, $config) {
- $new_config = array();
- $new_config['access_id'] = $config['accessKey'];
- $new_config['access_key'] = $config['secretKey'];
- $new_config['bucket'] = $config['bucket'];
- $new_config['host'] = $config['api'];
- $new_config['timeout'] = $config['timeout'];
-
- $this->config = array_merge($this->config, $new_config);
-
- $this->rootPath = trim($root, './') . '/';
- }
-
- public function checkRootPath($rootpath) {
- return true;
- }
-
- public function checkSavePath($savepath) {
- return true;
- }
-
- public function mkdir($savepath) {
- return true;
- }
-
- public function save($file, $replace = true) {
- $this->fileType = $file['type'];
- $resource = fopen($file['tmp_name'], 'r');
-
- $file['savepath'] = preg_replace('/(.*?)(?:\/(?!\w)|$)/', '$1', $file['savepath']);
-
- $path = $this->rootPath . $file['savepath'] .'/'. $file['savename'];
- return $this->request($path, $resource);
- }
-
- private function request($path, $resource = null){
- $_headers = array('Content-Type: ' . $this->fileType);
- $uri = "/{$this->config['bucket']}{$path}";
- $date = gmdate('D, d M Y H:i:s \G\M\T');
- array_push($_headers, "Authorization: {$this->sign($uri, $date)}");
- array_push($_headers, "Date: {$date}");
- fseek($resource, 0, SEEK_END);
- $length = ftell($resource);
- fseek($resource, 0);
- array_push($_headers, "Content-Length: {$length}");
- $ch = curl_init('http://' . $this->config['host'] . $uri);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_INFILE, $resource);
- curl_setopt($ch, CURLOPT_INFILESIZE, $length);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
- curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
- $response = curl_exec($ch);
- $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- fclose($resource);
- if ($status == 200) {
- return true;
- } else {
- $this->error = $response;
- return false;
- }
- }
-
- public function getError() {
- return $this->error;
- }
-
- private function sign( $uri, $date){
- $sign_string = "PUT\n\n" . $this->fileType . "\n" . $date . "\n" . $uri;
- $sign = $this->hex_to_base64(hash_hmac('sha1', $sign_string, $this->config['access_key']));
- return 'OSS ' . $this->config['access_id'] . ':' . $sign;
- }
- private function hex_to_base64($str) {
- $result = '';
- for ($i = 0; $i < strlen($str); $i += 2) {
- $result .= chr(hexdec(substr($str, $i, 2)));
- }
- return base64_encode($result);
- }
- }
|