FileCompressAction.class.php (2070B)
1 <?php 2 namespace cms\action\file; 3 use cms\action\Action; 4 use cms\action\FileAction; 5 use cms\action\Method; 6 use cms\action\RequestParams; 7 use cms\model\File; 8 use language\Messages; 9 use util\exception\ValidationException; 10 11 12 class FileCompressAction extends FileAction implements Method { 13 14 public function view() { 15 $formats = array(); 16 foreach( $this->getCompressionTypes() as $t ) 17 $formats[$t] = \cms\base\Language::lang('compression_'.$t); 18 19 $this->setTemplateVar('formats' ,$formats ); 20 } 21 22 public function post() { 23 $format = $this->request->getAlphanum('format'); 24 25 switch( $format ) 26 { 27 case 'gz': 28 if ( $this->request->getNumber('replace' ) =='1' ) 29 { 30 $this->file->value = gzencode( $this->file->loadValue(),1 ); 31 $this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz',FORCE_GZIP ); 32 $this->file->save(); 33 $this->file->saveValue(); 34 35 } 36 else 37 { 38 $newFile = new File(); 39 $newFile->parentid = $this->file->parentid; 40 $newFile->value = gzencode( $this->file->loadValue(),1 ); 41 $newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.gz',FORCE_GZIP ); 42 $newFile->persist(); 43 $newFile->copyNamesFrom( $this->file->objectid ); 44 } 45 46 break; 47 48 case 'bzip2': 49 if ( $this->request->getText('replace')=='1' ) 50 { 51 $this->file->value = bzcompress( $this->file->loadValue() ); 52 $this->file->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' ); 53 $this->file->save(); 54 $this->file->saveValue(); 55 56 } 57 else 58 { 59 $newFile = new File(); 60 $newFile->parentid = $this->file->parentid; 61 $newFile->value = bzcompress( $this->file->loadValue() ); 62 $newFile->parse_filename( $this->file->filename.'.'.$this->file->extension.'.bz2' ); 63 $newFile->persist(); 64 $newFile->copyNamesFrom( $this->file->objectid ); 65 } 66 67 break; 68 default: 69 throw new ValidationException('format'); 70 } 71 72 $this->addNoticeFor( $this->file, Messages::DONE); 73 } 74 }