View.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php namespace Upload\Lib;
  2. use Dever;
  3. class View
  4. {
  5. private $config = array
  6. (
  7. 1 => 'Local',
  8. 2 => 'Qiniu',
  9. 3 => 'Oss',
  10. );
  11. public function local($file)
  12. {
  13. $host = Dever::host() . 'data/';
  14. if (strstr($file, $host)) {
  15. return str_replace($host, Dever::data(), $file);
  16. } else {
  17. $local = Dever::file('tmp/' . md5($file));
  18. file_put_contents($local, file_get_contents($file), LOCK_EX);
  19. return $local;
  20. }
  21. }
  22. # 从内容中解析文件
  23. public function file($content, $domain, $local = false)
  24. {
  25. $content = preg_replace_callback('/[0-9a-zA-Z\-\\/]+(\.jpeg|\.jpg|\.png|\.gif|\.mp3|\.mp4|\.aov|\.m4a)/i', function($matches) use($domain, $local)
  26. {
  27. $file = $matches[0];
  28. $file = ltrim($file, '/');
  29. if (!strstr($file, 'http')) {
  30. $file = $domain . $file;
  31. }
  32. if ($local) {
  33. $upload = Dever::load('save', 'upload')->act($local, $file);
  34. if ($upload && isset($upload['url'])) {
  35. $file = $upload['url'];
  36. }
  37. }
  38. return $file;
  39. }, $content);
  40. return $content;
  41. }
  42. }