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