Skip to content

Commit

Permalink
Merge pull request #10 from nov1n/master
Browse files Browse the repository at this point in the history
Update project
  • Loading branch information
jlarriba authored Oct 28, 2021
2 parents e8cbd35 + c83895b commit 40f8108
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/main/java/es/jlarriba/jrmapi/Authentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
*/
public class Authentication {

private static final String DEVICE_AUTH_URL = "https://my.remarkable.com/token/json/2/device/new";
private static final String USER_AUTH_URL = "https://my.remarkable.com/token/json/2/user/new";
private static final String DEVICE_AUTH_URL =
"https://webapp-production-dot-remarkable-production.appspot.com/token/json/2/device/new";
private static final String USER_AUTH_URL =
"https://webapp-production-dot-remarkable-production.appspot.com/token/json/2/user/new";

private static final File RMAPI_PROPERTIES_FILE = new File(System.getProperty("user.home"), "/.rmapi");
private static final String DEVICETOKEN_PROPERTY_NAME = "devicetoken";
Expand Down Expand Up @@ -65,6 +67,10 @@ public String userToken() {
return net.post(USER_AUTH_URL, getDeviceToken());
}

public String userToken(String deviceToken) {
return net.post(USER_AUTH_URL, deviceToken);
}

protected String getDeviceToken() {
try {
Properties properties = new Properties();
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/es/jlarriba/jrmapi/Jrmapi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
import es.jlarriba.jrmapi.commonsio.FilenameUtils;
import es.jlarriba.jrmapi.http.Net;
import es.jlarriba.jrmapi.model.DeleteDocument;

import java.util.ArrayList;
import java.util.List;
import es.jlarriba.jrmapi.model.Document;
import es.jlarriba.jrmapi.model.MetadataDocument;
import es.jlarriba.jrmapi.model.UploadDocumentRequest;
import es.jlarriba.jrmapi.model.UploadDocumentResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.logging.log4j.LogManager;
Expand All @@ -50,15 +48,21 @@ public class Jrmapi {
private static final String UPLOAD_REQUEST = BASE_URL + "/document-storage/json/2/upload/request";
private static final String DELETE = BASE_URL + "/document-storage/json/2/delete";

private final Gson gson;
private final Gson gson = new Gson();
private final String userToken;
private final Net net;
private final Net net = new Net();

public Jrmapi() {
Authentication auth = new Authentication();
userToken = auth.userToken();
net = new Net();
gson = new Gson();
userToken = new Authentication().userToken();
createWorkdir();
}

public Jrmapi(String deviceToken) {
userToken = new Authentication().userToken(deviceToken);
createWorkdir();
}

private void createWorkdir() {
File workdir = new File(Utils.WORKDIR);
if (!workdir.exists()) {
workdir.mkdir();
Expand Down Expand Up @@ -98,6 +102,16 @@ public void fetchDoc(Document doc, String path) {
}
}

public File fetchZip(Document doc, String path) {
String response = net.get(LIST_DOCS, userToken, doc.getID());
List<Document> docs = gson.fromJson(response, new TypeToken<List<Document>>() {
}.getType());
File file = new File(path + doc.getVissibleName() + ".zip");
LOGGER.debug("Download zip to " + path + doc.getVissibleName() + ".zip");
net.getStream(docs.get(0).getBlobURLGet(), userToken, file);
return file;
}

public void exportPdf(Document doc, String path, String filename) {
String response = net.get(LIST_DOCS, userToken, doc.getID());
List<Document> docs = gson.fromJson(response, new TypeToken<List<Document>>(){}.getType());
Expand All @@ -115,7 +129,7 @@ public void exportPdf(Document doc, String path, String filename) {
}
}

public void createDir(String name, String parentID) {
public String createDir(String name, String parentID) {
String id = UUID.randomUUID().toString();
UploadDocumentRequest docRequest = new UploadDocumentRequest();
docRequest.setID(id);
Expand All @@ -141,6 +155,7 @@ public void createDir(String name, String parentID) {
uploadMetadataDoc.add(metadataDoc);

net.put(UPDATE_STATUS, userToken, uploadMetadataDoc);
return docResponse.get(0).getID();
}

public void uploadDoc(File file, String parentID) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/es/jlarriba/jrmapi/http/Net.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package es.jlarriba.jrmapi.http;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

import com.google.gson.Gson;
import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -121,7 +125,7 @@ private String sendRequest(HttpRequest request) {
private void sendRequest(HttpRequest request, File file) {
try {
LOGGER.debug(request.uri());
var response = client.send(request, BodyHandlers.ofFile(file.toPath()));
var response = client.send(request, BodyHandlers.ofFile(file.toPath(), CREATE, WRITE, TRUNCATE_EXISTING));
LOGGER.debug(response.statusCode());
} catch (IOException | InterruptedException e) {
LOGGER.error("Error while launching request", e);
Expand Down

0 comments on commit 40f8108

Please sign in to comment.