123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php namespace Upload\Lib;
- use Dever;
- class View
- {
- private $config = array
- (
- 1 => 'Local',
- 2 => 'Qiniu',
- 3 => 'Oss',
- );
- 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;
- }
- }
|