public function testGetDefaultExtensionIsBasedOnMimeType() { $file = new File(__DIR__ . '/Fixtures/test'); $guesser = $this->createMockGuesser($file->getPath(), 'image/gif'); MimeTypeGuesser::getInstance()->register($guesser); $this->assertEquals('.gif', $file->getDefaultExtension()); }
/** * Transforms a File instance to a path * * @param File $file The file * * @return string The path to the file * * @throws UnexpectedTypeException if the given file is not an instance of File */ public function transform($file) { if (null === $file || '' === $file) { return ''; } if (!$file instanceof File) { throw new UnexpectedTypeException($file, 'Symfony\\Component\\HttpFoundation\\File\\File'); } return $file->getPath(); }
/** * Set the avatar of the object to be a specific file * * @param File|null $file The avatar file * @return self */ public function setAvatarFile($file) { if ($file) { // We don't use File's fread() because it's unavailable in less // recent PHP versions $path = $file->getPath() . '/' . $file->getFilename(); $content = file_get_contents($path); $path = $this->getAvatarPath(null, false, false); $filename = $this->getAvatarFileName($content); $file->move(DOC_ROOT . $path, $filename); $this->setAvatar($path . $filename); } return $this; }
public function testMove() { $path = __DIR__ . '/Fixtures/test.copy.gif'; $targetPath = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'test.target.gif'; @unlink($path); @unlink($targetPath); copy(__DIR__ . '/Fixtures/test.gif', $path); $file = new File($path); $file->move($targetPath); $this->assertTrue(file_exists($targetPath)); $this->assertFalse(file_exists($path)); $this->assertEquals($targetPath, $file->getPath()); @unlink($path); @unlink($targetPath); }
/** * Validates the file as being supported by Colibri, verifying its size, extension... * @param $file * @return boolean * @throws \Exception */ public function supported(File $file) { $filePath = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename(); $fileData = getimagesize($filePath); $maxSize = 1101004; // 1.05 Mo $mimes = array('image/jpeg', 'image/png', 'image/gif'); $fileSize = $file->getSize(); $fileType = $file->getMimeType(); if ($fileSize > $maxSize) { throw new \Exception("File size too big. Got {$fileSize}, max {$maxSize}"); } if (!in_array($fileType, $mimes)) { throw new \Exception("File extension not supported. Got {$fileType}."); } if ($fileData[0] > 2400 || $fileData[1] > 2400) { throw new \Exception("L'image est trop grande, max 2400x2400, reçu {$fileData['0']}x{$fileData['1']}px"); } return true; }
public function testMoveFailing() { $path = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'test.copy.gif'; $targetPath = '/thisfolderwontexist'; @unlink($path); @unlink($targetPath); copy(__DIR__ . '/Fixtures/test.gif', $path); $file = new File($path); try { $file->move($targetPath); $this->fail('File::move should throw an exception.'); } catch (FileException $e) { } $this->assertFileExists($path); $this->assertFileNotExists($path . $targetPath . 'test.gif'); $this->assertEquals($path, $file->getPath()); @unlink($path); @unlink($targetPath); }
/** * If manually uploading a file (i.e. not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter * must be able to accept an instance of 'File' as the bundle will inject one here * during Doctrine hydration. * * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $newFile * * @return ProtectedFile */ public function setFile(File $newFile = null) { $this->file = $newFile; if ($newFile) { // It is required that at least one field changes if you are using doctrine // otherwise the event listeners won't be called and the file is lost $this->updatedAt = new \DateTime('now'); $this->setPath($newFile->getPath()); } return $this; }
public function testMoveFailing() { $path = __DIR__.'/Fixtures/test.copy.gif'; $targetPath = '/thisfolderwontexist'; @unlink($path); @unlink($targetPath); copy(__DIR__.'/Fixtures/test.gif', $path); $file = new File($path); $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException'); $file->move($targetPath); $this->assertFileExists($path); $this->assertFileNotExists($path.$targetPath.'test.gif'); $this->assertEquals($path, $file->getPath()); @unlink($path); @unlink($targetPath); }
/** * {@inheritdoc} */ public function path() { return $this->source->getPath() . '/' . $this->source->getFilename(); }