/** * @param AssetInterface $asset * @param UploadedFile $file * * @return AssetVersion */ public function createVersionFromFile(AssetInterface $asset, UploadedFile $file) { list($width, $height) = getimagesize($file->getRealPath()); $extension = File::extension($file->getClientOriginalName(), $file->getMimetype()); $version = $this->version->create(['asset_id' => $asset->getId(), 'extension' => $extension, 'filesize' => $file->getClientSize(), 'filename' => $file->getClientOriginalName(), 'width' => $width, 'height' => $height, 'edited_at' => time(), 'edited_by' => Auth::getPerson()->getId(), 'mimetype' => $file->getMimeType(), 'metadata' => File::exif($file->getRealPath())]); $file->move($asset->directory(), $version->id); return $version; }
/** * Orientate the image. * * @param UploadedFile $file * @param $orientation * @return UploadedFile */ protected function orientate(UploadedFile $file, $orientation) { $image = imagecreatefromjpeg($file->getRealPath()); switch ($orientation) { case 2: imageflip($image, IMG_FLIP_HORIZONTAL); break; case 3: $image = imagerotate($image, 180, 0); break; case 4: $image = imagerotate($image, 180, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 5: $image = imagerotate($image, -90, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 6: $image = imagerotate($image, -90, 0); break; case 7: $image = imagerotate($image, 90, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 8: $image = imagerotate($image, 90, 0); break; } imagejpeg($image, $file->getRealPath(), 90); return new UploadedFile($file->getRealPath(), $file->getClientOriginalName(), $file->getMimeType(), $file->getSize()); }
public static function upload(UploadedFile $file, $bucketName) { if (!$file->isValid()) { throw new \Exception(trans('validation.invalid_file')); } $bucket = Bucket::find($bucketName); if (!empty($bucket->mimeTypes()) && !in_array($file->getMimeType(), $bucket->mimeTypes())) { throw new \Exception(trans('validation.invalid_file_type')); } if (!empty($bucket->maxSize()) && !in_array($file->getClientSize(), $bucket->maxSize())) { throw new \Exception(trans('validation.invalid_file_size')); } $disk = Storage::disk($bucket->disk()); $media = Media::create(['mime' => $file->getMimeType(), 'bucket' => $bucketName, 'ext' => $file->guessExtension()]); $disk->put($bucket->path() . '/original/' . $media->fileName, File::get($file)); if (is_array($bucket->resize())) { foreach ($bucket->resize() as $name => $size) { $temp = tempnam(storage_path('tmp'), 'tmp'); Image::make(File::get($file))->fit($size[0], $size[1])->save($temp); $disk->put($bucket->path() . '/' . $name . '/' . $media->fileName, File::get($temp)); unlink($temp); } } return $media; }
public function mockStore(Picture $pic, UploadedFile $dummy) { if ($dummy->getMimeType() != 'image/png') { throw new \Exception('fail'); } $pic->setStorageKey('123.jpg'); $pic->setMimeType($dummy->getMimeType()); }
public function upload(UploadedFile $file) { if (!in_array($file->getMimeType(), $this->allowedMimeTypes)) { throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getMimeType())); } $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $file->getClientOriginalExtension()); $adapter = $this->filesystem->getAdapter(); $adapter->setMetadata($filename, array('contentType' => $file->getMimeType())); $adapter->write($filename, file_get_contents($file->getPathname())); return $adapter->getUrl($filename); }
/** * Process image file, make it progressive * @param UploadedFile $file */ public function processFile(UploadedFile $file) { $this->file = $file; foreach (self::$IMAGE_TYPES as $type) { if (in_array($this->file->getMimeType(), $type['mime_types'])) { $filePath = $this->file->getRealPath(); $typeObject = new $type['class']($filePath); $typeObject->process(); break; } } }
/** * Upload (move) file to branch logos directory * @param UploadedFile $logo * @param null|string $targetFilename * @return \Symfony\Component\HttpFoundation\File\File * @throws LogoHandlerLogicException */ public function upload(UploadedFile $logo, $targetFilename = null) { if (!in_array($logo->getMimeType(), $this->permittedMimeTypes)) { throw new LogoHandlerLogicException(sprintf('"%s" file type is not permitted. Use images for logo and try again.', $logo->getMimeType())); } if (is_null($targetFilename)) { $targetFilename = sha1(uniqid(mt_rand(), true)) . '.' . $logo->guessExtension(); } if (false === $this->branchLogoDir->isDir() || false === $this->branchLogoDir->isWritable()) { throw new \RuntimeException(sprintf("Branch logo directory (%s) is not writable, doesn't exist or no space left on the disk.", $this->branchLogoDir->getRealPath())); } return $logo->move($this->branchLogoDir->getRealPath(), $targetFilename); }
/** * {@inheritdoc} */ public function canUpload(UploadedFile $file) { $mimeType = $file->getMimeType(); if (in_array($mimeType, $this->getSupportedMimeTypes())) { return 5; } if ($file->getMimeType() == 'application/ogg') { // This could be a video or audio file. $meta = GenericMetadataReader::readMetadata($file->getPathname()); if (isset($meta['audio']['dataformat'])) { return 5; } } return 0; }
/** * @param UploadedFile $file * @param array $methods * * @return mixed|void * * @throws InvalidFileTypeException * @throws InvalidFileException * @throws UploadFileNotSetException * @throws MaxFileSizeExceededException */ public function validate(UploadedFile $file, $methods = [self::VALIDATOR_FILE_SET, self::VALIDATOR_FILE_ERRORS, self::VALIDATOR_BLOCK_FILE_TYPES, self::VALIDATOR_MAX_FILE_SIZE]) { if (in_array(self::VALIDATOR_FILE_ERRORS, $methods) && $file->getError() > 0) { throw new InvalidFileException(sprintf('The file upload had an error("%s: %s")', $file->getError(), $file->getErrorMessage())); } if (in_array(self::VALIDATOR_FILE_SET, $methods) && $file->getFilename() == '') { throw new UploadFileNotSetException(sprintf('No file "%s" was set', $file->getFilename())); } if (in_array(self::VALIDATOR_BLOCK_FILE_TYPES, $methods) && in_array($file->getMimeType(), $this->blockedMimeTypes)) { throw new InvalidFileTypeException(sprintf('The file type "%s" was blocked', $file->getMimeType())); } if (in_array(self::VALIDATOR_MAX_FILE_SIZE, $methods) && $this->maxFileSize !== null && $file->getSize() >= $this->maxFileSize) { throw new MaxFileSizeExceededException(sprintf('File "%s" exceeds the configured maximum filesize of "%s"', $file->getFilename(), $this->maxFilesize)); } }
/** * {@inheritdoc} */ public function canUpload(UploadedFile $file) { if ($file->getMimeType() == 'text/plain' && $file->getClientOriginalExtension() == 'md') { return 5; } return 0; }
/** * {@inheritDoc} */ protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) { $fs = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir . '/' . $name : $name; $stream = fopen($file->getRealPath(), 'r+'); $fs->writeStream($path, $stream, array('mimetype' => $file->getMimeType())); }
/** * {@inheritdoc} */ public function canUpload(UploadedFile $file) { $mimeType = $file->getMimeType(); if (in_array($mimeType, $this->getSupportedMimeTypes())) { return 4; } return 0; }
public function testFileUploadsWithNoMimeType() { $file = new UploadedFile(__DIR__ . '/Fixtures/test.gif', 'original.gif', null, filesize(__DIR__ . '/Fixtures/test.gif'), UPLOAD_ERR_OK); $this->assertEquals('application/octet-stream', $file->getClientMimeType()); if (extension_loaded('fileinfo')) { $this->assertEquals('image/gif', $file->getMimeType()); } }
/** * {@inheritDoc} */ protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) { $filesystem = $this->getFilesystem($mapping); $path = !empty($dir) ? $dir . '/' . $name : $name; if ($filesystem->getAdapter() instanceof MetadataSupporter) { $filesystem->getAdapter()->setMetadata($path, array('contentType' => $file->getMimeType())); } $filesystem->write($path, file_get_contents($file->getPathname()), true); }
/** * Checks if the passed value is valid. * * @param UploadedFile $file The value that should be validated * @param Constraint $constraint The constraint for the validation */ public function validate($file, Constraint $constraint) { if (!in_array($file->getMimeType(), $this->mimeTypes)) { $this->context->buildViolation($constraint->messageMimeTypes)->atPath('file')->addViolation(); } if ($file->getSize() > $file->getMaxFilesize()) { $this->context->buildViolation($constraint->messageMaxSize, array('%max_size%' => $file->getMaxFilesize()))->atPath('file')->addViolation(); } }
public function createAttachmentFromUpload(UploadedFile $file) { $attachment = new Attachment(); $attachment->setUuid(Uuid::uuid4()); $attachment->setFileName($attachment->getUuid() . '.' . $file->guessExtension()); $attachment->setMimeType($file->getMimeType()); $file->move(self::UPLOAD_PATH, $attachment->getFileName()); return $attachment; }
public function create(UploadedFile $file, User $user) { $filename = sha1(rand(11111, 99999)); $extension = $file->getClientOriginalExtension(); Storage::put('images/' . $filename . '.' . $extension, file_get_contents($file->getRealPath())); $image = new Image(['user_id' => $user->id, 'filename' => $file->getClientOriginalName(), 'path' => $filename . '.' . $extension, 'mime_type' => $file->getMimeType(), 'location' => 'local', 'status' => Image::STATUS_PENDING]); $image->save(); return $image; }
public function setFile(\Symfony\Component\HttpFoundation\File\UploadedFile $file) { // temporarily save file for further handling $this->tempFile = $file; $this->filename = $file->getClientOriginalName(); $this->size = $file->getClientSize(); $this->extension = $file->getClientOriginalExtension(); $this->mimetype = $file->getMimeType(); }
/** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null === $this->file) { return; } $this->extention = $this->file->guessExtension(); $this->name = str_replace('.' . $this->extention, '', $this->file->getClientOriginalName()); $this->slugify(); $this->mimeType = $this->file->getMimeType(); }
/** * @param UploadedFile $uploadedFile * @param FileEntity $fileEntity * @return bool */ public function putUploadedFile(UploadedFile $uploadedFile, FileEntity $fileEntity) { list($path, $filesystem, $settings) = $this->getConfigValuesForRecordType($fileEntity->getRecordType()); $fullPath = $path . DIRECTORY_SEPARATOR . $fileEntity->getFileName(); $this->getFilesystem($filesystem)->putStream($fullPath, fopen($uploadedFile->getPathname(), 'r'), $settings); $fileEntity->setContentType($uploadedFile->getMimeType())->setSize($uploadedFile->getSize()); /** @var EntityManager $em */ $em = $this->container['orm.em']; $em->flush(); return $fileEntity; }
/** * Creates a file object from a file an uploaded file. * @param Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile */ public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; } $this->file_name = $uploadedFile->getClientOriginalName(); $this->file_size = $uploadedFile->getClientSize(); $this->content_type = $uploadedFile->getMimeType(); $this->disk_name = $this->getDiskName(); $this->putFile($uploadedFile->getRealPath(), $this->disk_name); }
/** * @param UploadedFile $input Uploaded Image File * * @return string|false */ public function parse($input = null) { if ($input instanceof UploadedFile && in_array($input->getMimeType(), $this->allowedTypes)) { $this->uploadedFile = $input; $this->temporaryName = $imageName = $this->getHash() . "-{$input->getClientOriginalName()}"; $input->move($this->storageFolderPath, $imageName); return $this->relativeFolderPath . '/' . $imageName; } else { return false; } }
/** * Save an uploaded file into the standard system. * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile * @return File|false */ public static function createFromUploaded($uploadedFile) { if (!$uploadedFile instanceof UploadedFile) { return false; } $file = new File(); $file->name = $uploadedFile->getClientOriginalName(); $file->mime_type = $uploadedFile->getMimeType(); $file->save(); $uploadedFile->move(dirname($file->getPathname()), basename($file->getPathname())); return $file; }
/** * @param UploadedFile $file * @return Document */ public function add(UploadedFile $file) { $document = new Document(); $document->setExtension($file->guessExtension())->setMime($file->getMimeType())->setName($file->getClientOriginalName())->setSize($file->getSize()); if (is_null($document->getExtension())) { $document->setExtension($file->getClientOriginalExtension()); } $this->em->persist($document); $this->em->flush(); $file->move($this->directory . '/' . substr($document->getId(), 0, 1), $document->getId() . '.' . $document->getExtension()); return $document; }
public static function createFromFile(UploadedFile $file) { $upload = new Upload(); $extension = $file->getClientOriginalExtension(); $mimeType = $file->getMimeType(); $fileType = in_array($extension, static::$image_types) ? 'image' : 'document'; $upload->setExtension($extension); $upload->setType($fileType); $upload->setMimeType($mimeType); $upload->file = $file; return $upload; }
/** * Deplace un fichier uploader vers son dossier de destination en s'assurant que se fichier n'efface pas les fichiers precedents * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $fichier le fichier à uploader * @param string $destination le chemin qui mene vers le dossier de destination * @param string $webpath le chemin web du fichier qui sera retourner concatener avec le nom du fichier * * @throws \Symfony\Component\HttpFoundation\File\Exception\UnexpectedTypeException * * @return string le nom du fichier deplacer avec ou sans le chemin web */ public static function uploadImage(UploadedFile &$fichier, $destination, $webpath = '') { if (in_array($fichier->getMimeType(), self::$imageMimeType)) { return $webpath . self::uploadFile($fichier, $destination); } else { $str = ''; foreach (self::$imageMimeType as $image) { $str .= $image; } throw new UnexpectedTypeException($fichier->getMimeType(), $str); } }
public function isImage(UploadedFile $file) { $result = false; $extensions = ['jpg', 'jpeg', 'gif', 'png']; $mimeTypes = ['image/jpeg', 'image/jpeg', 'image/gif', 'image/png']; $fileExt = $file->getClientOriginalExtension(); $fileMime = $file->getMimeType(); if (in_array($fileExt, $extensions, true) && in_array($fileMime, $mimeTypes, true)) { $result = true; } return $result; }
public function handleUploadImageFile(WebsiteOptions $options, UploadedFile $uploadedFile, $imageStr) { if ($uploadedFile->getMimeType() == 'image/png' || $uploadedFile->getMimeType() == 'image/jpg' || $uploadedFile->getMimeType() == 'image/jpeg') { $newFileName = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension(); $oldFileName = null; $getImageValue = 'get' . ucfirst($imageStr); $setImageValue = 'set' . ucfirst($imageStr); $uploadDir = $this->webDir . DIRECTORY_SEPARATOR . $options->getUploadDir(); if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $realpathUploadRootDir = realpath($uploadDir); if (false === $realpathUploadRootDir) { throw new \Exception(sprintf("Invalid upload root dir '%s'for uploading website images.", $uploadDir)); } if ($options->{$getImageValue}() !== $newFileName) { $oldFileName = $options->{$getImageValue}(); $options->{$setImageValue}($newFileName); try { $uploadedFile->move($realpathUploadRootDir, $newFileName); $this->om->persist($options); $this->om->flush(); } catch (\Exception $e) { if (file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName); } $options->{$setImageValue}($oldFileName); throw new \InvalidArgumentException($e->getMessage()); } if (null !== $oldFileName && !filter_var($oldFileName, FILTER_VALIDATE_URL) && file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName); } } return array($imageStr => $options->getWebPath($imageStr)); } else { throw new \InvalidArgumentException(); } }
/** * @param string $email * @return UploadedFile * @throws InvalidArgumentException */ public function getFile($email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new InvalidArgumentException(); } $hash = md5(strtolower(trim($email))); $guzzle = new Client(); $tempDirectory = sys_get_temp_dir(); $guzzle->request("GET", "http://gravatar.com/avatar/" . $hash . "?d=404", [RequestOptions::SINK => $tempDirectory . '/' . $hash]); $file = new File($tempDirectory . '/' . $hash); $fileOriginalName = $hash . '.' . $file->guessExtension(); $file = new UploadedFile($tempDirectory . '/' . $hash, $fileOriginalName, $file->getMimeType(), $file->getSize(), null, true); return $file; }
/** * @param FileInterface $storageFile * @param UploadedFile $uploadData */ public function createFromUploadedFile(FileInterface $storageFile, UploadedFile $uploadData) { $fileSystem = new Filesystem(); $storageFile->setFileName($uploadData->getClientOriginalName()); $storageFile->setMimeType($uploadData->getMimeType()); $storageFile->setSize($uploadData->getSize()); if (!$storageFile->getId()) { $this->save($storageFile); } else { $fileSystem->remove($this->getFilePath($storageFile)); } $storageFile->setStorageId(uniqid($storageFile->getId() . '_', false)); $uploadData->move($this->getStorageDirectory($storageFile), $storageFile->getStorageId()); $this->save($storageFile); }