/** * Create track from local hard drive with job service * * @param MultimediaObject $multimediaObject * @param UploadedFile $file * @param string $profile * @param int $priority * @param string $language * @param array $description * @return MultimediaObject */ public function createTrackFromLocalHardDrive(MultimediaObject $multimediaObject, UploadedFile $trackFile, $profile, $priority, $language, $description) { if (null === $this->profileService->getProfile($profile)) { throw new \Exception("Can't find given profile with name " . $profile); } if (UPLOAD_ERR_OK != $trackFile->getError()) { throw new \Exception($trackFile->getErrorMessage()); } if (!is_file($trackFile->getPathname())) { throw new FileNotFoundException($trackFile->getPathname()); } $pathFile = $trackFile->move($this->tmpPath . "/" . $multimediaObject->getId(), $trackFile->getClientOriginalName()); $this->jobService->addJob($pathFile, $profile, $priority, $multimediaObject, $language, $description); return $multimediaObject; }
/** * @param UploadedFile $file * * @return Uploader * * @throws UploaderException */ public function init(UploadedFile $file) { // check if file correct if (!$file->isValid()) { throw new UploaderException($file->getErrorMessage()); } $this->file = $file; return $this; }
/** * @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)); } }
/** * Set a pic from an url into the event */ public function addPicFile(Event $event, UploadedFile $picFile) { if (UPLOAD_ERR_OK != $picFile->getError()) { throw new \Exception($picFile->getErrorMessage()); } if (!is_file($picFile->getPathname())) { throw new FileNotFoundException($picFile->getPathname()); } $path = $picFile->move($this->targetPath . "/" . $event->getId(), $picFile->getClientOriginalName()); $pic = new Pic(); $pic->setUrl(str_replace($this->targetPath, $this->targetUrl, $path)); $pic->setPath($path); $event->setPic($pic); $this->dm->persist($event); $this->dm->flush(); return $event; }
protected function checkFile(UploadedFile $file) { $mime = $file->getClientMimeType(); $info = array('name' => $file->getClientOriginalName(), 'size' => $file->getClientSize(), 'mime' => $mime, 'extension' => $file->getClientOriginalExtension(), 'type' => $mime, 'error' => ''); if ($file->isValid()) { if (!$info['type']) { $info['status'] = 9; $info['error'] = '不支持的文件类型'; } else { $info['status'] = UPLOAD_ERR_OK; } } else { $info['status'] = $file->getError(); $info['error'] = $file->getErrorMessage(); } return $info; }
/** * Add a material from a file into the multimediaObject */ public function addMaterialFile(MultimediaObject $multimediaObject, UploadedFile $materialFile, $formData) { if (UPLOAD_ERR_OK != $materialFile->getError()) { throw new \Exception($materialFile->getErrorMessage()); } if (!is_file($materialFile->getPathname())) { throw new FileNotFoundException($materialFile->getPathname()); } $material = new Material(); $material = $this->saveFormData($material, $formData); $path = $materialFile->move($this->targetPath . "/" . $multimediaObject->getId(), $materialFile->getClientOriginalName()); $material->setPath($path); $material->setUrl(str_replace($this->targetPath, $this->targetUrl, $path)); $multimediaObject->addMaterial($material); $this->dm->persist($multimediaObject); $this->dm->flush(); return $multimediaObject; }
/** * Set a pic from an url into the series */ public function addPicFile(Series $series, UploadedFile $picFile, $isBanner = false, $bannerTargetUrl = "") { if (UPLOAD_ERR_OK != $picFile->getError()) { throw new \Exception($picFile->getErrorMessage()); } if (!is_file($picFile->getPathname())) { throw new FileNotFoundException($picFile->getPathname()); } $path = $picFile->move($this->targetPath . "/" . $series->getId(), $picFile->getClientOriginalName()); $pic = new Pic(); $pic->setUrl(str_replace($this->targetPath, $this->targetUrl, $path)); $pic->setPath($path); if ($isBanner) { $pic->setHide(true); $pic->addTag('banner'); $series = $this->addBanner($series, $pic->getUrl(), $bannerTargetUrl); } // TODO: add pic the latest if it is banner $series->addPic($pic); $this->dm->persist($series); $this->dm->flush(); return $series; }
/** * @param UploadedFile $uploadedFile * @throws InvalidUploadException * @throws MaxFileSizeExceededException * @throws InvalidExtensionException * @throws InvalidMimeTypeException */ protected function validateUploadedFile(UploadedFile $uploadedFile) { if (!$uploadedFile->isValid()) { throw new InvalidUploadException($uploadedFile->getErrorMessage()); } if ($this->maxUploadSize() < $uploadedFile->getSize()) { throw new MaxFileSizeExceededException('Uploaded file exceeded maximum allowed upload size.'); } if (!in_array($uploadedFile->getExtension(), $this->allowedExtensions())) { throw new InvalidExtensionException('Files with extension (' . $uploadedFile->getExtension() . ') are not allowed'); } if (!in_array($uploadedFile->getMimeType(), $this->allowedMimeTypes())) { throw new InvalidMimeTypeException('Files with mime type (' . $uploadedFile->getMimeType() . ' are not allowed'); } }
/** * Upload file from the request * * @param UploadedFile $file * @return Array $data Retrieve into the content of response * @throws BadRequestHttpException The file is too big */ private function doRequestUpload(UploadedFile $file) { $tmpDirectory = $this->getApplication()->getTemporaryDir(); $data = []; if (null !== $file) { if ($file->isValid()) { if ($file->getClientSize() <= $file->getMaxFilesize()) { $data = $this->buildData($file->getClientOriginalName(), $file->guessExtension()); $file->move($tmpDirectory, $data['filename']); $data['size'] = round($file->getClientSize() / 1024, 2); if ($imageInfo = @getimagesize($data['path'])) { if (isset($imageInfo[0]) && isset($imageInfo[1])) { $data['width'] = $imageInfo[0]; $data['height'] = $imageInfo[1]; } } else { $data['width'] = 0; $data['height'] = 0; } } else { throw new BadRequestHttpException('Too big file, the max file size is ' . $file->getMaxFilesize()); } } else { throw new BadRequestHttpException($file->getErrorMessage()); } } return $data; }
/** * file upload to storage * * @param UploadedFile $uploaded uploaded file instance * @param string $path be saved path * @param string|null $name be saved file name * @param string|null $disk disk name (ex. local, ftp, s3 ...) * @param UserInterface $user user instance * @return File */ public function upload(UploadedFile $uploaded, $path, $name = null, $disk = null, UserInterface $user = null) { if ($uploaded->isValid() === false) { throw new InvalidFileException(['name' => $uploaded->getClientOriginalName(), 'detail' => $uploaded->getErrorMessage()]); } $id = $this->keygen->generate(); $name = $name ?: $this->makeFilename($uploaded->getClientOriginalName()); $path = $this->makePath($id, $path); $disk = $disk ?: $this->distributor->allot($uploaded); $user = $user ?: $this->auth->user(); if (!$this->files->store(file_get_contents($uploaded->getPathname()), $path . '/' . $name, $disk)) { throw new WritingFailException(); } $file = $this->createModel(); $file->id = $id; $file->userId = $user->getId(); $file->disk = $disk; $file->path = $path; $file->filename = $name; $file->clientname = $uploaded->getClientOriginalName(); $file->mime = $uploaded->getMimeType(); $file->size = $uploaded->getSize(); $file->save(); return $file; }
/** * Returns the upload error message. * * @return string upload error */ public function getErrorMessage() { return $this->uploadedFile->getErrorMessage(); }
/** * Upload file from the request * * @param UploadedFile $file * @return Array $data Retrieve into the content of response * @throws BadRequestHttpException The file is too big */ private function doRequestUpload(UploadedFile $file) { $tmpDirectory = $this->getParameter("kernel.cache_dir"); $data = []; if (null !== $file) { if ($file->isValid()) { if ($file->getClientSize() <= $file->getMaxFilesize()) { $data = $this->buildData($file->getClientOriginalName(), $file->guessExtension()); $file->move($tmpDirectory, $data['filename']); } else { throw new BadRequestHttpException('Too big file, the max file size is ' . $file->getMaxFilesize()); } } else { throw new BadRequestHttpException($file->getErrorMessage()); } } return $data; }