/vendor/encore/laravel-admin/src/Form/Field/UploadField.php

https://github.com/SmallRuralDog/cloud-doc-server · PHP · 298 lines · 136 code · 40 blank · 122 comment · 11 complexity · 8ecd481e37a3a3166c23a7582151a08f MD5 · raw file

  1. <?php
  2. namespace Encore\Admin\Form\Field;
  3. use Encore\Admin\Form;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\URL;
  6. use Illuminate\Support\MessageBag;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. trait UploadField
  9. {
  10. /**
  11. * Upload directory.
  12. *
  13. * @var string
  14. */
  15. protected $directory = '';
  16. /**
  17. * File name.
  18. *
  19. * @var null
  20. */
  21. protected $name = null;
  22. /**
  23. * Storage instance.
  24. *
  25. * @var \Illuminate\Filesystem\Filesystem
  26. */
  27. protected $storage = '';
  28. /**
  29. * If use unique name to store upload file.
  30. *
  31. * @var bool
  32. */
  33. protected $useUniqueName = false;
  34. /**
  35. * Initialize the storage instance.
  36. *
  37. * @return void.
  38. */
  39. protected function initStorage()
  40. {
  41. $this->disk(config('admin.upload.disk'));
  42. }
  43. /**
  44. * Set default options form image field.
  45. *
  46. * @return void
  47. */
  48. protected function setupDefaultOptions()
  49. {
  50. $defaultOptions = [
  51. 'overwriteInitial' => false,
  52. 'initialPreviewAsData' => true,
  53. 'browseLabel' => trans('admin::lang.browse'),
  54. 'showRemove' => false,
  55. 'showUpload' => false,
  56. 'initialCaption' => $this->initialCaption($this->value),
  57. 'deleteExtraData' => [
  58. $this->column => static::FILE_DELETE_FLAG,
  59. static::FILE_DELETE_FLAG => '',
  60. '_token' => csrf_token(),
  61. '_method' => 'PUT',
  62. ],
  63. ];
  64. if ($this->form instanceof Form) {
  65. $defaultOptions['deleteUrl'] = $this->form->resource().'/'.$this->form->model()->getKey();
  66. }
  67. $this->options($defaultOptions);
  68. }
  69. /**
  70. * Set preview options form image field.
  71. *
  72. * @return void
  73. */
  74. protected function setupPreviewOptions()
  75. {
  76. $this->options([
  77. //'initialPreview' => $this->preview(),
  78. 'initialPreviewConfig' => $this->initialPreviewConfig(),
  79. ]);
  80. }
  81. /**
  82. * Set options for file-upload plugin.
  83. *
  84. * @param array $options
  85. *
  86. * @return $this
  87. */
  88. public function options($options = [])
  89. {
  90. $this->options = array_merge($options, $this->options);
  91. return $this;
  92. }
  93. /**
  94. * Set disk for storage.
  95. *
  96. * @param string $disk Disks defined in `config/filesystems.php`.
  97. *
  98. * @return $this
  99. */
  100. public function disk($disk)
  101. {
  102. if (!array_key_exists($disk, config('filesystems.disks'))) {
  103. $error = new MessageBag([
  104. 'title' => 'Config error.',
  105. 'message' => "Disk [$disk] not configured, please add a disk config in `config/filesystems.php`.",
  106. ]);
  107. return session()->flash('error', $error);
  108. }
  109. $this->storage = Storage::disk($disk);
  110. return $this;
  111. }
  112. /**
  113. * Specify the directory and name for upload file.
  114. *
  115. * @param string $directory
  116. * @param null|string $name
  117. *
  118. * @return $this
  119. */
  120. public function move($directory, $name = null)
  121. {
  122. $this->dir($directory);
  123. $this->name($name);
  124. return $this;
  125. }
  126. /**
  127. * Specify the directory upload file.
  128. *
  129. * @param string $dir
  130. *
  131. * @return $this
  132. */
  133. public function dir($dir)
  134. {
  135. if ($dir) {
  136. $this->directory = $dir;
  137. }
  138. return $this;
  139. }
  140. /**
  141. * Set name of store name.
  142. *
  143. * @param string|callable $name
  144. *
  145. * @return $this
  146. */
  147. public function name($name)
  148. {
  149. if ($name) {
  150. $this->name = $name;
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Use unique name for store upload file.
  156. *
  157. * @return $this
  158. */
  159. public function uniqueName()
  160. {
  161. $this->useUniqueName = true;
  162. return $this;
  163. }
  164. /**
  165. * Get store name of upload file.
  166. *
  167. * @param UploadedFile $file
  168. *
  169. * @return string
  170. */
  171. protected function getStoreName(UploadedFile $file)
  172. {
  173. if ($this->useUniqueName) {
  174. return $this->generateUniqueName($file);
  175. }
  176. if (is_callable($this->name)) {
  177. $callback = $this->name->bindTo($this);
  178. return call_user_func($callback, $file);
  179. }
  180. if (is_string($this->name)) {
  181. return $this->name;
  182. }
  183. return $file->getClientOriginalName();
  184. }
  185. /**
  186. * Get directory for store file.
  187. *
  188. * @return mixed|string
  189. */
  190. public function getDirectory()
  191. {
  192. if ($this->directory instanceof \Closure) {
  193. return call_user_func($this->directory, $this->form);
  194. }
  195. return $this->directory ?: $this->defaultDirectory();
  196. }
  197. /**
  198. * Upload file and delete original file.
  199. *
  200. * @param UploadedFile $file
  201. *
  202. * @return mixed
  203. */
  204. protected function upload(UploadedFile $file)
  205. {
  206. $this->renameIfExists($file);
  207. return $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
  208. }
  209. /**
  210. * If name already exists, rename it.
  211. *
  212. * @param $file
  213. *
  214. * @return void
  215. */
  216. public function renameIfExists(UploadedFile $file)
  217. {
  218. if ($this->storage->exists("{$this->getDirectory()}/$this->name")) {
  219. $this->name = $this->generateUniqueName($file);
  220. }
  221. }
  222. /**
  223. * Get file visit url.
  224. *
  225. * @param $path
  226. *
  227. * @return string
  228. */
  229. public function objectUrl($path)
  230. {
  231. if (URL::isValidUrl($path)) {
  232. return $path;
  233. }
  234. return rtrim(config('admin.upload.host'), '/').'/'.trim($path, '/');
  235. }
  236. /**
  237. * Generate a unique name for uploaded file.
  238. *
  239. * @param UploadedFile $file
  240. *
  241. * @return string
  242. */
  243. protected function generateUniqueName(UploadedFile $file)
  244. {
  245. return md5(uniqid()).'.'.$file->guessExtension();
  246. }
  247. /**
  248. * Destroy original files.
  249. *
  250. * @return void.
  251. */
  252. public function destroy()
  253. {
  254. if ($this->storage->exists($this->original)) {
  255. $this->storage->delete($this->original);
  256. }
  257. }
  258. }