Search in sources :

Example 6 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice in project OsmAnd-tools by osmandapp.

the class MapApiController method getSrtmGpx.

@GetMapping(path = { "/get-srtm-gpx-info" }, produces = "application/json")
@ResponseBody
public ResponseEntity<String> getSrtmGpx(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = false) Long updatetime) throws IOException {
    PremiumUserDevice dev = checkUser();
    InputStream bin = null;
    try {
        @SuppressWarnings("unchecked") ResponseEntity<String>[] error = new ResponseEntity[] { null };
        UserFile userFile = userdataController.getUserFile(name, type, updatetime, dev);
        if (analysisPresent(SRTM_ANALYSIS, userFile)) {
            return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", userFile.details.get(SRTM_ANALYSIS))));
        }
        bin = userdataController.getInputStream(dev, error, userFile);
        ResponseEntity<String> err = error[0];
        if (err != null) {
            response.setStatus(err.getStatusCodeValue());
            response.getWriter().write(err.getBody());
            return err;
        }
        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(bin));
        if (gpxFile == null) {
            return ResponseEntity.badRequest().body(String.format("File %s not found", userFile.name));
        }
        GPXFile srtmGpx = gpxController.calculateSrtmAltitude(gpxFile, null);
        GPXTrackAnalysis analysis = srtmGpx == null ? null : getAnalysis(userFile, srtmGpx);
        if (!analysisPresent(SRTM_ANALYSIS, userFile)) {
            saveAnalysis(SRTM_ANALYSIS, userFile, analysis);
        }
        return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", analysis)));
    } finally {
        if (bin != null) {
            bin.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ResponseEntity(org.springframework.http.ResponseEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice in project OsmAnd-tools by osmandapp.

the class MapApiController method getGpxInfo.

@SuppressWarnings("unchecked")
@GetMapping(value = "/get-gpx-info")
@ResponseBody
public ResponseEntity<String> getGpxInfo(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = false) Long updatetime) throws IOException, SQLException {
    PremiumUserDevice dev = checkUser();
    InputStream bin = null;
    try {
        ResponseEntity<String>[] error = new ResponseEntity[] { null };
        UserFile userFile = userdataController.getUserFile(name, type, updatetime, dev);
        if (analysisPresent(ANALYSIS, userFile)) {
            return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", userFile.details.get(ANALYSIS))));
        }
        bin = userdataController.getInputStream(dev, error, userFile);
        ResponseEntity<String> err = error[0];
        if (err != null) {
            response.setStatus(err.getStatusCodeValue());
            response.getWriter().write(err.getBody());
            return err;
        }
        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(bin));
        if (gpxFile == null) {
            return ResponseEntity.badRequest().body(String.format("File %s not found", userFile.name));
        }
        GPXTrackAnalysis analysis = getAnalysis(userFile, gpxFile);
        if (!analysisPresent(ANALYSIS, userFile)) {
            saveAnalysis(ANALYSIS, userFile, analysis);
        }
        return ResponseEntity.ok(gson.toJson(Collections.singletonMap("info", analysis)));
    } finally {
        if (bin != null) {
            bin.close();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ResponseEntity(org.springframework.http.ResponseEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice in project OsmAnd-tools by osmandapp.

the class MapApiController method getFile.

@GetMapping(value = "/download-file")
@ResponseBody
public void getFile(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = false) Long updatetime) throws IOException, SQLException {
    PremiumUserDevice dev = checkUser();
    if (dev == null) {
        ResponseEntity<String> error = tokenNotValid();
        if (error != null) {
            response.setStatus(error.getStatusCodeValue());
            response.getWriter().write(error.getBody());
            return;
        }
    }
    userdataController.getFile(response, request, name, type, updatetime, dev);
}
Also used : PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice in project OsmAnd-tools by osmandapp.

the class MapApiController method listFiles.

@GetMapping(value = "/list-files")
@ResponseBody
public ResponseEntity<String> listFiles(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "type", required = false) String type, @RequestParam(name = "allVersions", required = false, defaultValue = "false") boolean allVersions) throws IOException, SQLException {
    PremiumUserDevice dev = checkUser();
    if (dev == null) {
        return tokenNotValid();
    }
    UserFilesResults res = userdataController.generateFiles(dev.userid, name, type, allVersions, true);
    for (UserFileNoData nd : res.uniqueFiles) {
        String ext = nd.name.substring(nd.name.lastIndexOf('.') + 1);
        if (nd.type.equalsIgnoreCase("gpx") && ext.equalsIgnoreCase("gpx") && !analysisPresent(ANALYSIS, nd.details)) {
            GPXTrackAnalysis analysis = null;
            Optional<UserFile> of = userFilesRepository.findById(nd.id);
            UserFile uf = of.get();
            if (uf != null) {
                try {
                    InputStream in = uf.data != null ? new ByteArrayInputStream(uf.data) : userdataController.getInputStream(uf);
                    if (in != null) {
                        GPXFile gpxFile = GPXUtilities.loadGPXFile(new GZIPInputStream(in));
                        if (gpxFile != null) {
                            analysis = getAnalysis(uf, gpxFile);
                        }
                    }
                } catch (RuntimeException e) {
                }
                saveAnalysis(ANALYSIS, uf, analysis);
                nd.details = uf.details.deepCopy();
            }
        }
        if (analysisPresent(ANALYSIS, nd.details)) {
            nd.details.get(ANALYSIS).getAsJsonObject().remove("speedData");
            nd.details.get(ANALYSIS).getAsJsonObject().remove("elevationData");
        }
        if (analysisPresent(SRTM_ANALYSIS, nd.details)) {
            nd.details.get(SRTM_ANALYSIS).getAsJsonObject().remove("speedData");
            nd.details.get(SRTM_ANALYSIS).getAsJsonObject().remove("elevationData");
        }
    }
    return ResponseEntity.ok(gson.toJson(res));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) UserFilesResults(net.osmand.server.controllers.pub.UserdataController.UserFilesResults) ByteArrayInputStream(java.io.ByteArrayInputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) GPXFile(net.osmand.GPXUtilities.GPXFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) UserFileNoData(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFileNoData) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice in project OsmAnd-tools by osmandapp.

the class UserdataController method migrateData.

@PostMapping(value = "/backup-storage")
@ResponseBody
public ResponseEntity<String> migrateData(@RequestParam(name = "storageid", required = true) String storageId, @RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken) throws IOException, SQLException {
    if (!storageService.hasStorageProviderById(storageId)) {
        return error(400, "Storage id is not configured");
    }
    PremiumUserDevice dev = checkToken(deviceId, accessToken);
    if (dev == null) {
        return tokenNotValid();
    }
    Iterable<UserFile> lst = filesRepository.findAllByUserid(dev.userid);
    for (UserFile fl : lst) {
        if (fl != null && fl.filesize > 0) {
            String newStorage = storageService.backupData(storageId, userFolder(fl), storageFileName(fl), fl.storage, fl.data);
            if (newStorage != null) {
                fl.storage = newStorage;
                filesRepository.save(fl);
            }
        }
    }
    return ok();
}
Also used : UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PremiumUserDevice (net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice)13 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)12 UserFile (net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile)8 GetMapping (org.springframework.web.bind.annotation.GetMapping)7 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 Date (java.util.Date)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 GPXFile (net.osmand.GPXUtilities.GPXFile)3 GPXTrackAnalysis (net.osmand.GPXUtilities.GPXTrackAnalysis)3 PremiumUser (net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser)3 ResponseEntity (org.springframework.http.ResponseEntity)2 IOException (java.io.IOException)1 UserFileNoData (net.osmand.server.api.repo.PremiumUserFilesRepository.UserFileNoData)1 UserFilesResults (net.osmand.server.controllers.pub.UserdataController.UserFilesResults)1