/** * @param NewImageEvent $event */ private function transferImageFiles(NewImageEvent $event) { $preProcessed = $this->imagePreProcessor->preProcess($event->image()); /** @var ImagickContract $imagick */ foreach ($preProcessed as $imagick) { $this->publicFilesystem->getDriver()->put("image/{$imagick->getFilename()}", $imagick->getImageBlob(), self::fileConfig()); } }
/** * Set up new image listener with mock dependencies. */ public function setUp() { parent::setUp(); $this->imagePreProcessor = $this->makeMock(ImagePreProcessor::class); $this->config = $this->makeMock(Repository::class); $this->publicFilesystem = $this->makeMock(FilesystemAdapter::class); $this->filesystem = $this->makeMock(FilesystemInterface::class); $this->publicFilesystem->expects($this->any())->method('getDriver')->willReturn($this->filesystem); $this->newImageListener = new NewImageListener($this->imagePreProcessor, $this->publicFilesystem, $this->config); $this->image = $this->makeMock(Image::class); $this->event = new NewImageEvent($this->image); }
/** * Generates unique filename * * @param FilesystemAdapter $filesystemAdapter * @param string $extension File extension * @param null|string $path Directory path * * @return string */ public function generate(FilesystemAdapter $filesystemAdapter, $extension, $path = null) { $hash = sha1(time() + microtime()); $exists = $filesystemAdapter->exists($path . '/' . $hash . '.' . $extension); if ($exists) { $this->generate($filesystemAdapter, $extension, $path); } return $hash; }
/** *@depends testCreateBucket */ public function testPutFile($s3_client) { //start create laravel filesystem adapter $cloud_adapter = new AwsS3Adapter($s3_client, $this->bucket_name, $this->root_folder_in_bucket); $filesystem = new Filesystem($cloud_adapter); $laravel_filesystem_adapter = new FilesystemAdapter($filesystem); //end create laravel filesystem adapter //start pul file $laravel_filesystem_adapter->put($this->bucket_file_path, $this->bucket_file_content, FilesystemContract::VISIBILITY_PUBLIC); //end put file //start check file exists $this->assertTrue($laravel_filesystem_adapter->exists($this->bucket_file_path)); //end check file exists //start check contect of file $this->assertEquals($this->bucket_file_content, $laravel_filesystem_adapter->get($this->bucket_file_path)); //end check contect of file return $laravel_filesystem_adapter; }
/** * @param $currentFile * @param $newFile * @param bool $isFolder * * @return bool|string */ public function move($currentFile, $newFile, $isFolder = false) { if ($isFolder) { if ($newFile == $currentFile) { return 'Please select another folder to move this folder into'; } if (starts_with($newFile, $currentFile)) { return 'You can not move this folder inside of itself'; } } if ($this->disk->exists($newFile)) { return 'File already exists.'; } return $this->disk->getDriver()->rename($currentFile, $newFile); }
/** * Get the Flysystem driver. * * @return \League\Flysystem\FilesystemInterface * @static */ public static function getDriver() { return \Illuminate\Filesystem\FilesystemAdapter::getDriver(); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Aws\S3\S3Client; use League\Flysystem\AwsS3v3\AwsS3Adapter; use League\Flysystem\Filesystem; use Illuminate\Filesystem\FilesystemAdapter; //start define settings $end_point = 'http://webapp.dev:4569'; $bucket_region = 'us-east-1'; $bucket_name = 'default1bucket'; $root_folder_in_bucket = 'news'; $file_path = 'test.jpg'; //end define settings //start create S3 client $s3_client = new S3Client(['credentials' => ['key' => 'AKIAIOSFODNN7EXAMPLE', 'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'], 'region' => $bucket_region, 'version' => 'latest', 'endpoint' => $end_point, 'scheme' => 'http']); //end create S3 client //start create bucket $s3_client->createBucket(['ACL' => 'private', 'Bucket' => $bucket_name, 'CreateBucketConfiguration' => ['LocationConstraint' => $bucket_region]]); //end create bucket //start create laravel filesystem adapter $cloud_adapter = new AwsS3Adapter($s3_client, $bucket_name, $root_folder_in_bucket); $filesystem = new Filesystem($cloud_adapter); $disk = new FilesystemAdapter($filesystem); //end create laravel filesystem adapter //start put file on disk if (!$disk->exists($file_path)) { $disk->put($file_path, file_get_contents('../resources/assets/img/test.jpg')); } //start put file on disk print "<img src='" . $disk->url($file_path) . "'/>";
/** * @param File $localFile * @param string $remotePath * @param string|null $filename * * @return bool */ private function copyToRemote(File $localFile, $remotePath, $filename = null) { $filename = $filename ? $filename : $localFile->getBasename(); $success = $this->storage->put($remotePath . '/' . $filename, file_get_contents($localFile->getPathname())); return $success; }
/** * @param FilesystemAdapter $disk */ private function deleteFiles(FilesystemAdapter $disk) { foreach ($this->getFiles() as $file) { $disk->delete($file); } }
/** * Renames existing file * * @param string $oldFileName * @param string $newFileName * @param string $context * @param FilesystemAdapter $filesystemAdapter * * @return boolean */ public function rename($oldFileName, $newFileName, $context, FilesystemAdapter $filesystemAdapter) { if ($filesystemAdapter->exists($context . '/' . $newFileName)) { throw new IcrRuntimeException("File with name {$newFileName} already exists!"); } $image = $filesystemAdapter->get($context . '/' . $oldFileName); $filesystemAdapter->move($context . '/' . $oldFileName, $context . '/' . $newFileName); foreach ($this->config[$context] as $sizeName => $value) { $oldPath = $context . '/' . $sizeName . '/' . $oldFileName; $newPath = $context . '/' . $sizeName . '/' . $newFileName; $filesystemAdapter->move($oldPath, $newPath); } return true; }