FileUncompressAction.class.php (1943B)
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\model\File; 7 use language\Messages; 8 9 10 class FileUncompressAction extends FileAction implements Method { 11 public function view() { 12 } 13 14 15 public function post() { 16 switch( $this->file->extension ) 17 { 18 case 'gz': 19 if ( $this->request->getNumber('replace') ) 20 { 21 if ( strcmp(substr($this->file->loadValue(),0,2),"\x1f\x8b")) 22 { 23 throw new \LogicException("Not GZIP format (See RFC 1952)"); 24 } 25 $method = ord(substr($this->file->loadValue(),2,1)); 26 if ( $method != 8 ) 27 { 28 throw new \LogicException("Unknown GZIP method: $method"); 29 } 30 $this->file->value = gzinflate( substr($this->file->loadValue(),10)); 31 $this->file->parse_filename( $this->file->filename ); 32 $this->file->save(); 33 $this->file->saveValue(); 34 } 35 else 36 { 37 $newFile = new File(); 38 $newFile->parentid = $this->file->parentid; 39 $newFile->value = gzinflate( substr($this->file->loadValue(),10)); 40 $newFile->parse_filename( $this->file->filename ); 41 $newFile->persist(); 42 } 43 44 break; 45 46 case 'bz2': 47 if ( $this->request->getNumber('replace') ) 48 { 49 $this->file->value = bzdecompress($this->file->loadValue()); 50 $this->file->parse_filename( $this->file->filename ); 51 $this->file->save(); 52 $this->file->saveValue(); 53 } 54 else 55 { 56 $newFile = new File(); 57 $newFile->parentid = $this->file->parentid; 58 $newFile->value = bzdecompress( $this->file->loadValue() ); 59 $newFile->parse_filename( $this->file->filename ); 60 $newFile->persist(); 61 $newFile->copyNamesFrom( $this->file->objectid); 62 } 63 64 break; 65 66 default: 67 throw new \util\exception\UIException('', 'cannot uncompress file with extension: ' . $this->file->extension, []); 68 } 69 70 $this->addNoticeFor( $this->file, Messages::DONE ); 71 } 72 }