Exemplo n.º 1
5
 /**
  * @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;
 }
Exemplo n.º 2
0
 /**
  * Upload new attachment
  *
  * @param UploadedFile $file
  * @param string       $descriptionText
  * @param Language     $language
  * @param array        $attributes
  * @param Attachment   $attachment
  *
  * @return Attachment
  */
 public function upload(UploadedFile $file, $descriptionText, Language $language, array $attributes, Attachment $attachment = null)
 {
     $filesystem = new Filesystem();
     $filesize = $file->getClientSize();
     if ($filesize == false) {
         throw new FileException('File size is not valid');
     }
     if (!file_exists($this->config['file_directory']) || !is_writable($this->config['file_directory'])) {
         throw new FileException('Directory ' . $this->config['file_directory'] . ' is not writable');
     }
     if (!is_null($attachment)) {
         if ($filesystem->exists($this->getStorageLocation($attachment))) {
             $filesystem->remove($this->getStorageLocation($attachment));
         }
         if ($descriptionText != $attachment->getDescription()->getTranslationText()) {
             $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
             $description = new Translation($nextTranslationPhraseId);
             $description->setLanguage($language);
             $description->setTranslationText($descriptionText);
             $this->em->persist($description);
         }
         unset($attributes['description']);
     } else {
         $attachment = new Attachment();
         $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
         $description = new Translation($nextTranslationPhraseId);
         $description->setLanguage($language);
         $description->setTranslationText($descriptionText);
         unset($attributes['description']);
         $attachment->setCreated(new \DateTime());
         $this->em->persist($description);
         $this->em->persist($attachment);
     }
     $attributes = array_merge(array('language' => $language, 'name' => $file->getClientOriginalName(), 'extension' => $file->getClientOriginalExtension(), 'mimeType' => $file->getClientMimeType(), 'contentDisposition' => Attachment::CONTENT_DISPOSITION, 'sizeInBytes' => $file->getClientSize(), 'description' => $description), $attributes);
     $this->fillAttachment($attachment, $attributes);
     if (is_null($attributes['name'])) {
         $attachment->setName($file->getClientOriginalName());
     }
     $this->em->flush();
     $target = $this->makeDirectories($attachment);
     try {
         $file->move($target, $this->getFileName($attachment));
         $filesystem->chmod($target . '/' . $this->getFileName($attachment), 0644);
     } catch (\Exceptiom $e) {
         $filesystem->remove($target);
         $this->em->remove($attachment);
         $this->em->flush();
         throw new \Exception($e->getMessage(), $e->getCode());
     }
     return $attachment;
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
 /**
  * Handle the file upload. Returns the array on success, or false
  * on failure.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @param String $path where to upload file
  * @return array|bool
  */
 public function handle(UploadedFile $file, $path = 'uploads')
 {
     $input = array();
     $fileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
     // Detect and transform Croppa pattern to avoid problem with Croppa::delete()
     $fileName = preg_replace('#([0-9_]+)x([0-9_]+)#', "\$1-\$2", $fileName);
     $input['path'] = $path;
     $input['extension'] = '.' . $file->getClientOriginalExtension();
     $input['filesize'] = $file->getClientSize();
     $input['mimetype'] = $file->getClientMimeType();
     $input['filename'] = $fileName . $input['extension'];
     $fileTypes = Config::get('file.types');
     $input['type'] = $fileTypes[strtolower($file->getClientOriginalExtension())];
     $filecounter = 1;
     while (file_exists($input['path'] . '/' . $input['filename'])) {
         $input['filename'] = $fileName . '_' . $filecounter++ . $input['extension'];
     }
     try {
         $file->move($input['path'], $input['filename']);
         list($input['width'], $input['height']) = getimagesize($input['path'] . '/' . $input['filename']);
         return $input;
     } catch (FileException $e) {
         Notification::error($e->getmessage());
         return false;
     }
 }
Exemplo n.º 5
0
 public function upload(UploadedFile $file)
 {
     if ($file->isValid()) {
         $name = $file->getClientOriginalName();
         $size = $file->getClientSize();
     }
 }
Exemplo n.º 6
0
 /**
  * @param string|array|UploadedFile $data
  *
  * @return bool
  */
 protected function isFileUpload($data)
 {
     if ($data instanceof UploadedFile) {
         return $data->isValid() && $data->getClientSize() > 0;
     }
     return is_array($data) && !empty($data['tmp_name']) && !empty($data['size']) && $data['error'] === UPLOAD_ERR_OK;
 }
Exemplo n.º 7
0
 /**
  * @param UploadedFile $file
  * @return bool
  */
 protected function validSize(UploadedFile $file)
 {
     $size = $file->getClientSize();
     if ($size > self::MAX_FILE_SIZE) {
         return false;
     }
     return true;
 }
Exemplo n.º 8
0
 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();
 }
Exemplo n.º 9
0
 /**
  * @param UploadedFile $file
  *
  * @return \WellCommerce\Bundle\MediaBundle\Entity\MediaInterface
  */
 protected function createMediaFromUploadedFile(UploadedFile $file)
 {
     $media = $this->initResource();
     $media->setName($file->getClientOriginalName());
     $media->setExtension($file->guessClientExtension());
     $media->setMime($file->getClientMimeType());
     $media->setSize($file->getClientSize());
     return $media;
 }
Exemplo n.º 10
0
 /**
  * @param UploadedFile $uploadedFile
  */
 function __construct(UploadedFile $uploadedFile)
 {
     $cleanFilename = preg_replace("/[^A-Za-z0-9\\.]/", "-", ucwords(strtolower($uploadedFile->getClientOriginalName())));
     $path = uniqid() . '-' . $cleanFilename;
     $this->setPath($path);
     $this->setSize($uploadedFile->getClientSize());
     $this->setName($cleanFilename);
     $uploadedFile->move($this->getUploadRootDir(), $path);
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function save(UploadedFile $file, $dir)
 {
     $media = new Media();
     $media->setName($file->getClientOriginalName());
     $media->setExtension($file->guessClientExtension());
     $media->setMime($file->getClientMimeType());
     $media->setSize($file->getClientSize());
     $this->_em->persist($media);
     $this->_em->flush();
     return $media;
 }
Exemplo n.º 12
0
 /**
  * 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);
 }
Exemplo n.º 13
0
 /**
  * @param UserModel $user
  * @param UploadedFile $file
  * @param ImageTypeModel $image_type
  *
  * @throws InvalidCreationException
  * @return ImageModel
  */
 public function createUpload($owner, $file, $uploadTarget, $uploadInfo)
 {
     // Check that image is valid
     $ext = $file->guessClientExtension();
     if ($ext == "jpeg") {
         $ext = "jpg";
     }
     $size = $file->getClientSize();
     if (!in_array($ext, $this->valid_exts)) {
         throw new InvalidCreationException('File format unacceptable: ' . $ext);
     }
     if (!$size || $size > $this->max_size) {
         throw new InvalidCreationException('File is too large: ' . strval($size));
     }
     if ($uploadInfo['unique'] == true) {
         try {
             $target = \DB::table('upload_map')->where('owner_hash', $uploadTarget->hash)->where('upload_type', $uploadInfo['type'])->first();
             if ($target) {
                 $previousUpload = $this->uploadRepository->getByUploadWithTypeAndTarget($uploadTarget, $uploadInfo['type'])->first();
                 $this->deleteUpload($previousUpload);
                 \DB::table('upload_map')->where('upload_hash', $previousUpload->hash)->delete();
             }
         } catch (ModelNotFoundException $e) {
             //To be expected if there is no previous
         }
     }
     $upload_dir = storage_path() . '/app/' . $owner->hash;
     // Create the path for the upload file
     if (!\File::exists($upload_dir)) {
         \File::makeDirectory($upload_dir, 0775);
     }
     // Create data for object
     $data = [];
     $data['user_id'] = $owner->id;
     $data['extension'] = $ext;
     $data['upload_type'] = $uploadInfo['type'];
     $data['path'] = $upload_dir;
     // Create the object
     // Create the full image
     if ($ext != 'pdf') {
         \Cloudder::upload($file);
         $publicID = \Cloudder::getPublicId();
     } else {
         \Cloudder::upload($file);
         $publicID = \Cloudder::getPublicId();
     }
     $data['hash'] = $publicID;
     $upload = $this->uploadRepository->create($data);
     \DB::table('upload_map')->insert(['upload_hash' => $publicID, 'owner_hash' => $uploadTarget->hash, 'upload_type' => $uploadInfo['type'], 'created_at' => date('Y-m-d G:i:s'), 'updated_at' => date('Y-m-d G:i:s')]);
     \Log::info('New upload created', $upload->toArray());
     return $upload;
 }
Exemplo n.º 14
0
 /**
  * Build a \Torann\MediaSort\File\UploadedFile object from
  * a Symfony\Component\HttpFoundation\File\UploadedFile object.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  *
  * @return \Torann\MediaSort\File\UploadedFile
  * @throws \Torann\MediaSort\Exceptions\FileException
  */
 protected function createFromObject(SymfonyUploadedFile $file)
 {
     $path = $file->getPathname();
     $originalName = $file->getClientOriginalName();
     $mimeType = $file->getClientMimeType();
     $size = $file->getClientSize();
     $error = $file->getError();
     $uploadFile = new UploadedFile($path, $originalName, $mimeType, $size, $error);
     if (!$uploadFile->isValid()) {
         throw new FileException($uploadFile->getErrorMessage($uploadFile->getError()));
     }
     return $uploadFile;
 }
Exemplo n.º 15
0
 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;
 }
Exemplo n.º 16
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $rootPath = $this->container->get('kernel')->getRootDir() . '/../web/themes/wellcommerce/assets/prod/';
     $uploader = $this->container->get('media.manager.admin');
     $uploadPath = $uploader->getUploadRootDir('images');
     $filesystem = $this->container->get('filesystem');
     foreach (self::$samples as $photo) {
         $image = new UploadedFile($rootPath . $photo, $photo, 'image/jpeg', filesize($rootPath . $photo));
         $media = new Media();
         $media->setName($image->getClientOriginalName());
         $media->setExtension($image->guessClientExtension());
         $media->setMime($image->getClientMimeType());
         $media->setSize($image->getClientSize());
         $manager->persist($media);
         $filesystem->copy($rootPath . $photo, $uploadPath . '/' . $media->getPath());
         $this->setReference('photo_' . $photo, $media);
     }
     $manager->flush();
 }
Exemplo n.º 17
0
 public function upload(UploadedFile $file, $dir) : MediaInterface
 {
     if (!$file->isValid()) {
         throw new InvalidMediaException('Passed file object is not valid');
     }
     /** @var MediaInterface $media */
     $media = $this->manager->initResource();
     $media->setName($file->getClientOriginalName());
     $media->setExtension($file->guessClientExtension());
     $media->setMime($file->getClientMimeType());
     $media->setSize($file->getClientSize());
     $errors = $this->validatorHelper->validate($media);
     if (0 !== count($errors)) {
         throw new InvalidMediaException($errors);
     }
     $this->manager->createResource($media);
     $file->move($this->getUploadDir($dir), $media->getPath());
     return $media;
 }
Exemplo n.º 18
0
 /**
  * Handle the file upload. Returns the array on success, or false
  * on failure.
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @param  String                                              $path where to upload file
  * @return array|bool
  */
 public function handle(UploadedFile $file, $path = 'uploads')
 {
     $input = [];
     $fileName = str_slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
     $fileName = preg_replace('#([0-9_]+)x([0-9_]+)#', "\$1-\$2", $fileName);
     $input['path'] = $path;
     $input['extension'] = '.' . $file->getClientOriginalExtension();
     $input['filesize'] = $file->getClientSize();
     $input['mimetype'] = $file->getClientMimeType();
     $input['filename'] = $fileName . $input['extension'];
     $filecounter = 1;
     while (file_exists($input['path'] . '/' . $input['filename'])) {
         $input['filename'] = $fileName . '_' . $filecounter++ . $input['extension'];
     }
     try {
         $file->move($input['path'], $input['filename']);
         return $input;
     } catch (FileException $e) {
         \Log::error($e->getmessage());
         return false;
     }
 }
Exemplo n.º 19
0
 /**
  * Parse Filename
  *
  * @param UploadedFile $file     Uploaded File
  * @param string       $name     String Name
  * @param string       $folder   String Folder
  * @param bool         $override Boolean Over Ride
  *
  * @return bool|array
  */
 protected function parseFile($file, $folder = null, $name = null, $override = false)
 {
     $folder = trim((string) $folder, '/');
     $folder = $folder ? "{$folder}/" : "";
     $this->storage->makeDirectory($folder);
     $name = $name ?: $file->getClientOriginalName();
     $nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME));
     if (empty($nameOriginal)) {
         $nameOriginal = str_random(10);
     }
     $extension = $file->getClientOriginalExtension();
     $size = $file->getClientSize();
     $mime = $file->getClientMimeType();
     $sufix = '';
     $count = 1;
     do {
         $name = "{$nameOriginal}{$sufix}.{$extension}";
         $filename = "{$folder}{$name}";
         $sufix = "({$count})";
         $count++;
     } while (!$override && $this->storage->exists($filename));
     return compact('filename', 'name', 'extension', 'size', 'mime');
 }
 /**
  * Create a new file instance from a base instance.
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile  $file
  * @return static
  */
 public static function createFromBase(SymfonyUploadedFile $file)
 {
     return $file instanceof static ? $file : new static($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $file->getError());
 }
Exemplo n.º 21
0
 /**
  * @param UploadedFile $file
  * @return string
  * @throws \Vendor\GalleryBundle\Exception\FileUploadException
  */
 public function uploadPictureAndReturnUrl(UploadedFile $file)
 {
     $allowedExts = array("gif", "jpeg", "jpg", "png");
     $allowedTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png");
     $maxFileSize = 200000;
     $validationErrors = array();
     if (!$file->isValid()) {
         $validationErrors[] = "Return Code: " . $file->getError();
     }
     if (!in_array($file->getClientMimeType(), $allowedTypes)) {
         $validationErrors[] = 'Invalid file type.';
     }
     $temp = explode(".", $file->getClientOriginalName());
     $extension = end($temp);
     if (!in_array($extension, $allowedExts)) {
         $validationErrors[] = 'Invalid extension';
     }
     if ($file->getClientSize() > $maxFileSize) {
         $validationErrors[] = 'File too big (' . $file->getClientSize() . '). Max file size: ' . $maxFileSize;
     }
     if (count($validationErrors) > 0) {
         throw new FileUploadException(implode('\\n', $validationErrors));
     }
     $imgDirPath = $this->getBaseWebPath() . $this->getBundleWebPath();
     $filePath = $imgDirPath . $file->getClientOriginalName();
     move_uploaded_file($file->getRealPath(), $filePath);
     return 'images/' . $file->getClientOriginalName();
 }
Exemplo n.º 22
0
 /**
  * @param UploadedFile $uploadedFile
  * @return array
  */
 public function handleUpload(UploadedFile $uploadedFile)
 {
     $data = array();
     if (null !== $uploadedFile && $uploadedFile->isValid()) {
         $uploadedTempFile = $uploadedFile->getPathname();
         $data['originalName'] = $uploadedFile->getClientOriginalName();
         $data['filename'] = $this->sanitizeFilename($uploadedFile->getClientOriginalName());
         $data['mimeType'] = $uploadedFile->getClientMimeType();
         $data['size'] = $uploadedFile->getClientSize();
         $data['extension'] = $uploadedFile->getClientOriginalExtension();
         if (empty($data['extension'])) {
             $data['extension'] = $uploadedFile->guessClientExtension();
         }
         $data['width'] = 0;
         $data['height'] = 0;
         if ($this->checkIfImage($uploadedFile->getClientOriginalExtension(), $uploadedFile->getClientMimeType())) {
             $imageInfo = getimagesize($uploadedTempFile);
             if (!empty($imageInfo)) {
                 $data['width'] = $imageInfo[0];
                 $data['height'] = $imageInfo[1];
             }
         }
         $data['binary'] = null;
         if (file_exists($uploadedTempFile)) {
             $mediaStream = fopen($uploadedTempFile, 'rb');
             $data['binary'] = stream_get_contents($mediaStream);
             fclose($mediaStream);
         }
     }
     return $data;
 }
Exemplo n.º 23
0
 /**
  * Return the size of the file.
  *
  * @return string
  */
 public function getSize()
 {
     return $this->uploadedFile->getClientSize();
 }
Exemplo n.º 24
0
 /**
  * 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;
 }
Exemplo n.º 25
0
 /**
  * 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;
 }
Exemplo n.º 26
0
 /**
  * Validate uploaded file.
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @return void
  *
  * @throws \Hazzzard\Uploader\Exception\FileValidationException
  */
 protected function validateFile(UploadedFile $file)
 {
     $maxSize = $this->config['max_file_size'];
     $minSize = $this->config['min_file_size'];
     if (!$this->acceptFileType($file->getClientOriginalName())) {
         throw new FileValidationException($this->getErrorMessage('accept_file_types'));
     }
     if ($maxSize && $file->getClientSize() > $maxSize) {
         throw new FileValidationException($this->getErrorMessage('max_file_size', array($maxSize / 1024)));
     }
     if ($minSize && $file->getClientSize() < $minSize) {
         throw new FileValidationException($this->getErrorMessage('min_file_size'));
     }
     if ($this->imageFileType($file->getClientOriginalName())) {
         $this->validateImage($file);
     }
 }
Exemplo n.º 27
0
 /**
  * {@inheritdoc}
  */
 public function size()
 {
     return $this->source->getClientSize();
 }
 /**
  * Mueve un arcivo subido
  * @param UploadedFile $file 
  * @param string $path 
  * @return SplFileInfo|null
  */
 public function upload($file, $path)
 {
     if ($this->validExt($file->getClientOriginalName())) {
         if ($file->getClientSize() > $this->getMaxUploadFileSize() * 1024 * 1024) {
             $result = array("query" => "BE_UPLOAD_FILE_SIZE_NOT_SERVER", "params" => array($file->getClientSize()));
             $this->setInfo(array("msg" => $result));
             if ($this->config['debug']) {
                 $this->_log(__METHOD__ . " - file size no permitido server: " . $file->getClientSize());
             }
             return;
         } elseif ($file->getClientSize() > $this->config['upload']['size_max'] * 1024 * 1024) {
             $result = array("query" => "BE_UPLOAD_FILE_SIZE_NOT_PERMITIDO", "params" => array($file->getClientSize()));
             $this->setInfo(array("msg" => $result));
             if ($this->config['debug']) {
                 $this->_log(__METHOD__ . " - file size no permitido: " . $file->getClientSize());
             }
             return;
         } else {
             if ($file->isValid()) {
                 $dir = $this->getFullPath() . $path;
                 $namefile = $file->getClientOriginalName();
                 $namefile = $this->clearNameFile($namefile);
                 $nametemp = $namefile;
                 if ($this->config["upload"]["overwrite"] == false) {
                     $ext = $this->getExtension($namefile);
                     $i = 0;
                     while (true) {
                         $pathnametemp = $dir . $nametemp;
                         if (file_exists($pathnametemp)) {
                             $i++;
                             $nametemp = $this->removeExtension($namefile) . '_' . $i . '.' . $ext;
                         } else {
                             break;
                         }
                     }
                 }
                 $file->move($dir, $nametemp);
                 $file = new \SplFileInfo($dir . $nametemp);
                 return $file;
             }
         }
     } else {
         if ($this->config['debug']) {
             $this->_log(__METHOD__ . " - file extension no permitido: " . $file->getExtension());
         }
     }
 }
Exemplo n.º 29
0
 /**
  * @param UploadedFile $uploadedFile
  * @param MediaInterface $entity
  * @param bool $move
  * @return MediaInterface
  */
 public function upload(UploadedFile $uploadedFile, MediaInterface $entity, $keepOriginalFileName = false)
 {
     $entity->setMimetype($uploadedFile->getMimeType());
     $entity->setSize($uploadedFile->getClientSize());
     $entity->setType(Media::UPLOADED_FILE);
     $entity->setOriginal($uploadedFile->getClientOriginalName());
     $entity->setPath($this->getUploadRootDir());
     if ($keepOriginalFileName) {
         $fileName = $entity->getOriginal();
         $entity->setFileName($entity->getOriginal());
     } else {
         $ext = $uploadedFile->guessExtension() ? $uploadedFile->guessExtension() : 'bin';
         $fileName = md5(rand(1, 9999999) . time() . $uploadedFile->getClientOriginalName()) . '.' . $ext;
     }
     $entity->setFileName($fileName);
     $uploadedFile->move($this->getUploadRootDir(), $entity->getFileName());
     $webPath = sprintf('/%s/%s', $this->getUploadDir(), $entity->getFileName());
     $entity->setWebPath($webPath);
     return $entity;
 }
Exemplo n.º 30
0
 /**
  * Move uploaded file to target path
  *
  * @param object $file
  * @param string $uploadBasePath
  * @return string
  */
 public function moveUploadedFile(UploadedFile $file, $uploadBasePath)
 {
     // Get file extension
     $basePath = $uploadBasePath . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
     $ext = pathinfo($basePath, PATHINFO_EXTENSION);
     // Validate max file size
     if ($file->getClientSize() > $this->_uploadMaxSize) {
         $maxSize = ceil($this->_uploadMaxSize / 1024 / 1024);
         //$sizeError = Message::getMessage('UPLOAD_INVALID_FILE_SIZE', $maxSize);
         $sizeError = 'Upload invalid file size, maximum: ' . $maxSize . 'MB';
         $this->error = $sizeError;
         return false;
     }
     // Validate allowed file types
     if (!in_array($ext, $this->_validFileType)) {
         //$this->error = Message::getMessage('UPLOAD_INVALID_FILE_TYPE');
         $this->error = 'Upload invalid file type (' . implode(',', $this->_validFileType) . ')';
         return false;
     }
     // Create new file name
     $newFileName = $this->_fileName . '.' . $ext;
     $targetPath = $uploadBasePath . DIRECTORY_SEPARATOR . $newFileName;
     if (!is_dir($uploadBasePath)) {
         mkdir($uploadBasePath, 0777, true);
     }
     $file->move($uploadBasePath, $newFileName);
     $this->_uploadedFilePath = $targetPath;
     return $targetPath;
 }