initStorage(); parent::__construct($column, $arguments); } /** * Default directory for file to upload. * * @return mixed */ public function defaultDirectory() { return config('admin.upload.directory.file'); } /** * {@inheritdoc} */ public function getValidator(array $input) { if (request()->has(static::FILE_DELETE_FLAG)) { return false; } if ($this->validator) { return $this->validator->call($this, $input); } /* * If has original value, means the form is in edit mode, * then remove required rule from rules. */ if ($this->original()) { $this->removeRule('required'); } /* * Make input data validatable if the column data is `null`. */ if (Arr::has($input, $this->column) && is_null(Arr::get($input, $this->column))) { $input[$this->column] = ''; } $rules = $attributes = []; if (!$fieldRules = $this->getRules()) { return false; } $rules[$this->column] = $fieldRules; $attributes[$this->column] = $this->label; return Validator::make($input, $rules, $this->getValidationMessages(), $attributes); } /** * Prepare for saving. * * @param UploadedFile|array $file * * @return mixed|string */ protected function prepareToSave($file) { if (request()->has(static::FILE_DELETE_FLAG)) { return $this->destroy(); } $this->name = $this->getStoreName($file); return $this->uploadAndDeleteOriginal($file); } /** * Upload file and delete original file. * * @param UploadedFile $file * * @return mixed */ protected function uploadAndDeleteOriginal(UploadedFile $file) { $this->renameIfExists($file); $path = null; if (!is_null($this->storagePermission)) { $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission); } else { $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name); } $this->destroy(); return $path; } /** * Preview html for file-upload plugin. * * @return string */ protected function preview() { return $this->objectUrl($this->value()); } /** * Initialize the caption. * * @param string $caption * * @return string */ protected function initialCaption($caption) { return basename($caption); } /** * @return array */ protected function initialPreviewConfig() { return [ ['caption' => basename($this->value()), 'key' => 0], ]; } /** * Render file upload field. * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function render() { $this->options(['overwriteInitial' => true]); $this->setupDefaultOptions(); if (!empty($this->value)) { $this->attribute('data-initial-preview', $this->preview()); $this->attribute('data-initial-caption', $this->initialCaption($this->value)); $this->setupPreviewOptions(); } $options = json_encode($this->options); $this->script = <<getElementClassSelector()}").fileinput({$options}); JS; return parent::render(); } }