protected function _process() { $return = array(); try { $fileInfo = reset($_FILES); if (empty($fileInfo)) { throw new CM_Exception('Invalid file upload'); } if (isset($fileInfo['error']) && $fileInfo['error'] !== UPLOAD_ERR_OK) { throw new CM_Exception('File upload error: ' . self::$_uploadErrors[$fileInfo['error']]); } $fileTmp = new CM_File($fileInfo['tmp_name']); if ($fileTmp->getSize() > self::MAX_FILE_SIZE) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('File too big')); } $file = CM_File_UserContent_Temp::create($fileInfo['name'], $fileTmp->read()); $fileTmp->delete(); $query = $this->_request->getQuery(); $preview = null; if (isset($query['field'])) { $field = CM_FormField_File::factory($query['field'], ['name' => 'file']); $field->validateFile($file); $preview = $this->getRender()->fetchViewTemplate($field, 'preview', array('file' => $file)); } $return['success'] = array('id' => $file->getUniqid(), 'preview' => $preview); } catch (CM_Exception_FormFieldValidation $ex) { $return['error'] = array('type' => get_class($ex), 'msg' => $ex->getMessagePublic($this->getRender())); } $this->_setContent(json_encode($return, JSON_HEX_TAG)); // JSON decoding in IE-iframe needs JSON_HEX_TAG }
public function testResizeFileSize() { $imageFileOriginal = new CM_File(DIR_TEST_DATA . 'img/test.jpg'); $image = new CM_Image_Image($imageFileOriginal->read()); $this->assertEquals(17661, $imageFileOriginal->getSize(), '', 300); $image->setCompressionQuality(90)->resize(100, 100); $imageFile = CM_File::createTmp(null, $image->getBlob()); $this->assertEquals(4620, $imageFile->getSize(), '', 300); }
/** * @param CM_File $file * @param CM_File_Filesystem_Adapter $adapterSource * @param CM_File_Filesystem_Adapter $adapterDestination * @throws CM_Exception */ protected function _copyToFileStreaming(CM_File $file, CM_File_Filesystem_Adapter $adapterSource, CM_File_Filesystem_Adapter $adapterDestination) { /** @var CM_File_Filesystem_Adapter_StreamInterface $adapterSource */ /** @var CM_File_Filesystem_Adapter_StreamInterface $adapterDestination */ $streamSource = $adapterSource->getStreamRead($this->getPath()); $adapterDestination->writeStream($file->getPath(), $streamSource); $sizeDestination = $file->getSize(); $sizeSource = $this->getSize(); if ($sizeSource !== $sizeDestination) { throw new CM_Exception('Copy failed', null, ['Source' => $this->getPath(), 'Destination' => $file->getPath(), 'Original size' => $sizeSource, 'Bytes copied' => $sizeDestination, 'Source adapter' => get_class($adapterSource), 'Destination adapter' => get_class($adapterDestination)]); } if (is_resource($streamSource) && !@fclose($streamSource)) { throw new CM_Exception('Could not close stream for path', null, ['path' => $this->getPath(), 'Source adapter' => get_class($adapterSource)]); } }