Handle.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. class Handle
  5. {
  6. /**
  7. * __construct
  8. *
  9. * @return mixed
  10. */
  11. public function __construct($data = array())
  12. {
  13. $this->yun = $this->local = false;
  14. $config = Dever::load('upload/lib/config')->get($data['key']);
  15. //$config = Dever::db('upload/upload')->one($data['key']);
  16. if (!$config) {
  17. Dever::alert('错误的上传配置');
  18. return;
  19. }
  20. if (isset($data['yun']) && $data['yun']) {
  21. $config['save_type'] = 4;
  22. $config['yun'] = $data['yun'];
  23. }
  24. $this->save_type = $config['save_type'];
  25. if ($this->save_type >= 2 && $config['yun']) {
  26. if ($config['yun']['type'] == 2) {
  27. $class = 'Upload\Lib\Store\Qiniu';
  28. } elseif ($config['yun']['type'] == 1) {
  29. $class = 'Upload\Lib\Store\Oss';
  30. } elseif ($config['yun']['type'] == 3) {
  31. $class = 'Upload\Lib\Store\Sina';
  32. }
  33. $data['host'] = $config['yun']['host'];
  34. $this->yun = new $class($config, $data);
  35. }
  36. if ($this->save_type <= 3) {
  37. $this->local = new Local($config, $data);
  38. }
  39. }
  40. # 复制文件到服务器上
  41. public function copy()
  42. {
  43. return $this->get();
  44. }
  45. # 获取文件名
  46. public function name()
  47. {
  48. return $this->get('getName');
  49. }
  50. # 手动裁图
  51. public function cropper()
  52. {
  53. if ($this->local) {
  54. $this->local->cropper();
  55. }
  56. if ($this->yun) {
  57. $this->yun->cropper();
  58. }
  59. }
  60. # 获取文件名
  61. public function get($type = 'copy')
  62. {
  63. $yun = $local = array();
  64. if ($this->save_type <= 3) {
  65. $local = $this->local->$type();
  66. }
  67. if ($local && $local['status'] == -1) {
  68. return $local;
  69. }
  70. if ($this->save_type >= 2) {
  71. $yun = $this->yun->$type();
  72. }
  73. if ($type == 'copy') {
  74. if ($this->local) {
  75. $this->local->delete();
  76. } elseif ($this->yun) {
  77. $this->yun->delete();
  78. }
  79. }
  80. if ($this->save_type <= 2) {
  81. return $local;
  82. } elseif ($this->save_type > 2) {
  83. return $yun;
  84. }
  85. }
  86. public function handle_t($id, $source, $dest, $path)
  87. {
  88. $yun = $local = array();
  89. if ($this->save_type <= 3) {
  90. $local = $this->local->handle_t($id, $source, $dest);
  91. }
  92. if ($local) {
  93. return $local;
  94. }
  95. if ($this->save_type >= 2) {
  96. $yun = $this->yun->handle_t($id, $source, $dest, $path);
  97. }
  98. if ($this->save_type <= 2) {
  99. return $local;
  100. } elseif ($this->save_type > 2) {
  101. return $yun;
  102. }
  103. }
  104. public function handle_c($id, $source, $dest, $path)
  105. {
  106. $yun = $local = array();
  107. if ($this->save_type <= 3) {
  108. $local = $this->local->handle_c($id, $source, $dest);
  109. }
  110. if ($local) {
  111. return $local;
  112. }
  113. if ($this->save_type >= 2) {
  114. $yun = $this->yun->handle_c($id, $source, $dest, $path);
  115. }
  116. if ($this->save_type <= 2) {
  117. return $local;
  118. } elseif ($this->save_type > 2) {
  119. return $yun;
  120. }
  121. }
  122. }