File.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\Validator;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. class File extends Field
  9. {
  10. use WebUploader, UploadField;
  11. /**
  12. * Css.
  13. *
  14. * @var array
  15. */
  16. protected static $css = [
  17. '/vendor/dcat-admin/webuploader/webuploader.min.css',
  18. ];
  19. /**
  20. * Js.
  21. *
  22. * @var array
  23. */
  24. protected static $js = [
  25. '/vendor/dcat-admin/webuploader/webuploader.min.js',
  26. '/vendor/dcat-admin/dcat-admin/upload.min.js',
  27. ];
  28. /**
  29. * Create a new File instance.
  30. *
  31. * @param string $column
  32. * @param array $arguments
  33. */
  34. public function __construct($column, $arguments = [])
  35. {
  36. parent::__construct($column, $arguments);
  37. $this->initStorage();
  38. $this->setupDefaultOptions();
  39. }
  40. /**
  41. * Default directory for file to upload.
  42. *
  43. * @return mixed
  44. */
  45. public function defaultDirectory()
  46. {
  47. return config('admin.upload.directory.file');
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getValidator(array $input)
  53. {
  54. if (request()->has(static::FILE_DELETE_FLAG)) {
  55. return false;
  56. }
  57. if ($this->validator) {
  58. return $this->validator->call($this, $input);
  59. }
  60. $value = Arr::get($input, $this->column);
  61. $value = array_filter(is_array($value) ? $value : explode(',', $value));
  62. $fileLimit = $this->options['fileNumLimit'] ?? 1;
  63. if ($fileLimit < count($value)) {
  64. $this->form->responseValidationMessages(
  65. $this->column,
  66. trans('admin.uploader.max_file_limit', ['attribute' => $this->label, 'max' => $fileLimit])
  67. );
  68. return false;
  69. }
  70. $rules = $attributes = [];
  71. if (!$this->hasRule('required')) {
  72. return false;
  73. }
  74. $rules[$this->column] = 'required';
  75. $attributes[$this->column] = $this->label;
  76. return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
  77. }
  78. /**
  79. * Prepare for saving.
  80. *
  81. * @param string $file
  82. *
  83. * @return mixed|string
  84. */
  85. public function prepare($file)
  86. {
  87. if (request()->has(static::FILE_DELETE_FLAG)) {
  88. return $this->destroy();
  89. }
  90. $this->destroyIfChanged($file);
  91. return $file;
  92. }
  93. /**
  94. * Set field as disabled.
  95. *
  96. * @return $this
  97. */
  98. public function disable()
  99. {
  100. $this->options['disabled'] = true;
  101. return $this;
  102. }
  103. protected function formatFieldData($data)
  104. {
  105. return Helper::array(Arr::get($data, $this->column));
  106. }
  107. /**
  108. * @return array
  109. */
  110. protected function initialPreviewConfig()
  111. {
  112. $previews = [];
  113. foreach ($this->value as $value) {
  114. $previews[] = [
  115. 'id' => $value,
  116. 'path' => basename($value),
  117. 'url' => $this->objectUrl($value)
  118. ];
  119. }
  120. return $previews;
  121. }
  122. protected function forceOptions()
  123. {
  124. $this->options['fileNumLimit'] = 1;
  125. }
  126. /**
  127. * Render file upload field.
  128. *
  129. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  130. */
  131. public function render()
  132. {
  133. $this->setDefaultServer();
  134. if (!empty($this->value)) {
  135. $this->setupPreviewOptions();
  136. }
  137. $this->forceOptions();
  138. if ($this->value !== null) {
  139. $this->value = join(',', $this->value);
  140. }
  141. $this->addVariables([
  142. 'options' => json_encode($this->options),
  143. '_files' => $this->options['isImage'] ? '' : '_files',
  144. ]);
  145. return parent::render();
  146. }
  147. }