/** * * Upload file to CDN * @param string $file * @param string $uploadName * @param string $content_type * @return boolean */ public function uploadFile($file, $uploadName, $content_type = null) { Varien_Profiler::start('cdn_upload_file_' . $uploadName); if ($content_type == null) { $content_type = Mage::helper('mycdn')->detectFileMimeType($file); } $adapter = $this->getAdapter(); if ($adapter) { $fileName = Mage::helper('mycdn')->getRelativeFile($uploadName); Mage::helper('mycdn')->addLog('[UPLOAD] ' . $fileName . ' as type ' . $content_type); $size = new Zend_Validate_File_Size(['min' => Mage::getStoreConfig('mycdn/general/min')]); if (!$size->isValid($file)) { return false; } $result = $adapter->uploadFile($file, $fileName, $content_type); if ($result) { Mage::helper('mycdn')->addLog('[UPLOADED] ' . $fileName); //saving to cache Mage::helper('mycdn')->savePathInCache($fileName, $this->getUrl($fileName)); return true; } Mage::helper('mycdn')->addLog('[NOT UPLOADED] ' . $fileName); Varien_Profiler::stop('cdn_upload_file_' . $uploadName); return false; } }
/** * Ensures that the validator follows expected behavior * * @return void */ public function testBasic() { $valuesExpected = array(array(0, 10000, true), array(0, '10 MB', true), array(0, '10MB', true), array(0, '10 MB', true), array(794, null, true), array(0, 500, false), array(500, null, false)); foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_Size($element[0], $element[1]); $this->assertEquals($element[2], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'), "Tested with " . var_export($element, 1)); } foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_Size(array($element[0], $element[1])); $this->assertEquals($element[2], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'), "Tested with " . var_export($element, 1)); } foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_Size(array('min' => $element[0], 'max' => $element[1])); $this->assertEquals($element[2], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'), "Tested with " . var_export($element, 1)); } $validator = new Zend_Validate_File_Size(array(2000)); $this->assertEquals(true, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); $validator = new Zend_Validate_File_Size(array(1, 2, 3)); $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); $files = array('name' => 'testsize.mo', 'type' => 'text', 'size' => 200, 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo', 'error' => 0); $validator = new Zend_Validate_File_Size(array(2000)); $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/nofile.mo', $files)); $files = array('name' => 'testsize.mo', 'type' => 'text', 'size' => 200, 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo', 'error' => 0); $validator = new Zend_Validate_File_Size(array(0, 1)); $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo', $files)); $files = array('name' => 'testsize.mo', 'type' => 'text', 'size' => 200, 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo', 'error' => 0); $validator = new Zend_Validate_File_Size(array(5000, 10000)); $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo', $files)); }
/** * Ensures that the validator follows expected behavior * * @return void */ public function testBasic() { $valuesExpected = array(array(0, 10000, true), array(0, '10 MB', true), array(0, '10MB', true), array(0, '10 MB', true), array(10000, 0, true), array(0, 500, false), array(500, 0, false)); foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_Size($element[0], $element[1]); $this->assertEquals($element[2], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); } foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_Size(array($element[0], $element[1])); $this->assertEquals($element[2], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); } }
public function update() { $helper = Mage::helper('geoip'); $ret = array('status' => 'error'); if ($permissions_error = $this->checkFilePermissions()) { $ret['message'] = $permissions_error; return $ret; } $remote_file_size = $helper->getSize($this->remote_archive); if ($remote_file_size < 100000) { $ret['message'] = $helper->__('You are banned from downloading the file. Please try again in several hours.'); return $ret; } /** @var $_session Mage_Core_Model_Session */ $_session = Mage::getSingleton('core/session'); $_session->setData('_geoip_file_size', $remote_file_size); $ch = curl_init($this->remote_archive); $fp = fopen($this->local_archive, 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode != 200) { $ret['message'] = $helper->__('Failed to Download Zip'); fclose($fp); return $ret; } curl_close($ch); fclose($fp); $arch_size = new Zend_Validate_File_Size(array('min' => 1000)); if (!$arch_size->isValid($this->local_archive)) { $ret['message'] = $helper->__('Download failed.'); return $ret; } if (!$helper->unGZip($this->local_archive, $this->local_dir)) { $ret['message'] = $helper->__('UnGzipping failed'); return $ret; } $ret['status'] = 'success'; $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM); $ret['date'] = Mage::app()->getLocale()->date(filemtime($this->local_file))->toString($format); return $ret; }
/** * Ensures that the validator returns size infos * * @return void */ public function testFailureMessage() { $validator = new Zend_Validate_File_Size(array('min' => 9999, 'max' => 10000)); $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); $this->assertContains('9.76kB', current($validator->getMessages())); $this->assertContains('794B', current($validator->getMessages())); $validator = new Zend_Validate_File_Size(array('min' => 9999, 'max' => 10000, 'bytestring' => false)); $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/testsize.mo')); $this->assertContains('9999', current($validator->getMessages())); $this->assertContains('794', current($validator->getMessages())); }