File.php 4.3 KB

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