/** * {@inheritdoc} */ public function move(FileInterface $file, $srcFsAlias, $destFsAlias) { $isFileMoved = $this->mountManager->move(sprintf('%s://%s', $srcFsAlias, $file->getKey()), sprintf('%s://%s', $destFsAlias, $file->getKey())); if (!$isFileMoved) { throw new FileTransferException(sprintf('Impossible to move the file "%s" from "%s" to "%s".', $file->getKey(), $srcFsAlias, $destFsAlias)); } $file->setStorage($destFsAlias); $this->saver->save($file); }
/** * Move an uploaded file from the /tmp directory of the local filesystem * to the configured location * * @param Symfony\Component\HttpFoundation\File\UploadedFile $file * @return string $url A URL to to the file, resolveable in HTML */ public function moveUpload(UploadedFile $file) { // Nest the uploaded file into unique sub directory and a unqiue name $path = $this->makeNestedAndUniquePath($file->getClientOriginalName()); // Move the uploaded file to the destination using Flysystem and return // the new path $this->manager->move('tmp://' . $file->getFilename(), 'disk://' . $path); // Return the URL of the upload. return $this->helpers->url($path); }
/** * {@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\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; }
public function saveImage($tmpFile) { $dir = date('Ym/d'); $mount = new MountManager(['source' => new Filesystem(new LocalFS(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => new Filesystem(new LocalFS(public_path('assets/uploads')))]); $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$dir}/{$uploadName}"); return $this->config['urlPrefix'] . 'uploads/' . $dir . '/' . $uploadName; }
/** * @param FileNode $from * @param FileNode $to * * @return FileNode * @throws TransferFailedException */ public function moveTo(FileNode $from, FileNode $to) { $mountManager = new MountManager(['from' => $from->getFilesystem(), 'to' => $to->getFilesystem()]); $this->log(LogLevel::INFO, "Moving file from: '{from}', to: '{to}'", ['from' => $from, 'to' => $to]); if (!$mountManager->move('from://' . $from->getPath(), 'to://' . $to->getPath())) { throw new TransferFailedException($from, $to); } return $to; }
public function handle(Request $request) { $images = $request->http->getUploadedFiles()['images']; $results = []; foreach ($images as $image_key => $image) { $tmpFile = tempnam(sys_get_temp_dir(), 'image'); $image->moveTo($tmpFile); $urlGenerator = app('Flarum\\Http\\UrlGeneratorInterface'); $dir = 'uploads/' . date('Ym/d'); $path = './assets/' . $dir; $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => new Filesystem(new Local($path))]); $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $results['img_' . $image_key] = Core::url() . '/assets/' . $dir . '/' . $uploadName; } return new JsonResponse($results); }
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; }
/** * @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); $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; }
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}"); }
private function copyormovefile($source, $destination, $domove = true) { $filesystemid = $this->extractFilesystemIdFromPath($source); $path = $this->extractPathFromPath($source); $filesystemid2 = $this->extractFilesystemIdFromPath($destination); $newname = $this->extractPathFromPath($destination); $returndata = array('success' => false); $filesystems = $this->getFilesystems(); if (count($filesystems) == 0) { return $this->throwJsonError('No Filesystem found'); } else { $filesystem = isset($filesystems[$filesystemid]) ? $filesystems[$filesystemid] : null; if (!$filesystem) { return $this->throwJsonError('Filesystem not found'); } $fs = $this->getFs($filesystem); if ($fs->has($path)) { //$metadata = $fs->getMetadata($path); if ($filesystemid == $filesystemid2) { try { $fs->rename($path, $newname); } catch (FileExistsException $e) { return $this->throwJsonError($e->getMessage()); } catch (FileNotFoundException $e) { return $this->throwJsonError($e->getMessage()); } catch (NotSupportedException $e) { return $this->throwJsonError($e->getMessage()); } catch (RootViolationException $e) { return $this->throwJsonError($e->getMessage()); } catch (\Exception $e) { return $this->throwJsonError($e->getMessage()); } } else { $filesystem2 = isset($filesystems[$filesystemid2]) ? $filesystems[$filesystemid2] : null; if (!$filesystem2) { return $this->throwJsonError('Filesystem not found'); } $fs2 = $this->getFs($filesystem2); $manager = new MountManager(['fssource' => $fs, 'fsdestination' => $fs2]); try { if ($domove) { $manager->move('fssource:/' . $path, 'fsdestination:/' . $newname); } else { $manager->copy('fssource:/' . $path, 'fsdestination:/' . $newname); } } catch (FileExistsException $e) { return $this->throwJsonError($e->getMessage()); } catch (FileNotFoundException $e) { return $this->throwJsonError($e->getMessage()); } catch (NotSupportedException $e) { return $this->throwJsonError($e->getMessage()); } catch (RootViolationException $e) { return $this->throwJsonError($e->getMessage()); } catch (\Exception $e) { return $this->throwJsonError($e->getMessage()); } } $returndata = array('success' => true); } } $response = new Response(json_encode($returndata)); $response->headers->set('Content-Type', 'application/json; charset=UTF-8'); return $response; }
/** * @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; } }