WebUploader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Illuminate\Support\Facades\URL;
  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. $primaryKey = Admin::user() ? Admin::user()->getKey() : null;
  124. $defaultOptions = [
  125. 'isImage' => false,
  126. 'disableRemove' => false,
  127. 'chunked' => true,
  128. 'fileNumLimit' => 10,
  129. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  130. 'disableGlobalDnd' => true,
  131. 'fileSizeLimit' => 20971520000, // 20000M
  132. 'fileSingleSizeLimit' => 10485760, // 10M
  133. 'autoUpdateColumn' => true, // 上传完图片后自动保存图片路径
  134. 'elementName' => $this->getElementName(), // 字段name属性值
  135. 'lang' => trans('admin.uploader'),
  136. 'deleteData' => [
  137. static::FILE_DELETE_FLAG => '',
  138. '_token' => csrf_token(),
  139. '_method' => 'PUT',
  140. ],
  141. 'formData' => [
  142. '_id' => $primaryKey,
  143. 'upload_column' => $this->column,
  144. '_method' => 'PUT',
  145. '_token' => csrf_token(),
  146. ],
  147. ];
  148. $this->options($defaultOptions);
  149. }
  150. protected function setDefaultServer()
  151. {
  152. if (!$this->form || !method_exists($this->form, 'getAction')) {
  153. return;
  154. }
  155. if (empty($this->options['server'])) {
  156. $this->options['server'] = $this->form->getAction();
  157. }
  158. if (empty($this->options['deleteUrl'])) {
  159. $this->options['deleteUrl'] = $this->form->getAction();
  160. }
  161. if ($this->form->builder() && $this->form->builder()->isCreating()) {
  162. unset(
  163. $this->options['formData']['_method'],
  164. $this->options['deleteData']['_method'],
  165. $this->options['autoUpdateColumn']
  166. );
  167. }
  168. }
  169. /**
  170. * Get create url.
  171. *
  172. * @return string
  173. */
  174. public function getCreateUrl()
  175. {
  176. return str_replace('/create', '', URL::full());
  177. }
  178. /**
  179. * Set preview options form image field.
  180. *
  181. * @return void
  182. */
  183. protected function setupPreviewOptions()
  184. {
  185. $this->options['preview'] = $this->initialPreviewConfig();
  186. }
  187. /**
  188. * Set options for file-upload plugin.
  189. *
  190. * @param array $options
  191. *
  192. * @return $this
  193. */
  194. public function options($options = [])
  195. {
  196. $this->options = array_merge($options, $this->options);
  197. return $this;
  198. }
  199. }