Search in sources :

Example 1 with GPXSessionContext

use of net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext in project OsmAnd-tools by osmandapp.

the class GpxController method downloadObf.

@RequestMapping(path = { "/download-obf" })
@ResponseBody
public ResponseEntity<Resource> downloadObf(@RequestParam(defaultValue = "", required = false) String gzip, HttpSession httpSession, HttpServletResponse resp) throws IOException, FactoryConfigurationError, XMLStreamException, SQLException, InterruptedException, XmlPullParserException {
    GPXSessionContext ctx = session.getGpxResources(httpSession);
    File tmpOsm = File.createTempFile("gpx_obf_" + httpSession.getId(), ".osm.gz");
    ctx.tempFiles.add(tmpOsm);
    List<File> files = new ArrayList<>();
    for (GPXSessionFile f : ctx.files) {
        files.add(f.file);
    }
    String sessionId = httpSession.getId();
    File tmpFolder = new File(tmpOsm.getParentFile(), sessionId);
    String fileName = "gpx_" + sessionId;
    QueryParams qp = new QueryParams();
    qp.osmFile = tmpOsm;
    qp.details = QueryParams.DETAILS_ELE_SPEED;
    OsmGpxWriteContext writeCtx = new OsmGpxWriteContext(qp);
    File targetObf = new File(tmpFolder.getParentFile(), fileName + IndexConstants.BINARY_MAP_INDEX_EXT);
    writeCtx.writeObf(files, tmpFolder, fileName, targetObf);
    ctx.tempFiles.add(targetObf);
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"gpx.obf\""));
    headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-binary");
    return ResponseEntity.ok().headers(headers).body(new FileSystemResource(targetObf));
}
Also used : GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) HttpHeaders(org.springframework.http.HttpHeaders) GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) ArrayList(java.util.ArrayList) QueryParams(net.osmand.obf.OsmGpxWriteContext.QueryParams) FileSystemResource(org.springframework.core.io.FileSystemResource) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) OsmGpxWriteContext(net.osmand.obf.OsmGpxWriteContext) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with GPXSessionContext

use of net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext in project OsmAnd-tools by osmandapp.

the class GpxController method getGpx.

@GetMapping(path = { "/get-gpx-file" }, produces = "application/json")
@ResponseBody
public ResponseEntity<Resource> getGpx(@RequestParam(required = true) String name, HttpSession httpSession) throws IOException {
    GPXSessionContext ctx = session.getGpxResources(httpSession);
    File tmpGpx = null;
    for (int i = 0; i < ctx.files.size(); i++) {
        GPXSessionFile file = ctx.files.get(i);
        if (file.analysis != null && file.analysis.name.equals(name)) {
            tmpGpx = file.file;
        }
    }
    if (tmpGpx == null) {
        return ResponseEntity.notFound().build();
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"" + name + "\""));
    headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-binary");
    return ResponseEntity.ok().headers(headers).body(new FileSystemResource(tmpGpx));
}
Also used : GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) HttpHeaders(org.springframework.http.HttpHeaders) GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) FileSystemResource(org.springframework.core.io.FileSystemResource) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) GetMapping(org.springframework.web.bind.annotation.GetMapping) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with GPXSessionContext

use of net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext in project OsmAnd-tools by osmandapp.

the class GpxController method clear.

@PostMapping(path = { "/clear" }, produces = "application/json")
@ResponseBody
public String clear(HttpServletRequest request, HttpSession httpSession) throws IOException {
    GPXSessionContext ctx = session.getGpxResources(httpSession);
    for (File f : ctx.tempFiles) {
        f.delete();
    }
    ctx.tempFiles.clear();
    ctx.files.clear();
    return gson.toJson(Map.of("all", ctx.files));
}
Also used : GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) PostMapping(org.springframework.web.bind.annotation.PostMapping) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with GPXSessionContext

use of net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext in project OsmAnd-tools by osmandapp.

the class GpxController method uploadGpx.

@PostMapping(path = { "/upload-session-gpx" }, produces = "application/json")
public ResponseEntity<String> uploadGpx(@RequestPart(name = "file") @Valid @NotNull @NotEmpty MultipartFile file, HttpServletRequest request, HttpSession httpSession) throws IOException {
    GPXSessionContext ctx = session.getGpxResources(httpSession);
    File tmpGpx = File.createTempFile("gpx_" + httpSession.getId(), ".gpx");
    double fileSizeMb = file.getSize() / (double) (1 << 20);
    double filesSize = getCommonSavedFilesSize(ctx.files);
    double maxSizeMb = getCommonMaxSizeFiles();
    if (fileSizeMb + filesSize > maxSizeMb) {
        return ResponseEntity.badRequest().body(String.format("You don't have enough cloud space to store this file!" + "\nUploaded file size: %.1f MB." + "\nMax cloud space: %.0f MB." + "\nAvailable free space: %.1f MB.", fileSizeMb, maxSizeMb, maxSizeMb - filesSize));
    }
    InputStream is = file.getInputStream();
    FileOutputStream fous = new FileOutputStream(tmpGpx);
    Algorithms.streamCopy(is, fous);
    is.close();
    fous.close();
    ctx.tempFiles.add(tmpGpx);
    GPXFile gpxFile = GPXUtilities.loadGPXFile(tmpGpx);
    if (gpxFile.error != null) {
        return ResponseEntity.badRequest().body("Error reading gpx!");
    } else {
        GPXSessionFile sessionFile = new GPXSessionFile();
        ctx.files.add(sessionFile);
        gpxFile.path = file.getOriginalFilename();
        GPXTrackAnalysis analysis = gpxFile.getAnalysis(System.currentTimeMillis());
        sessionFile.file = tmpGpx;
        sessionFile.size = fileSizeMb;
        cleanupFromNan(analysis);
        sessionFile.analysis = analysis;
        GPXFile srtmGpx = calculateSrtmAltitude(gpxFile, null);
        GPXTrackAnalysis srtmAnalysis = null;
        if (srtmGpx != null) {
            srtmAnalysis = srtmGpx.getAnalysis(System.currentTimeMillis());
        }
        sessionFile.srtmAnalysis = srtmAnalysis;
        if (srtmAnalysis != null) {
            cleanupFromNan(srtmAnalysis);
        }
        return ResponseEntity.ok(gson.toJson(Map.of("info", sessionFile)));
    }
}
Also used : GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) GPXSessionContext(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

File (java.io.File)4 GPXFile (net.osmand.GPXUtilities.GPXFile)4 GPXSessionContext (net.osmand.server.controllers.pub.UserSessionResources.GPXSessionContext)4 GPXSessionFile (net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile)4 MultipartFile (org.springframework.web.multipart.MultipartFile)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 StreamingResponseBody (org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody)3 FileSystemResource (org.springframework.core.io.FileSystemResource)2 HttpHeaders (org.springframework.http.HttpHeaders)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 GPXTrackAnalysis (net.osmand.GPXUtilities.GPXTrackAnalysis)1 OsmGpxWriteContext (net.osmand.obf.OsmGpxWriteContext)1 QueryParams (net.osmand.obf.OsmGpxWriteContext.QueryParams)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1