Local.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\lib\storage;
  12. class Local
  13. {
  14. private $config;
  15. /**
  16. * Local constructor.
  17. * @param $config
  18. */
  19. public function __construct($config)
  20. {
  21. $this->config = $config;
  22. }
  23. /**
  24. * 文件上传
  25. * @param string $file 上传文件路径
  26. * @param string $filePath 文件路径相对于upload目录
  27. * @param string $fileType 文件类型,image,video,audio,file
  28. * @param array $param 额外参数
  29. * @return mixed
  30. */
  31. public function upload($file, $filePath = '', $fileType = 'image', $param = null)
  32. {
  33. return [
  34. 'preview_url' => $this->getPreviewUrl($file),
  35. 'url' => $this->getUrl($file),
  36. ];
  37. }
  38. /**
  39. * 获取图片地址
  40. * @param string $file
  41. * @param string $style
  42. * @return mixed
  43. */
  44. public function getImageUrl($file, $style = '')
  45. {
  46. return $this->_getWebRoot() . '/upload/' . $file;
  47. }
  48. /**
  49. * 获取图片预览地址
  50. * @param string $file
  51. * @param string $style
  52. * @return mixed
  53. */
  54. public function getPreviewUrl($file, $style = '')
  55. {
  56. return $this->_getWebRoot() . '/upload/' . $file;
  57. }
  58. /**
  59. * 获取文件地址
  60. * @param string $file
  61. * @param string $style
  62. * @return mixed
  63. */
  64. public function getUrl($file, $style = '')
  65. {
  66. return $this->_getWebRoot() . '/upload/' . $file;
  67. }
  68. /**
  69. * 获取文件下载地址
  70. * @param string $file
  71. * @param int $expires
  72. * @return mixed
  73. */
  74. public function getFileDownloadUrl($file, $expires = 3600)
  75. {
  76. $url = $this->getUrl($file);
  77. return $url;
  78. }
  79. /**
  80. * 获取本地存储域名
  81. * @return mixed
  82. */
  83. public function getDomain()
  84. {
  85. return request()->host();
  86. }
  87. /**
  88. * 获取文件相对上传目录路径
  89. * @param string $url
  90. * @return mixed
  91. */
  92. public function getFilePath($url)
  93. {
  94. $storageDomain = $this->getDomain();
  95. $url = preg_replace("/^http(s)?:\/\/$storageDomain/", '', $url);
  96. $url = preg_replace("/^\/upload\//", '', $url);
  97. return $url;
  98. }
  99. private function _getWebRoot()
  100. {
  101. return STATICSITE;
  102. }
  103. }