/** * Tell if we have a "pdf" folder in the given path. * * @param string $path * * @return bool */ public function hasPdfFolder($path) { if ($path === '.') { return $this->manager->has('local://pdf'); } return $this->manager->has('local://' . $path . '/pdf'); }
/** * {@inheritdoc} */ public function data(ServerRequestInterface $request, Document $document) { $this->assertAdmin($request->getAttribute('actor')); $file = array_get($request->getUploadedFiles(), 'favicon'); $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'favicon'); $file->moveTo($tmpFile); $extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION); if ($extension !== 'ico') { $manager = new ImageManager(); $encodedImage = $manager->make($tmpFile)->resize(64, 64, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->encode('png'); file_put_contents($tmpFile, $encodedImage); $extension = 'png'; } $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => new Filesystem(new Local($this->app->publicPath() . '/assets'))]); if (($path = $this->settings->get('favicon_path')) && $mount->has($file = "target://{$path}")) { $mount->delete($file); } $uploadName = 'favicon-' . Str::lower(Str::quickRandom(8)) . '.' . $extension; $mount->move('source://' . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $this->settings->set('favicon_path', $uploadName); return parent::data($request, $document); }
/** * @param UploadAvatar $command * @return \Flarum\Core\User * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); if ($actor->id !== $user->id) { $this->assertCan($actor, 'edit', $user); } $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar'); $command->file->moveTo($tmpFile); try { $file = new UploadedFile($tmpFile, $command->file->getClientFilename(), $command->file->getClientMediaType(), $command->file->getSize(), $command->file->getError(), true); $this->validator->assertValid(['avatar' => $file]); $manager = new ImageManager(); // Explicitly tell Intervention to encode the image as JSON (instead of having to guess from the extension) $encodedImage = $manager->make($tmpFile)->fit(100, 100)->encode('jpg', 100); file_put_contents($tmpFile, $encodedImage); $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move('source://' . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user, $actor); return $user; } catch (Exception $e) { @unlink($tmpFile); throw $e; } }
/** * @param UploadAvatar $command * @return \Flarum\Core\Users\User * @throws \Flarum\Core\Exceptions\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); // Make sure the current user is allowed to edit the user profile. // This will let admins and the user themselves pass through, and // throw an exception otherwise. if ($actor->id !== $user->id) { $user->assertCan($actor, 'edit'); } $tmpFile = tempnam(sys_get_temp_dir(), 'avatar'); $command->file->moveTo($tmpFile); $manager = new ImageManager(); $manager->make($tmpFile)->fit(100, 100)->save(); event(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user); return $user; }
/** * @param UploadAvatar $command * @return \Flarum\Core\User * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); if ($actor->id !== $user->id) { $this->assertCan($actor, 'edit', $user); } $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar'); $command->file->moveTo($tmpFile); $file = new UploadedFile($tmpFile, $command->file->getClientFilename(), $command->file->getClientMediaType(), $command->file->getSize(), $command->file->getError(), true); $this->validator->assertValid(['avatar' => $file]); $manager = new ImageManager(); $manager->make($tmpFile)->fit(100, 100)->save(); $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user, $actor); return $user; }
/** * Delete an upload * * @param string $url A URL like was returned from moveUpload() * @return void */ public function delete($url) { // Convert to a path $path = $this->helpers->path($url); // Delete the path if it still exists if ($this->manager->has('disk://' . $path)) { $this->manager->delete('disk://' . $path); } }
/** * Fired just after * saving the form entry. * * This is basically a validator * but I am putting it here because * it's far easier to test being that * the disk is being loaded already. * * @param MountManager $manager * @param MessageBag $messages * @param Redirector $redirector */ public function onSaved(MountManager $manager, MessageBag $messages, Redirector $redirector) { /* @var DiskFormBuilder $builder */ $builder = $this->forms->get('disk'); /* @var DiskInterface $entry */ $entry = $builder->getFormEntry(); app()->call('Anomaly\\FilesModule\\Disk\\Listener\\RegisterDisks@handle'); try { $manager->has($entry->path('test.me')); } catch (\Exception $e) { $messages->error($e->getMessage()); $this->setFormResponse($redirector->to('admin/files/disks/edit/' . $entry->getId())); } }
public function handle(UploadAvatarCommand $command) { $user = $this->users->findOrFail($command->userId); // Make sure the current user is allowed to edit the user profile. // This will let admins and the user themselves pass through, and // throw an exception otherwise. $user->assertCan($command->actor, 'edit'); $manager = new ImageManager(array('driver' => 'imagick')); $manager->make($command->file->getRealPath())->fit(100, 100)->save(); $filename = $command->file->getFilename(); $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $mount = new MountManager(['source' => new Filesystem(new Local($command->file->getPath())), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $user->changeAvatarPath($uploadName); event(new AvatarWillBeUploaded($user, $command)); $mount->move("source://{$filename}", "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user); return $user; }
/** * Publish the directory to the given directory. * * @param string $from * @param string $to * @return void */ protected function publishDirectory($from, $to) { $manager = new MountManager(['from' => new Flysystem(new LocalAdapter($from)), 'to' => new Flysystem(new LocalAdapter($to))]); foreach ($manager->listContents('from://', true) as $file) { if ($file['type'] === 'file' && (!$manager->has('to://' . $file['path']) || $this->option('force'))) { $manager->put('to://' . $file['path'], $manager->read('from://' . $file['path'])); } } $this->status($from, $to, 'Directory'); }
/** * Publish the directory to the given directory. * * @param string $from * @param string $to * * @return void */ protected function publishDirectory($from, $to) { $manager = new MountManager(['from' => new Flysystem(new LocalAdapter($from)), 'to' => new Flysystem(new LocalAdapter($to))]); foreach ($manager->listContents('from://', true) as $file) { $path = $file['path']; if (substr($path, 0, 8) === 'database' || substr($path, 0, 15) === 'resources/views') { continue; } if ($file['type'] === 'file' && (!$manager->has('to://' . $file['path']) || $this->option('force'))) { $manager->put('to://' . $file['path'], $manager->read('from://' . $file['path'])); } } $this->status($from, $to, 'Directory'); }