View.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. public function http($file)
  23. {
  24. $base = Dever::data();
  25. $host = Dever::host() . 'data/';
  26. if (strstr($file, $base)) {
  27. return str_replace($base, $host, $file);
  28. } else {
  29. $local = Dever::file('tmp/' . md5($file));
  30. file_put_contents($local, file_get_contents($file), LOCK_EX);
  31. return str_replace($base, $host, $local);
  32. }
  33. }
  34. # 从内容中解析文件
  35. public function file($content, $domain, $local = false)
  36. {
  37. $content = preg_replace_callback('/[0-9a-zA-Z\-\\/]+(\.jpeg|\.jpg|\.png|\.gif|\.mp3|\.mp4|\.aov|\.m4a)/i', function($matches) use($domain, $local)
  38. {
  39. $file = $matches[0];
  40. $file = ltrim($file, '/');
  41. if (!strstr($file, 'http')) {
  42. $file = $domain . $file;
  43. }
  44. if ($local) {
  45. $upload = Dever::load('save', 'upload')->act($local, $file);
  46. if ($upload && isset($upload['url'])) {
  47. $file = $upload['url'];
  48. }
  49. }
  50. return $file;
  51. }, $content);
  52. return $content;
  53. }
  54. }