Search in sources :

Example 1 with UserFile

use of net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile in project OsmAnd-tools by osmandapp.

the class UserdataController method getFile.

public void getFile(HttpServletResponse response, HttpServletRequest request, String name, String type, Long updatetime, PremiumUserDevice dev) throws IOException {
    InputStream bin = null;
    try {
        @SuppressWarnings("unchecked") ResponseEntity<String>[] error = new ResponseEntity[] { null };
        UserFile userFile = getUserFile(name, type, updatetime, dev);
        bin = getInputStream(dev, error, userFile);
        if (error[0] != null) {
            response.setStatus(error[0].getStatusCodeValue());
            response.getWriter().write(error[0].getBody());
            return;
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + userFile.name);
        // InputStream bin = fl.data.getBinaryStream();
        String acceptEncoding = request.getHeader("Accept-Encoding");
        if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
            response.setHeader("Content-Encoding", "gzip");
        } else {
            bin = new GZIPInputStream(bin);
        }
        response.setContentType(APPLICATION_OCTET_STREAM.getType());
        byte[] buf = new byte[BUFFER_SIZE];
        int r;
        while ((r = bin.read(buf)) != -1) {
            response.getOutputStream().write(buf, 0, r);
        }
    } 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)

Example 2 with UserFile

use of net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile in project OsmAnd-tools by osmandapp.

the class UserdataController method deleteFile.

@PostMapping(value = "/delete-file-version")
@ResponseBody
public ResponseEntity<String> deleteFile(HttpServletResponse response, HttpServletRequest request, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "updatetime", required = true) Long updatetime, @RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken) throws IOException, SQLException {
    UserFile fl = null;
    PremiumUserDevice dev = checkToken(deviceId, accessToken);
    if (dev == null) {
        return tokenNotValid();
    } else {
        if (updatetime != null) {
            fl = filesRepository.findTopByUseridAndNameAndTypeAndUpdatetime(dev.userid, name, type, new Date(updatetime));
        }
        if (fl == null) {
            return error(ERROR_CODE_FILE_NOT_AVAILABLE, "File is not available");
        }
        storageService.deleteFile(fl.storage, userFolder(fl), storageFileName(fl));
        filesRepository.delete(fl);
        return ok();
    }
}
Also used : UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with UserFile

use of net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile in project OsmAnd-tools by osmandapp.

the class UserdataController method upload.

@PostMapping(value = "/upload-file", consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public ResponseEntity<String> upload(@RequestPart(name = "file") @Valid @NotNull @NotEmpty MultipartFile file, @RequestParam(name = "name", required = true) String name, @RequestParam(name = "type", required = true) String type, @RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken, @RequestParam(name = "clienttime", required = false) Long clienttime) throws IOException {
    // This could be slow series of checks (token, user, subscription, amount of space):
    // probably it's better to support multiple file upload without these checks
    PremiumUserDevice dev = checkToken(deviceId, accessToken);
    if (dev == null) {
        return tokenNotValid();
    }
    PremiumUser pu = usersRepository.findById(dev.userid);
    if (pu == null) {
        return error(ERROR_CODE_USER_IS_NOT_REGISTERED, "Unexpected error: user is not registered.");
    }
    String errorMsg = checkOrderIdPremium(pu.orderid);
    if (errorMsg != null) {
        return error(ERROR_CODE_SUBSCRIPTION_WAS_EXPIRED_OR_NOT_PRESENT, "Subscription is not valid any more: " + errorMsg);
    }
    UserFilesResults res = generateFiles(dev.userid, null, null, false, false);
    if (res.totalZipSize > MAXIMUM_ACCOUNT_SIZE) {
        return error(ERROR_CODE_SIZE_OF_SUPPORTED_BOX_IS_EXCEEDED, "Maximum size of OsmAnd Cloud exceeded " + (MAXIMUM_ACCOUNT_SIZE / MB) + " MB. Please contact support in order to investigate possible solutions.");
    }
    UserFile usf = new PremiumUserFilesRepository.UserFile();
    long cnt, sum;
    long zipsize = file.getSize();
    try {
        GZIPInputStream gzis = new GZIPInputStream(file.getInputStream());
        byte[] buf = new byte[1024];
        sum = 0;
        while ((cnt = gzis.read(buf)) >= 0) {
            sum += cnt;
        }
    } catch (IOException e) {
        return error(ERROR_CODE_GZIP_ONLY_SUPPORTED_UPLOAD, "File is submitted not in gzip format");
    }
    usf.name = name;
    usf.type = type;
    usf.updatetime = new Date();
    usf.userid = dev.userid;
    usf.deviceid = deviceId;
    usf.filesize = sum;
    usf.zipfilesize = zipsize;
    if (clienttime != null) {
        usf.clienttime = new Date(clienttime.longValue());
    }
    // Session session = entityManager.unwrap(Session.class);
    // Blob blob = session.getLobHelper().createBlob(file.getInputStream(), file.getSize());
    // usf.data = blob;
    usf.storage = storageService.save(userFolder(usf), storageFileName(usf), file);
    if (storageService.storeLocally()) {
        usf.data = file.getBytes();
    }
    filesRepository.saveAndFlush(usf);
    return ok();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) UserFile(net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile) PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) IOException(java.io.IOException) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with UserFile

use of net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile 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 5 with UserFile

use of net.osmand.server.api.repo.PremiumUserFilesRepository.UserFile 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)

Aggregations

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