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));
}
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));
}
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));
}
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)));
}
}
Aggregations