Uploader::validate

@param \O2System\Kernel\Http\Message\UploadFile $file

@return bool
430369
protected function validate(UploadFile $file)
    {
        /* Validate extension */
        if (is_array($this->allowedExtensions) && count($this->allowedExtensions)) {
            if ( ! in_array('.' . $file->getExtension(), $this->allowedExtensions)) {
                $this->errors[] = language()->getLine(
                    'UPLOADER_E_ALLOWED_EXTENSIONS',
                    [implode(',', $this->allowedExtensions), $file->getExtension()]
                );
            }
        }

        /* Validate mime */
        if (is_array($this->allowedMimes) && count($this->allowedExtensions)) {
            if ( ! in_array($file->getFileMime(), $this->allowedMimes)) {
                $this->errors[] = language()->getLine(
                    'UPLOADER_E_ALLOWED_MIMES',
                    [implode(',', $this->allowedMimes), $file->getFileMime()]
                );
            }
        }

        /* Validate min size */
        if ($this->allowedFileSize[ 'min' ] > 0) {
            if ($file->getSize() < $this->allowedFileSize[ 'min' ]) {
                $this->errors[] = language()->getLine(
                    'UPLOADER_E_ALLOWED_MIN_FILESIZE',
                    [$this->allowedFileSize[ 'min' ], $file->getSize()]
                );
            }
        }

        /* Validate max size */
        if ($this->allowedFileSize[ 'min' ] > 0) {
            if ($file->getSize() > $this->allowedFileSize[ 'max' ]) {
                $this->errors[] = language()->getLine(
                    'UPLOADER_E_ALLOWED_MAX_FILESIZE',
                    [$this->allowedFileSize[ 'max' ], $file->getSize()]
                );
            }
        }

        if (count($this->errors) == 0) {
            return true;
        }

        return false;
    }
109963
private function validateError(UploadedFile $uploadedFile) {
        switch($uploadedFile->getError()) {
            case UPLOAD_ERR_OK:
                //Do Nothing
                break;
            case UPLOAD_ERR_INI_SIZE:
                throw new Exception('File too large, must not be greater than ' . ini_get('upload_max_filesize'), 401);
                break;
            case UPLOAD_ERR_NO_FILE:
                throw new Exception('No File Uploaded', 401);
                break;
            default:
                throw new Exception($uploadedFile->getError(), 401);
                break;
        }
    }
1010620
public function handleUpload(&$uploader)
    {
        // store uploader
        $this->uploader =& $uploader;
        //store input
        $this->setInput();
        // tell uploader which field is uploaded
        $this->uploader->setField($this->field);
        // make temporary file
        $this->uploader->setDestination(sys_get_temp_dir());
        // loop validation rules
        foreach($this->validations as $validation) {
            $options = isset($validation['options']) ? $validation['options'] : null;
            $message = isset($validation['message']) ? $validation['message'] : null;
            // set rule
            $this->uploader->addValidationRule($validation['type'], $options, $message);
        }
        // upload
        if(!$this->uploader->process()) {
            // failure
            return false;
        }
        // loop field outputs
        $temporaryFile = sprintf('%s/%s', sys_get_temp_dir(), $this->uploader->getUploadedFileInfo()['name']);
        foreach($this->outputs as $outputName => $output) {
            $outputPath = $output->getDestination() .  $this->uploader->getUploadedFileInfo()['name'];
            if(is_file($outputPath)){
                $outputPath = sprintf('%s%s_%s', $output->getDestination(), time(),  $this->uploader->getUploadedFileInfo()['name']);
            }
            copy($temporaryFile, $outputPath);
            $this->outputsFiles[$outputName] = [
                'name' => $this->uploader->getUploadedFileInfo()['name'],
                'path' => $outputPath
            ];
        }
        // delete temporary file
        unlink($temporaryFile);
        // close upload
        $this->uploader->close();
        return true;
    }
1289535
public function checkType(\Nette\Http\FileUpload $file) {
		if(\Nette\Utils\Arrays::searchKey($this->getMimeTypes(), $file->getContentType()) !== false) {
			return true;
		} else {
			// Pokud se nepodaří ověřit mimetype, ověříme alespoň koncovku.
			if(array_search($this->getExtension($file->getName()), array_unique($this->getMimeTypes())) !== false) {
				return true;
			} else {
				return false;
			}
		}
	}
1120751
protected function validateFileSize()
    {
        if (!is_numeric($this->max_file_size) && !is_int($this->max_file_size)) {
            // it is not set max file size limitation.
            return true;
        }

        if (is_array($this->files[$this->input_file_name]) && array_key_exists('size', $this->files[$this->input_file_name]) && $this->files[$this->input_file_name]['size'] > $this->max_file_size) {
            $this->setErrorMessage(
                sprintf(static::__('The uploaded file exceeds the max file size directive. (%s &gt; %s).'), $this->files[$this->input_file_name]['size'], $this->max_file_size),
                'RDU_1',
                $this->files[$this->input_file_name]['size'] . ' &gt; ' . $this->max_file_size,
                $this->files[$this->input_file_name]['name'],
                $this->files[$this->input_file_name]['size'],
                $this->files[$this->input_file_name]['type']
            );
            return false;
        } else {
            if (is_array($this->files[$this->input_file_name]) && array_key_exists('tmp_name', $this->files[$this->input_file_name]) && function_exists('filesize')) {
                if (filesize($this->files[$this->input_file_name]['tmp_name']) > $this->max_file_size) {
                    $this->setErrorMessage(
                        sprintf(static::__('The uploaded file exceeds the max file size directive. (%s &gt; %s).'), $this->files[$this->input_file_name]['size'], $this->max_file_size),
                        'RDU_1',
                        $this->files[$this->input_file_name]['size'] . ' &gt; ' . $this->max_file_size,
                        $this->files[$this->input_file_name]['name'],
                        $this->files[$this->input_file_name]['size'],
                        $this->files[$this->input_file_name]['type']
                    );
                    return false;
                }
            }
        }

        return true;
    }
450034
public function validate(\Upload\FileInfoInterface $fileInfo)
    {
        $fileExtension = strtolower($fileInfo->getExtension());

        if (in_array($fileExtension, $this->allowedExtensions) === false) {
            throw new \Upload\Exception(sprintf('Invalid file extension. Must be one of: %s', implode(', ', $this->allowedExtensions)), $fileInfo);
        }
    }
1369899
public function validateFile($file)
    {
        if (file_exists($file) !== true) {
            throw new Exception('File not found');
        }
        if (is_readable($file) !== true) {
            throw new Exception('File not readable');
        }

        $data = file_get_contents($file);
        if ($data === false) {
            throw new Exception('Failed get file');
        }

        return $this->validateFragment($data);
    }
816008
public function validateFile(Entity\File $file, $sourceFilePath = null)
	{
		$this->validate($file, self::TYPE_FILE, $sourceFilePath);
	}
1064552
public function uploadImage(UploadedFile $file)
    {
        $image = $this
            ->imageManager
            ->createImage($file);

        $this
            ->mediaEventDispatcher
            ->dispatchImagePreUploadEvent($image);

        $this->imageObjectManager->persist($image);
        $this->imageObjectManager->flush($image);

        $this->fileManager->uploadFile(
            $image,
            file_get_contents($file->getRealPath()),
            true
        );

        $this
            ->mediaEventDispatcher
            ->dispatchImageOnUploadEvent($image);

        return $image;
    }
1162008
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();
        }
    }
1006686
protected function validateValue($value)
    {
        if (is_string($value) && file_exists($value))
        {
            $uploadFile = new UploadedFile();
            $uploadFile->size = filesize($value);
            $uploadFile->type = FileHelper::getMimeType($value, null, false);
            $uploadFile->tempName = $value;
            $uploadFile->name = $value;

            $value = $uploadFile;
        }

        $result = parent::validateValue($value);

        return empty($result) ? $this->validateImage($value) : $result;
    }
810472
public function isFile($file)
	{
		$file = Util::normalizePath($file);

		return (bool) $this->adapter->isFile($file);
	}
1144674
public function validate($validator)
    {
        $maxFiles = $this->getAllowedMaxFileNumber();
        $count = count($this->getItems());

        if ($maxFiles < 1 || $count <= $maxFiles) {
            return true;
        }

        $validator->validationError($this->getName(), _t(
            __CLASS__ . '.ErrorMaxFilesReached',
            'You can only upload {count} file.|You can only upload {count} files.',
            [
                'count' => $maxFiles,
            ]
        ));

        return false;
    }
887540
public function check($file = '')
        {
            $types = $this->getAllowedMimeTypes();
            if ($types === '*') {
                return true;
            }

            // dosyanın tipini alıyoruz
            $fileMimeType = $this->getFileMimeType();
            foreach($types as $type)
            {
                 if($type === $fileMimeType){
                     return true;
                 }else{
                     continue;
                 }
            }

            return false;
        }
905037
protected function validateFile()
    {
        $id = $this->getUpdatingId();

        if ($id === false) {
            return null;
        }

        $data = $this->file->get($id);

        if (empty($data)) {
            $this->setErrorUnavailable('update', $this->translation->text('File'));
            return false;
        }

        $this->setSubmitted('update', $data);
        return true;
    }
279443
public function isValid($value, $file = null)
    {
        if (array_key_exists($value, $this->_files)) {
            $files[$value] = $this->_files[$value];
        } else {
            foreach ($this->_files as $file => $content) {
                if (isset($content['name']) && ($content['name'] === $value)) {
                    $files[$file] = $this->_files[$file];
                }

                if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
                    $files[$file] = $this->_files[$file];
                }
            }
        }

        if (empty($files)) {
            return $this->_throw($file, self::FILE_NOT_FOUND);
        }

        foreach ($files as $file => $content) {
            $this->_value = $file;
            switch($content['error']) {
                case 0:
                    if (!is_uploaded_file($content['tmp_name'])) {
                        $this->_throw($file, self::ATTACK);
                    }
                    break;

                case 1:
                    $this->_throw($file, self::INI_SIZE);
                    break;

                case 2:
                    $this->_throw($file, self::FORM_SIZE);
                    break;

                case 3:
                    $this->_throw($file, self::PARTIAL);
                    break;

                case 4:
                    $this->_throw($file, self::NO_FILE);
                    break;

                case 6:
                    $this->_throw($file, self::NO_TMP_DIR);
                    break;

                case 7:
                    $this->_throw($file, self::CANT_WRITE);
                    break;

                case 8:
                    $this->_throw($file, self::EXTENSION);
                    break;

                default:
                    $this->_throw($file, self::UNKNOWN);
                    break;
            }
        }

        if (count($this->_messages) > 0) {
            return false;
        } else {
            return true;
        }
    }
1231665
public function checkFile($file)
    {
        $result = $this->execute(new Process([$this->getCli(), '-l', $file]));

        if (0 === $result['code']) {
            return [
                'validity' => true,
                'errors' => null
            ];
        }

        $fullMessage = \preg_replace('/ in (?:.+) on line (?:[0-9]+)$/', '', $result['output']);
        \preg_match('/ on line ([0-9]+)$/', $result['output'], $matchLine);
        $line = isset($matchLine[1]) ? \intval($matchLine[1]) : null;

        list($type, $message) = \explode(': ', $fullMessage);

        return [
            'validity' => false,
            'errors' => [
                [
                    'file' => $file,
                    'code' => $result['code'],
                    'line' => $line,
                    'type' => $type,
                    'message' => $this->convertMessage($message)
                ],
            ],
        ];
    }