/** * Uploads and attaches files for the current relation. * * @param Filesystem $filesystem * * @return bool|array */ public function handle(Filesystem $filesystem) { $files = $this->request->file('files'); if (is_array($files)) { $uploaded = []; foreach ($files as $file) { // Double check that we have an uploaded file instance. if ($file instanceof UploadedFile) { // Generates the unique file name. $name = implode('.', [uuid(), $file->getClientOriginalExtension()]); // Generates the complete storage path. $path = implode(DIRECTORY_SEPARATOR, [$this->path, $name]); // Try and move the uploaded file into storage. if ($filesystem->put($path, file_get_contents($file->getRealPath()))) { // Successfully moved uploaded file, create the record. $attributes = ['user_id' => auth()->id(), 'name' => $file->getClientOriginalName(), 'file_name' => $name, 'file_path' => $path]; $uploaded[] = $this->relation->create($attributes); } } } return $uploaded; } return false; }
/** * Creates an upload record for the specified work order and uploaded file. * * @param Model $model * @param UploadedFile $file * @param BelongsToMany $relation * * @return \Stevebauman\Maintenance\Models\Attachment|bool */ public function create(Model $model, UploadedFile $file, BelongsToMany $relation) { $path = $this->getUploadPath($model->getKey()); $fileName = $this->getUniqueFileName($file); $storePath = $path . DIRECTORY_SEPARATOR . $fileName; $realPath = $file->getRealPath(); $contents = $realPath ? file_get_contents($realPath) : false; if ($contents && $this->storage->put($storePath, $contents)) { $attributes = ['user_id' => $this->sentry->getCurrentUserId(), 'name' => $file->getClientOriginalName(), 'file_name' => $file->getClientOriginalName(), 'file_path' => $storePath]; $attachment = $relation->create($attributes); if ($attachment) { return $attachment; } } return false; }