Search in sources :

Example 1 with PremiumUserDevice

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

the class UserdataController 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, @RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken) throws IOException, SQLException {
    PremiumUserDevice dev = checkToken(deviceId, accessToken);
    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 2 with PremiumUserDevice

use of net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice 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 PremiumUserDevice

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

the class UserdataController method check.

@GetMapping(value = "/user-validate-sub")
@ResponseBody
public ResponseEntity<String> check(@RequestParam(name = "deviceid", required = true) int deviceId, @RequestParam(name = "accessToken", required = true) String accessToken) throws IOException {
    PremiumUserDevice dev = checkToken(deviceId, accessToken);
    if (dev == null) {
        return tokenNotValid();
    }
    PremiumUser pu = usersRepository.findById(dev.userid);
    if (pu == null) {
        return error(ERROR_CODE_EMAIL_IS_INVALID, "email is not registered");
    }
    String errorMsg = checkOrderIdPremium(pu.orderid);
    if (errorMsg != null) {
        return error(ERROR_CODE_NO_VALID_SUBSCRIPTION, errorMsg);
    }
    return ResponseEntity.ok(gson.toJson(pu));
}
Also used : PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with PremiumUserDevice

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

the class UserdataController method registerNewDevice.

private ResponseEntity<String> registerNewDevice(String email, String token, String deviceId, String accessToken) {
    PremiumUser pu = usersRepository.findByEmail(email);
    if (pu == null) {
        return error(ERROR_CODE_USER_IS_NOT_REGISTERED, "user with that email is not registered");
    }
    if (pu.token == null || !pu.token.equals(token) || pu.tokenTime == null || System.currentTimeMillis() - pu.tokenTime.getTime() > TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS)) {
        return error(ERROR_CODE_TOKEN_IS_NOT_VALID_OR_EXPIRED, "token is not valid or expired (24h)");
    }
    pu.token = null;
    pu.tokenTime = null;
    PremiumUserDevice device = new PremiumUserDevice();
    PremiumUserDevice sameDevice;
    while ((sameDevice = devicesRepository.findTopByUseridAndDeviceidOrderByUdpatetimeDesc(pu.id, deviceId)) != null) {
        devicesRepository.delete(sameDevice);
    }
    device.userid = pu.id;
    device.deviceid = deviceId;
    device.udpatetime = new Date();
    device.accesstoken = accessToken;
    usersRepository.saveAndFlush(pu);
    devicesRepository.saveAndFlush(device);
    return ResponseEntity.ok(gson.toJson(device));
}
Also used : PremiumUser(net.osmand.server.api.repo.PremiumUsersRepository.PremiumUser) PremiumUserDevice(net.osmand.server.api.repo.PremiumUserDevicesRepository.PremiumUserDevice) Date(java.util.Date)

Example 5 with PremiumUserDevice

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

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