Handle.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  31. $data['host'] = $config['yun']['host'];
  32. $this->yun = new $class($config, $data);
  33. }
  34. if ($this->save_type <= 3) {
  35. $this->local = new Local($config, $data);
  36. }
  37. }
  38. # 复制文件到服务器上
  39. public function copy()
  40. {
  41. return $this->get();
  42. }
  43. # 获取文件名
  44. public function name()
  45. {
  46. return $this->get('getName');
  47. }
  48. # 手动裁图
  49. public function cropper()
  50. {
  51. if ($this->local) {
  52. $this->local->cropper();
  53. }
  54. if ($this->yun) {
  55. $this->yun->cropper();
  56. }
  57. }
  58. # 获取文件名
  59. public function get($type = 'copy')
  60. {
  61. $yun = $local = array();
  62. if ($this->save_type <= 3) {
  63. $local = $this->local->$type();
  64. }
  65. if ($local && $local['status'] == -1) {
  66. return $local;
  67. }
  68. if ($this->save_type >= 2) {
  69. $yun = $this->yun->$type();
  70. }
  71. if ($type == 'copy') {
  72. if ($this->local) {
  73. $this->local->delete();
  74. } elseif ($this->yun) {
  75. $this->yun->delete();
  76. }
  77. }
  78. if ($this->save_type <= 2) {
  79. return $local;
  80. } elseif ($this->save_type > 2) {
  81. return $yun;
  82. }
  83. }
  84. public function handle_t($id, $source, $dest, $path)
  85. {
  86. $yun = $local = array();
  87. if ($this->save_type <= 3) {
  88. $local = $this->local->handle_t($id, $source, $dest);
  89. }
  90. if ($local) {
  91. return $local;
  92. }
  93. if ($this->save_type >= 2) {
  94. $yun = $this->yun->handle_t($id, $source, $dest, $path);
  95. }
  96. if ($this->save_type <= 2) {
  97. return $local;
  98. } elseif ($this->save_type > 2) {
  99. return $yun;
  100. }
  101. }
  102. public function handle_c($id, $source, $dest, $path)
  103. {
  104. $yun = $local = array();
  105. if ($this->save_type <= 3) {
  106. $local = $this->local->handle_c($id, $source, $dest);
  107. }
  108. if ($local) {
  109. return $local;
  110. }
  111. if ($this->save_type >= 2) {
  112. $yun = $this->yun->handle_c($id, $source, $dest, $path);
  113. }
  114. if ($this->save_type <= 2) {
  115. return $local;
  116. } elseif ($this->save_type > 2) {
  117. return $yun;
  118. }
  119. }
  120. }