123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace plugins\qiniu\lib;
- use Qiniu\Auth;
- use Qiniu\Storage\UploadManager;
- class Qiniu
- {
- private $config;
- private $storageRoot;
-
- private $plugin;
-
- public function __construct($config)
- {
- $pluginClass = cmf_get_plugin_class('Qiniu');
- $this->plugin = new $pluginClass();
- $this->config = $this->plugin->getConfig();
- $this->storageRoot = $this->config['protocol'] . '://' . $this->config['domain'] . '/';
- }
-
- public function upload($file, $filePath, $fileType = 'image', $param = null)
- {
- $accessKey = $this->config['accessKey'];
- $secretKey = $this->config['secretKey'];
- $watermark = empty($this->config['styles_watermark']) ? 'watermark' : $this->config['styles_watermark'];
- $upManager = new UploadManager();
- $auth = new Auth($accessKey, $secretKey);
- $token = $auth->uploadToken($this->config['bucket']);
- $result = $upManager->putFile($token, $file, $filePath);
- $previewUrl = $fileType == 'image' ? $this->getPreviewUrl($file, $watermark) : $this->getFileDownloadUrl($file);
- $url = $fileType == 'image' ? $this->getImageUrl($file, $watermark) : $this->getFileDownloadUrl($file);
- return [
- 'preview_url' => $previewUrl,
- 'url' => $url,
- ];
- }
-
- public function getPreviewUrl($file, $style = 'watermark')
- {
- $url = $this->getUrl($file, $style);
- return $url;
- }
-
- public function getImageUrl($file, $style = 'watermark')
- {
- $config = $this->config;
- $url = $this->storageRoot . $file;
- if (!empty($style)) {
-
- }
- return $url;
- }
-
- public function getUrl($file, $style = '')
- {
- $config = $this->config;
- $url = $this->storageRoot . $file;
- if (!empty($style)) {
-
- }
- return $url;
- }
-
- public function getFileDownloadUrl($file, $expires = 3600)
- {
- $accessKey = $this->config['accessKey'];
- $secretKey = $this->config['secretKey'];
- $auth = new Auth($accessKey, $secretKey);
- $url = $this->getUrl($file);
- $filename = db('asset')->where('file_path', $file)->value('filename');
- $url = $auth->privateDownloadUrl($url, $expires);
- if (!empty($filename)) {
- $url .= '&attname=' . urlencode($filename);
- }
- return $url;
- }
-
- public function getDomain()
- {
- return $this->config['domain'];
- }
-
- public function getFilePath($url)
- {
- $parsedUrl = parse_url($url);
- if (!empty($parsedUrl['path'])) {
- $url = ltrim($parsedUrl['path'], '/\\');
- $config = $this->config;
-
- $styleSeparator = '!';
- $styleSeparatorPosition = strpos($url, $styleSeparator);
- if ($styleSeparatorPosition !== false) {
- $url = substr($url, 0, strpos($url, $styleSeparator));
- }
- } else {
- $url = '';
- }
- return $url;
- }
-
- public function gettoken(){
- $config = $this->config;
- $accessKey = $config['accessKey'];
- $secretKey = $config['secretKey'];
- $domain = $config['domain'];
-
- $auth = new Auth($accessKey, $secretKey);
- $token = $auth->uploadToken($config['bucket']);
- $data=[
- 'token'=>$token,
- 'domain'=>$domain,
- ];
-
- return $data;
- }
- }
|