WebUploader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form;
  4. use Illuminate\Support\Facades\URL;
  5. use Illuminate\Support\Str;
  6. /**
  7. * @property Form $form
  8. */
  9. trait WebUploader
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $options = [];
  15. /**
  16. * @param string $extensions exp. gif,jpg,jpeg,bmp,png
  17. * @param string|null $mimeTypes exp. image/*
  18. *
  19. * @return $this
  20. */
  21. public function accept(string $extensions, string $mimeTypes = null)
  22. {
  23. $this->options['accept'] = [
  24. 'extensions' => $extensions,
  25. ];
  26. if ($mimeTypes !== null) {
  27. $this->options['accept']['mimeTypes'] = $mimeTypes;
  28. }
  29. return $this;
  30. }
  31. /**
  32. * @param bool $disable
  33. *
  34. * @return $this
  35. */
  36. public function disableChunked(bool $disable = true)
  37. {
  38. $this->options['chunked'] = ! $disable;
  39. return $this;
  40. }
  41. /**
  42. * @param int|null $size kb
  43. *
  44. * @return $this
  45. */
  46. public function chunkSize(int $size)
  47. {
  48. $this->options['chunkSize'] = $size * 1024;
  49. return $this;
  50. }
  51. /**
  52. * @param int $size kb
  53. *
  54. * @return $this
  55. */
  56. public function maxSize(int $size)
  57. {
  58. $this->rules('max:'.$size);
  59. $this->options['fileSingleSizeLimit'] = $size * 1024;
  60. return $this;
  61. }
  62. /**
  63. * @param int $num
  64. *
  65. * @return $this
  66. */
  67. public function threads(int $num)
  68. {
  69. $this->options['threads'] = $num;
  70. return $this;
  71. }
  72. /**
  73. * Set upload server.
  74. *
  75. * @param string $server
  76. *
  77. * @return $this
  78. */
  79. public function url(string $server)
  80. {
  81. $this->options['server'] = admin_url($server);
  82. return $this;
  83. }
  84. /**
  85. * Disable auto save the path of uploaded file.
  86. *
  87. * @return $this
  88. */
  89. public function disableAutoSave()
  90. {
  91. $this->options['autoUpdateColumn'] = false;
  92. return $this;
  93. }
  94. /**
  95. * Disable remove file.
  96. *
  97. * @return $this
  98. */
  99. public function disableRemove()
  100. {
  101. $this->options['disableRemove'] = true;
  102. return $this;
  103. }
  104. /**
  105. * Set upload server.
  106. *
  107. * @param string $server
  108. *
  109. * @return $this
  110. */
  111. public function deleteUrl(string $server)
  112. {
  113. $this->options['deleteUrl'] = admin_url($server);
  114. return $this;
  115. }
  116. /**
  117. * Set default options form file field.
  118. *
  119. * @return void
  120. */
  121. protected function setupDefaultOptions()
  122. {
  123. $defaultOptions = [
  124. 'isImage' => false,
  125. 'disableRemove' => false,
  126. 'chunked' => true,
  127. 'fileNumLimit' => 10,
  128. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  129. 'disableGlobalDnd' => true,
  130. 'fileSizeLimit' => 20971520000, // 20000M
  131. 'fileSingleSizeLimit' => 10485760, // 10M
  132. 'autoUpdateColumn' => true, // 上传完图片后自动保存图片路径
  133. 'elementName' => $this->elementName(), // 字段name属性值
  134. 'lang' => trans('admin.uploader'),
  135. 'deleteData' => [
  136. static::FILE_DELETE_FLAG => '',
  137. '_token' => csrf_token(),
  138. '_method' => 'PUT',
  139. ],
  140. 'formData' => [
  141. '_id' => Str::random(),
  142. 'upload_column' => $this->column,
  143. '_method' => 'PUT',
  144. '_token' => csrf_token(),
  145. ],
  146. ];
  147. $this->options($defaultOptions);
  148. }
  149. protected function setDefaultServer()
  150. {
  151. if (! $this->form || ! method_exists($this->form, 'action')) {
  152. return;
  153. }
  154. if (empty($this->options['server'])) {
  155. $this->options['server'] = $this->form->action();
  156. }
  157. if (empty($this->options['deleteUrl'])) {
  158. $this->options['deleteUrl'] = $this->form->action();
  159. }
  160. if (
  161. method_exists($this->form, 'builder')
  162. && $this->form->builder()
  163. && $this->form->builder()->isCreating()
  164. ) {
  165. unset(
  166. $this->options['formData']['_method'],
  167. $this->options['deleteData']['_method'],
  168. $this->options['autoUpdateColumn']
  169. );
  170. }
  171. }
  172. /**
  173. * Get create url.
  174. *
  175. * @return string
  176. */
  177. public function getCreateUrl()
  178. {
  179. return str_replace('/create', '', URL::full());
  180. }
  181. /**
  182. * Set preview options form image field.
  183. *
  184. * @return void
  185. */
  186. protected function setupPreviewOptions()
  187. {
  188. $this->options['preview'] = $this->initialPreviewConfig();
  189. }
  190. /**
  191. * Set options for file-upload plugin.
  192. *
  193. * @param array $options
  194. *
  195. * @return $this
  196. */
  197. public function options($options = [])
  198. {
  199. $this->options = array_merge($options, $this->options);
  200. return $this;
  201. }
  202. }