/** * {@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\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; }
/** * Handles the command execution. * * @param UploadImage $command * @return null|string * * @todo check permission */ public function handle(UploadImage $command) { // check if the user can upload images, otherwise return $this->assertCan($command->actor, 'flagrow.image.upload'); $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'image'); $command->file->moveTo($tmpFile); $file = new UploadedFile($tmpFile, $command->file->getClientFilename(), $command->file->getClientMediaType(), $command->file->getSize(), $command->file->getError(), true); // validate the file $this->validator->maxFileSize = $this->settings->get('flagrow.image-upload.maxFileSize', 2048); $this->validator->assertValid(['image' => $file]); // resize if enabled if ($this->settings->get('flagrow.image-upload.mustResize')) { $manager = new ImageManager(); $manager->make($tmpFile)->fit($this->settings->get('flagrow.image-upload.resizeMaxWidth', 100), $this->settings->get('flagrow.image-upload.resizeMaxHeight', 100))->save(); } $image = (new Image())->forceFill(['user_id' => $command->actor->id, 'upload_method' => $this->settings->get('flagrow.image-upload.uploadMethod', 'local'), 'created_at' => Carbon::now(), 'file_name' => sprintf('%d-%s.%s', $command->actor->id, Str::quickRandom(), $file->guessExtension() ?: 'jpg'), 'file_size' => $file->getSize()]); // fire the Event ImageWillBeSaved, which can be extended and/or modified elsewhere $this->events->fire(new ImageWillBeSaved($command->actor, $image, $file)); $tmpFilesystem = new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))); $meta = $this->upload->uploadContents($image->file_name, $tmpFilesystem->readAndDelete(pathinfo($tmpFile, PATHINFO_BASENAME))); if ($meta) { $image->file_url = array_get($meta, 'url'); if ($image->isDirty()) { $image->save(); } return $image; } return false; }
/** * Set up the asset manager, preloaded with a JavaScript compiler and a LESS * compiler. Automatically add the files necessary to boot a Flarum client, * as well as any configured LESS customizations. * * @return AssetManager */ protected function getAssets() { $public = $this->getAssetDirectory(); $watch = $this->app->config('debug'); $assets = new AssetManager(new JsCompiler($public, "{$this->clientName}.js", $watch, $this->cache), new LessCompiler($public, "{$this->clientName}.css", $watch, $this->app->storagePath() . '/less')); $this->addAssets($assets); $this->addCustomizations($assets); return $assets; }
private function saveAvatarFromUrl(User $user, $url) { $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar'); $manager = new ImageManager(); $manager->make($url)->fit(100, 100)->save($tmpFile); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); }
/** * @param Application $app */ protected function registerLogger(Application $app) { $logger = new Logger($app->environment()); $logPath = $app->storagePath() . '/logs/flarum.log'; $handler = new StreamHandler($logPath, Logger::DEBUG); $handler->setFormatter(new LineFormatter(null, null, true, true)); $logger->pushHandler($handler); $app->instance('log', $logger); $app->alias('log', 'Psr\\Log\\LoggerInterface'); }
/** * @param Application $app */ protected function registerCache(Application $app) { $app->singleton('cache.store', function ($app) { return new \Illuminate\Cache\Repository($app->make('cache.filestore')); }); $app->singleton('cache.filestore', function ($app) { return new \Illuminate\Cache\FileStore(new \Illuminate\Filesystem\Filesystem(), $app->storagePath() . '/cache'); }); $app->alias('cache.filestore', 'Illuminate\\Contracts\\Cache\\Store'); $app->alias('cache.store', 'Illuminate\\Contracts\\Cache\\Repository'); }
protected function getLessStorage() { return $this->app->storagePath() . '/less'; }