disk(config('admin.upload.disk')); if (! $this->storage) { $this->storage = false; } } /** * If name already exists, rename it. * * @param $file * * @return void */ public function renameIfExists(UploadedFile $file) { if ($this->getStorage()->exists("{$this->getDirectory()}/$this->name")) { $this->name = $this->generateUniqueName($file); } } /** * @return string */ protected function getUploadPath() { return "{$this->getDirectory()}/$this->name"; } /** * Get store name of upload file. * * @param UploadedFile $file * * @return string */ protected function getStoreName(UploadedFile $file) { if ($this->useUniqueName) { return $this->generateUniqueName($file); } if ($this->useSequenceName) { return $this->generateSequenceName($file); } if ($this->name instanceof \Closure) { return $this->name->call($this, $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(); } /** * Indicates if the underlying field is retainable. * * @return $this */ public function retainable($retainable = true) { $this->retainable = $retainable; return $this; } /** * Upload File. * * @param UploadedFile $file * * @return Response */ public function upload(UploadedFile $file) { try { $request = request(); $id = $request->get('_id'); /* @var \Dcat\Admin\Support\WebUploader $webUploader */ $webUploader = Admin::context()->webUploader; if (! $id) { return $webUploader->responseErrorMessage(403, 'Missing id'); } if ($errors = $this->getErrorMessages($file)) { $webUploader->deleteTempFile(); return $webUploader->responseValidationMessage($errors); } $this->name = $this->getStoreName($file); $this->renameIfExists($file); $this->prepareFile($file); if (! is_null($this->storagePermission)) { $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission); } else { $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name); } $webUploader->deleteTempFile(); if ($result) { $path = $this->getUploadPath(); return $webUploader->responseUploaded($path, $this->objectUrl($path)); } return $webUploader->responseFailedMessage(); } catch (\Throwable $e) { $webUploader->deleteTempFile(); throw $e; } } /** * @param UploadedFile $file */ protected function prepareFile(UploadedFile $file) { } /** * 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; } /** * Use sequence name for store upload file. * * @return $this */ public function sequenceName() { $this->useSequenceName = true; return $this; } /** * Generate a unique name for uploaded file. * * @param UploadedFile $file * * @return string */ protected function generateUniqueName(UploadedFile $file) { return md5(uniqid()).'.'.$file->getClientOriginalExtension(); } /** * Generate a sequence name for uploaded file. * * @param UploadedFile $file * * @return string */ protected function generateSequenceName(UploadedFile $file) { $index = 1; $extension = $file->getClientOriginalExtension(); $originalName = $file->getClientOriginalName(); $newName = $originalName.'_'.$index.'.'.$extension; while ($this->getStorage()->exists("{$this->getDirectory()}/$newName")) { $index++; $newName = $originalName.'_'.$index.'.'.$extension; } return $newName; } /** * @param UploadedFile $file * * @return bool|\Illuminate\Support\MessageBag */ protected function getErrorMessages(UploadedFile $file) { $rules = $attributes = []; if (! $fieldRules = $this->getRules()) { return false; } $rules[$this->column] = $fieldRules; $attributes[$this->column] = $this->label; /* @var \Illuminate\Validation\Validator $validator */ $validator = Validator::make([$this->column => $file], $rules, $this->validationMessages, $attributes); if (! $validator->passes()) { $errors = $validator->errors()->getMessages()[$this->column]; return implode('; ', $errors); } } /** * Destroy original files. * * @return void. */ public function destroy() { if ($this->retainable) { return; } $this->deleteFile($this->original); } /** * Destroy original files. * * @param $file */ public function destroyIfChanged($file) { if (! $file || ! $this->original) { return $this->destroy(); } $file = array_filter((array) $file); $original = (array) $this->original; $this->deleteFile(Arr::except(array_combine($original, $original), $file)); } /** * Destroy files. * * @param string|array $path */ public function deleteFile($paths) { if (! $paths) { return; } $storage = $this->getStorage(); foreach ((array) $paths as $path) { if ($storage->exists($path)) { $storage->delete($path); } else { $prefix = $storage->url(''); $path = str_replace($prefix, '', $path); if ($storage->exists($path)) { $storage->delete($path); } } } } /** * Get storage instance. * * @return \Illuminate\Filesystem\Filesystem|null */ public function getStorage() { if ($this->storage === null) { $this->initStorage(); } return $this->storage; } /** * Set disk for storage. * * @param string $disk Disks defined in `config/filesystems.php`. * * @throws \Exception * * @return $this */ public function disk($disk) { try { $this->storage = Storage::disk($disk); } catch (\Exception $exception) { if (! array_key_exists($disk, config('filesystems.disks'))) { admin_error( 'Config error.', "Disk [$disk] not configured, please add a disk config in `config/filesystems.php`." ); return $this; } throw $exception; } return $this; } /** * Get file visit url. * * @param string $path * * @return string */ public function objectUrl($path) { if (URL::isValidUrl($path)) { return $path; } return $this->getStorage()->url($path); } /** * @param $permission * * @return $this */ public function storagePermission($permission) { $this->storagePermission = $permission; return $this; } }