1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php namespace Upload\Lib;
- use Dever;
- class View
- {
- private $config = array
- (
- 1 => 'Local',
- 2 => 'Qiniu',
- 3 => 'Oss',
- );
- public function getUrl($info)
- {
- if (strstr($info['file'], 'http')) {
- return $info['file'];
- }
- $save = Dever::db('save', 'upload')->find($info['save_id']);
- if ($save['host']) {
- $host = $save['host'];
- if (strstr($host, '{host}')) {
- $host = str_replace('{host}', Dever::host(), $host);
- }
- } else {
- $host = Dever::host() . 'data/' . DEVER_PROJECT . '/upload/';
- }
- return $host . $info['file'];
- }
- public function local($file)
- {
- $host = Dever::host() . 'data/';
- if (strstr($file, $host)) {
- return str_replace($host, Dever::data(), $file);
- } else {
- $local = Dever::file('tmp/' . md5($file));
- file_put_contents($local, file_get_contents($file), LOCK_EX);
- return $local;
- }
- }
- public function http($file)
- {
- $base = Dever::data();
- $host = Dever::host() . 'data/';
- if (strstr($file, $base)) {
- return str_replace($base, $host, $file);
- } else {
- $local = Dever::file('tmp/' . md5($file));
- file_put_contents($local, file_get_contents($file), LOCK_EX);
- return str_replace($base, $host, $local);
- }
- }
- # 从内容中解析文件
- public function file($content, $domain, $local = false)
- {
- $content = preg_replace_callback('/[0-9a-zA-Z\-\\/]+(\.jpeg|\.jpg|\.png|\.gif|\.mp3|\.mp4|\.aov|\.m4a)/i', function($matches) use($domain, $local)
- {
- $file = $matches[0];
- $file = ltrim($file, '/');
- if (!strstr($file, 'http')) {
- $file = $domain . $file;
- }
- if ($local) {
- $upload = Dever::load('save', 'upload')->act($local, $file);
- if ($upload && isset($upload['url'])) {
- $file = $upload['url'];
- }
- }
- return $file;
- }, $content);
- return $content;
- }
- }
|