View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.rest.extraction;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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.quadrige3.core.dao.technical.ZipUtils;
29  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
30  import fr.ifremer.quadrige3.synchro.server.config.SynchroServerConfiguration;
31  import fr.ifremer.quadrige3.synchro.server.technical.ServletUtils;
32  import org.apache.commons.io.FileUtils;
33  import org.springframework.http.HttpStatus;
34  import org.springframework.web.bind.annotation.*;
35  import org.springframework.web.multipart.MultipartFile;
36  import org.springframework.web.multipart.MultipartHttpServletRequest;
37  
38  import java.io.File;
39  import java.io.IOException;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * REST controller for external extraction purpose.
45   *
46   * Created by Ludovic on 08/02/2016.
47   */
48  @RestController
49  @RequestMapping("/extraction")
50  public class ExtractionRestController {
51  
52      @ResponseStatus(value = HttpStatus.OK, reason = "File successfully uploaded")
53      @RequestMapping(value = "/upload", method = RequestMethod.POST)
54      public void uploadFile(@RequestParam("type") String type, @RequestParam("file") MultipartFile file, @RequestParam("filename") String fileName, MultipartHttpServletRequest request) {
55  
56          File tempDir = SynchroServerConfiguration.getInstance().getTempDirectory();
57          File outputDir = SynchroServerConfiguration.getInstance().getExtractionDirectory(type);
58          if (outputDir == null) {
59              throw new QuadrigeTechnicalException(t("quadrige3.extraction.upload.type.error", type));
60          }
61  
62          // Create the upload directory
63          try {
64              if (!outputDir.exists()) {
65                  FileUtils.forceMkdir(outputDir);
66              }
67          } catch (IOException e) {
68              throw new QuadrigeTechnicalException(t("quadrige3.extraction.upload.createDir.error"), e);
69          }
70  
71          File tempFile = new File(tempDir, fileName);
72  
73          // Copy file to temp directory
74          try {
75              ServletUtils.copyFile(file, tempFile);
76          } catch (IOException e) {
77              throw new QuadrigeTechnicalException(t("quadrige3.extraction.upload.copy.error"), e);
78          }
79  
80          // Extract file to output and delete temp zip file
81          try {
82              ZipUtils.uncompressFileToPath(tempFile.toPath(), outputDir.toPath(), true);
83          } catch (IOException e) {
84              throw new QuadrigeTechnicalException(t("quadrige3.extraction.upload.unzip.error"), e);
85          }
86  
87      }
88  }