View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.rest.extraction;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
29  import fr.ifremer.quadrige2.core.dao.technical.ZipUtils;
30  import fr.ifremer.quadrige2.synchro.server.config.SynchroServerConfiguration;
31  import fr.ifremer.quadrige2.synchro.server.technical.ServletUtils;
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.springframework.http.HttpStatus;
36  import org.springframework.web.bind.annotation.*;
37  import org.springframework.web.multipart.MultipartFile;
38  import org.springframework.web.multipart.MultipartHttpServletRequest;
39  
40  import java.io.File;
41  import java.io.IOException;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * REST controller for external extraction purpose.
47   *
48   * Created by Ludovic on 08/02/2016.
49   */
50  @RestController
51  @RequestMapping("/extraction")
52  public class ExtractionRestController {
53  
54      private static final Log log = LogFactory.getLog(ExtractionRestController.class);
55  
56      @ResponseStatus(value = HttpStatus.OK, reason = "File successfully uploaded")
57      @RequestMapping(value = "/upload", method = RequestMethod.POST)
58      public void uploadFile(@RequestParam("type") String type, @RequestParam("file") MultipartFile file, @RequestParam("filename") String fileName, MultipartHttpServletRequest request) throws IOException {
59  
60          File tempDir = SynchroServerConfiguration.getInstance().getTempDirectory();
61          File outputDir = SynchroServerConfiguration.getInstance().getExtractionDirectory(type);
62          if (outputDir == null) {
63              throw new Quadrige2TechnicalException(t("quadrige2.extraction.upload.type.error", type));
64          }
65  
66          // Create the upload directory
67          try {
68              if (!outputDir.exists()) {
69                  FileUtils.forceMkdir(outputDir);
70              }
71          } catch (IOException e) {
72              throw new Quadrige2TechnicalException(t("quadrige2.extraction.upload.createDir.error"), e);
73          }
74  
75          File tempFile = new File(tempDir, fileName);
76  
77          // Copy file to temp directory
78          try {
79              ServletUtils.copyFile(file, tempFile);
80          } catch (IOException e) {
81              throw new Quadrige2TechnicalException(t("quadrige2.extraction.upload.copy.error"), e);
82          }
83  
84          // Extract file to output and delete temp zip file
85          try {
86              ZipUtils.uncompressFileToPath(tempFile, outputDir.getPath(), true);
87          } catch (IOException e) {
88              throw new Quadrige2TechnicalException(t("quadrige2.extraction.upload.unzip.error"), e);
89          }
90  
91      }
92  }