View.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 getUrl($info)
  12. {
  13. if (strstr($info['file'], 'http')) {
  14. return $info['file'];
  15. }
  16. $save = Dever::db('save', 'upload')->find($info['save_id']);
  17. if ($save['host']) {
  18. $host = $save['host'];
  19. if (strstr($host, '{host}')) {
  20. $host = str_replace('{host}', Dever::host(), $host);
  21. }
  22. } else {
  23. $host = Dever::host() . 'data/' . DEVER_PROJECT . '/upload/';
  24. }
  25. return $host . $info['file'];
  26. }
  27. public function local($file)
  28. {
  29. $host = Dever::host() . 'data/';
  30. if (strstr($file, $host)) {
  31. return str_replace($host, Dever::data(), $file);
  32. } else {
  33. $local = Dever::file('tmp/' . md5($file));
  34. file_put_contents($local, file_get_contents($file), LOCK_EX);
  35. return $local;
  36. }
  37. }
  38. public function http($file)
  39. {
  40. $base = Dever::data();
  41. $host = Dever::host() . 'data/';
  42. if (strstr($file, $base)) {
  43. return str_replace($base, $host, $file);
  44. } else {
  45. $local = Dever::file('tmp/' . md5($file));
  46. file_put_contents($local, file_get_contents($file), LOCK_EX);
  47. return str_replace($base, $host, $local);
  48. }
  49. }
  50. # 从内容中解析文件
  51. public function file($content, $domain, $local = false)
  52. {
  53. $content = preg_replace_callback('/[0-9a-zA-Z\-\\/]+(\.jpeg|\.jpg|\.png|\.gif|\.mp3|\.mp4|\.aov|\.m4a)/i', function($matches) use($domain, $local)
  54. {
  55. $file = $matches[0];
  56. $file = ltrim($file, '/');
  57. if (!strstr($file, 'http')) {
  58. $file = $domain . $file;
  59. }
  60. if ($local) {
  61. $upload = Dever::load('save', 'upload')->act($local, $file);
  62. if ($upload && isset($upload['url'])) {
  63. $file = $upload['url'];
  64. }
  65. }
  66. return $file;
  67. }, $content);
  68. return $content;
  69. }
  70. }