001package com.ganteater.ae.web; 002 003import java.io.File; 004import java.io.FileNotFoundException; 005import java.io.FileOutputStream; 006import java.io.IOException; 007import java.io.InputStream; 008import java.util.zip.ZipEntry; 009import java.util.zip.ZipInputStream; 010 011import org.apache.commons.io.IOUtils; 012import org.apache.commons.lang.StringUtils; 013import org.springframework.stereotype.Controller; 014import org.springframework.web.bind.annotation.PostMapping; 015import org.springframework.web.bind.annotation.RequestParam; 016import org.springframework.web.multipart.MultipartFile; 017import org.springframework.web.servlet.view.RedirectView; 018 019import com.ganteater.ae.ConfigConstants; 020 021@Controller 022public class ConfigUploadController { 023 024 private WebWorkspace workspace; 025 026 public ConfigUploadController(WebWorkspace workspace) { 027 super(); 028 this.workspace = workspace; 029 } 030 031 @PostMapping(value = "/upload-configuration") 032 public RedirectView handleFileUpload(@RequestParam("file") MultipartFile file) 033 throws FileNotFoundException, IOException { 034 String originalFilename = file.getOriginalFilename(); 035 036 if (StringUtils.endsWithIgnoreCase(originalFilename, ".zip")) { 037 if (!file.isEmpty()) { 038 try { 039 unZipIt(file.getInputStream()); 040 workspace.afterPropertiesSet(); 041 042 } catch (Exception e) { 043 e.printStackTrace(); 044 } 045 } 046 } else { 047 File confFile = new File(workspace.getWorkingDir(), ConfigConstants.AE_CONFIG_XML); 048 try (FileOutputStream outputStream = new FileOutputStream(confFile)) { 049 IOUtils.copy(file.getInputStream(), outputStream); 050 } 051 } 052 053 return new RedirectView("dashboard"); 054 } 055 056 public void unZipIt(InputStream zipStream) throws IOException { 057 058 byte[] buffer = new byte[1024]; 059 060 File folder = workspace.getStartDir(); 061 if (!folder.exists()) { 062 folder.delete(); 063 folder.mkdir(); 064 } 065 066 ZipInputStream zis = new ZipInputStream(zipStream); 067 ZipEntry ze = zis.getNextEntry(); 068 069 while (ze != null) { 070 071 String fileName = ze.getName(); 072 File newFile = new File(folder, fileName); 073 074 System.out.println("file unzip : " + newFile.getAbsoluteFile()); 075 if (ze.isDirectory()) { 076 newFile.mkdir(); 077 078 } else { 079 080 FileOutputStream fos = new FileOutputStream(newFile); 081 082 int len; 083 while ((len = zis.read(buffer)) > 0) { 084 fos.write(buffer, 0, len); 085 } 086 087 fos.close(); 088 } 089 ze = zis.getNextEntry(); 090 } 091 092 zis.closeEntry(); 093 zis.close(); 094 095 System.out.println("Done"); 096 097 } 098}