/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
- <?php
- namespace Encore\Admin\Form\Field;
- use Encore\Admin\Form;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\URL;
- use Illuminate\Support\MessageBag;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- trait UploadField
- {
- /**
- * Upload directory.
- *
- * @var string
- */
- protected $directory = '';
- /**
- * File name.
- *
- * @var null
- */
- protected $name = null;
- /**
- * Storage instance.
- *
- * @var \Illuminate\Filesystem\Filesystem
- */
- protected $storage = '';
- /**
- * If use unique name to store upload file.
- *
- * @var bool
- */
- protected $useUniqueName = false;
- /**
- * Initialize the storage instance.
- *
- * @return void.
- */
- protected function initStorage()
- {
- $this->disk(config('admin.upload.disk'));
- }
- /**
- * Set default options form image field.
- *
- * @return void
- */
- protected function setupDefaultOptions()
- {
- $defaultOptions = [
- 'overwriteInitial' => false,
- 'initialPreviewAsData' => true,
- 'browseLabel' => trans('admin::lang.browse'),
- 'showRemove' => false,
- 'showUpload' => false,
- 'initialCaption' => $this->initialCaption($this->value),
- 'deleteExtraData' => [
- $this->column => static::FILE_DELETE_FLAG,
- static::FILE_DELETE_FLAG => '',
- '_token' => csrf_token(),
- '_method' => 'PUT',
- ],
- ];
- if ($this->form instanceof Form) {
- $defaultOptions['deleteUrl'] = $this->form->resource().'/'.$this->form->model()->getKey();
- }
- $this->options($defaultOptions);
- }
- /**
- * Set preview options form image field.
- *
- * @return void
- */
- protected function setupPreviewOptions()
- {
- $this->options([
- //'initialPreview' => $this->preview(),
- 'initialPreviewConfig' => $this->initialPreviewConfig(),
- ]);
- }
- /**
- * Set options for file-upload plugin.
- *
- * @param array $options
- *
- * @return $this
- */
- public function options($options = [])
- {
- $this->options = array_merge($options, $this->options);
- return $this;
- }
- /**
- * Set disk for storage.
- *
- * @param string $disk Disks defined in `config/filesystems.php`.
- *
- * @return $this
- */
- public function disk($disk)
- {
- if (!array_key_exists($disk, config('filesystems.disks'))) {
- $error = new MessageBag([
- 'title' => 'Config error.',
- 'message' => "Disk [$disk] not configured, please add a disk config in `config/filesystems.php`.",
- ]);
- return session()->flash('error', $error);
- }
- $this->storage = Storage::disk($disk);
- return $this;
- }
- /**
- * Specify the directory and name for upload file.
- *
- * @param string $directory
- * @param null|string $name
- *
- * @return $this
- */
- public function move($directory, $name = null)
- {
- $this->dir($directory);
- $this->name($name);
- return $this;
- }
- /**
- * Specify the directory upload file.
- *
- * @param string $dir
- *
- * @return $this
- */
- public function dir($dir)
- {
- if ($dir) {
- $this->directory = $dir;
- }
- return $this;
- }
- /**
- * Set name of store name.
- *
- * @param string|callable $name
- *
- * @return $this
- */
- public function name($name)
- {
- if ($name) {
- $this->name = $name;
- }
- return $this;
- }
- /**
- * Use unique name for store upload file.
- *
- * @return $this
- */
- public function uniqueName()
- {
- $this->useUniqueName = true;
- return $this;
- }
- /**
- * Get store name of upload file.
- *
- * @param UploadedFile $file
- *
- * @return string
- */
- protected function getStoreName(UploadedFile $file)
- {
- if ($this->useUniqueName) {
- return $this->generateUniqueName($file);
- }
- if (is_callable($this->name)) {
- $callback = $this->name->bindTo($this);
- return call_user_func($callback, $file);
- }
- if (is_string($this->name)) {
- return $this->name;
- }
- return $file->getClientOriginalName();
- }
- /**
- * Get directory for store file.
- *
- * @return mixed|string
- */
- public function getDirectory()
- {
- if ($this->directory instanceof \Closure) {
- return call_user_func($this->directory, $this->form);
- }
- return $this->directory ?: $this->defaultDirectory();
- }
- /**
- * Upload file and delete original file.
- *
- * @param UploadedFile $file
- *
- * @return mixed
- */
- protected function upload(UploadedFile $file)
- {
- $this->renameIfExists($file);
- return $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
- }
- /**
- * If name already exists, rename it.
- *
- * @param $file
- *
- * @return void
- */
- public function renameIfExists(UploadedFile $file)
- {
- if ($this->storage->exists("{$this->getDirectory()}/$this->name")) {
- $this->name = $this->generateUniqueName($file);
- }
- }
- /**
- * Get file visit url.
- *
- * @param $path
- *
- * @return string
- */
- public function objectUrl($path)
- {
- if (URL::isValidUrl($path)) {
- return $path;
- }
- return rtrim(config('admin.upload.host'), '/').'/'.trim($path, '/');
- }
- /**
- * Generate a unique name for uploaded file.
- *
- * @param UploadedFile $file
- *
- * @return string
- */
- protected function generateUniqueName(UploadedFile $file)
- {
- return md5(uniqid()).'.'.$file->guessExtension();
- }
- /**
- * Destroy original files.
- *
- * @return void.
- */
- public function destroy()
- {
- if ($this->storage->exists($this->original)) {
- $this->storage->delete($this->original);
- }
- }
- }