/** * Run the seeder. */ public function run() { $disk = $this->disks->findBySlug('local'); $this->folders->truncate(); $this->folders->create(['en' => ['name' => 'Images', 'description' => 'A folder for images.'], 'slug' => 'images', 'disk' => $disk, 'allowed_types' => ['png', 'jpeg', 'jpg']]); $this->folders->create(['en' => ['name' => 'Documents', 'description' => 'A folder for documents.'], 'slug' => 'documents', 'disk' => $disk, 'allowed_types' => ['pdf', 'docx']]); }
/** * Locate a file by disk and path. * * @param $disk * @param $path * @return FileInterface|null */ public function locate($disk, $path) { if (!($disk = $this->disks->findBySlug($disk))) { return null; } $folder = dirname($path) !== '.' ? $this->folders->findByPath(dirname($path), $disk) : null; if (!($file = $this->files->findByName(basename($path), $disk, $folder))) { return null; } return $file; }
/** * Handle the command. * * @param DiskRepositoryInterface $disks * @param FileRepositoryInterface $files * @param FileFieldTypeParser $parser * @param Request $request * @param MountManager $manager * * @return null|bool|FileInterface */ public function handle(DiskRepositoryInterface $disks, FileRepositoryInterface $files, FileFieldTypeParser $parser, Request $request, MountManager $manager) { $path = trim(array_get($this->fieldType->getConfig(), 'path'), './'); $file = $request->file($this->fieldType->getInputName()); $value = $request->get($this->fieldType->getInputName() . '_id'); /** * Make sure we have at least * some kind of input. */ if ($file === null) { if (!$value) { return null; } return $files->find($value); } // Make sure we have a valid upload disk. First by slug. if (!($disk = $disks->findBySlug($slug = array_get($this->fieldType->getConfig(), 'disk')))) { // If that fails look up by id. if (!($disk = $disks->find($id = array_get($this->fieldType->getConfig(), 'disk')))) { return null; } } // Make the path. $path = $parser->parse($path, $this->fieldType); $path = (!empty($path) ? $path . '/' : null) . $file->getClientOriginalName(); return $manager->putStream($disk->path($path), fopen($file->getRealPath(), 'r+')); }
/** * Return a form to create a new folder. * * @param FolderFormBuilder $form * @param $path * @return \Symfony\Component\HttpFoundation\Response */ public function create(FolderFormBuilder $form, DiskRepositoryInterface $disks, FolderRepositoryInterface $folders, $disk, $path = null) { $form->setDisk($disk = $disks->findBySlug($disk)); if ($path && ($parent = $folders->findByPath($path, $disk))) { $form->setParent($parent); } return $form->render(); }
/** * Return the form to upload files. * * @param FolderRepositoryInterface $folders * @param DiskRepositoryInterface $disks * @param FileFormBuilder $form * @param $disk * @param null $path * @return Response */ public function upload(FolderRepositoryInterface $folders, DiskRepositoryInterface $disks, FileFormBuilder $form, $disk, $path = null) { $form->setDisk($disk = $disks->findBySlug($disk)); if ($path && ($folder = $folders->findByPath($path, $disk))) { $form->setFolder($folder); } return $form->render(); }
/** * Handle the validation. * * @param FormBuilder $builder * @param DiskRepositoryInterface $disks * @param $attribute * @return bool */ public function handle(FormBuilder $builder, DiskRepositoryInterface $disks, $attribute) { $fieldType = $builder->getFormField($attribute); $disk = array_get($fieldType->getConfig(), 'disk'); if (is_numeric($disk) && !$disks->find($disk)) { return false; } if (!is_numeric($disk) && !$disks->findBySlug($disk)) { return false; } return true; }
/** * Handle the file upload. * * @param DiskRepositoryInterface $disks * @param ResponseFactory $response * @param MountManager $manager * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function handle(DiskRepositoryInterface $disks, ResponseFactory $response, MountManager $manager, Request $request) { $path = trim($request->get('path'), '.'); $file = $request->file('upload'); $disk = $request->get('disk'); if (is_numeric($disk)) { $disk = $disks->find($disk); } elseif (is_string($disk)) { $disk = $disks->findBySlug($disk); } $file = $manager->putStream($disk->path(ltrim(trim($path, '/') . '/' . $file->getClientOriginalName(), '/')), fopen($file->getRealPath(), 'r+')); /* @var FileInterface $file */ return $response->json($file->getAttributes()); }
/** * Return the details of a file or folder. * * @param $disk * @param $path * @return string */ public function view($disk, $path) { $disk = $this->disks->findBySlug($disk); $folder = $this->folders->findByPath(dirname($path), $disk); $file = $this->files->findByName(basename($path), $disk, $folder); if (!$disk || !$folder && !$file) { abort(404); } if ($file) { return json_encode($file); } if ($folder) { return json_encode($folder); } }