use of org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk in project kie-wb-common by kiegroup.
the class DocumentUploadManager method initFileReader.
public void initFileReader() {
fileReader = new FileReader();
fileReader.onload = event -> {
if (fileReader.readyState == FileReader.DONE) {
String[] split = fileReader.result.asString().split(",");
String content = "";
if (split.length == 2) {
content = split[1];
}
if (activeSession == null) {
return null;
}
DocumentUploadChunk newChunk = new DocumentUploadChunk(activeSession.documentId, activeSession.file.name, activeSession.chunk, activeSession.maxChunks, content);
uploadService.call((RemoteCallback<DocumentUploadResponse>) response -> {
if (response.getState().equals(DocumentUploadResponse.DocumentUploadState.FINISH)) {
activeSession.onUploadEnd.execute(response.isSuccess());
activeSession = null;
startUpload();
} else if (!response.isSuccess()) {
activeSession.onUploadEnd.execute(response.isSuccess());
activeSession = null;
startUpload();
}
}, (ErrorCallback<Message>) (message, throwable) -> {
activeSession.onUploadEnd.execute(false);
activeSession = null;
startUpload();
return false;
}).uploadContent(newChunk);
if (activeSession.nextChunk <= activeSession.file.size) {
upload(activeSession.nextChunk);
}
}
return null;
};
}
use of org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk in project kie-wb-common by kiegroup.
the class UploadedDocumentServiceImplTest method testSuccessfullyUpload.
@Test
public void testSuccessfullyUpload() {
DocumentUploadChunk chunk1 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 0, 3, Base64.getEncoder().encodeToString(PART_1.getBytes()));
DocumentUploadResponse response = uploadedDocumentService.uploadContent(chunk1);
validateResponse(response, DocumentUploadResponse.DocumentUploadState.UPLOADING, true);
DocumentUploadSession session = uploadedDocumentService.getUploadSessions().get(DOC_ID);
Assertions.assertThat(session).isNotNull();
Assertions.assertThat(session.getChunks()).isNotNull().hasSize(1).containsExactly(chunk1);
checkParts(1);
DocumentUploadChunk chunk2 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 1, 3, Base64.getEncoder().encodeToString(PART_2.getBytes()));
response = uploadedDocumentService.uploadContent(chunk2);
validateResponse(response, DocumentUploadResponse.DocumentUploadState.UPLOADING, true);
session = uploadedDocumentService.getUploadSessions().get(DOC_ID);
Assertions.assertThat(session).isNotNull();
Assertions.assertThat(session.getChunks()).isNotNull().hasSize(2).containsExactly(chunk1, chunk2);
checkParts(2);
DocumentUploadChunk chunk3 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 3, 3, Base64.getEncoder().encodeToString(PART_3.getBytes()));
response = uploadedDocumentService.uploadContent(chunk3);
validateResponse(response, DocumentUploadResponse.DocumentUploadState.FINISH, true);
session = uploadedDocumentService.getUploadSessions().get(DOC_ID);
Assertions.assertThat(session).isNull();
File file = storage.getRootFolder().resolve(DOC_ID).resolve(DOC_NAME).toFile();
Assertions.assertThat(file).isNotNull().exists();
String content = new String(storage.getContent(DOC_ID));
Assertions.assertThat(content).isNotNull().isEqualTo(PART_1 + PART_2 + PART_3);
}
use of org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk in project kie-wb-common by kiegroup.
the class FormsDocumentServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, Object> response = new HashMap<>();
try {
FileItem fileItem = getFileItem(req);
String id = UUID.randomUUID().toString();
String content = Base64.getEncoder().encodeToString(fileItem.get());
DocumentUploadChunk chunk = new DocumentUploadChunk(id, fileItem.getName(), 0, 1, content);
DocumentUploadSession session = new DocumentUploadSession(chunk.getDocumentId(), chunk.getDocumentName(), chunk.getMaxChunks());
session.add(chunk);
storage.uploadContentChunk(chunk);
session.setState(DocumentUploadSession.State.MERGING);
storage.merge(session);
DocumentData data = new DocumentData(id, fileItem.getName(), fileItem.getSize(), "", System.currentTimeMillis());
response.put("document", data);
} catch (Exception e) {
response.put("error", "error");
} finally {
writeResponse(resp, response);
}
}
use of org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk in project kie-wb-common by kiegroup.
the class UploadedDocumentStorageImpl method doMerge.
private File doMerge(DocumentUploadSession session) throws Exception {
File docFolder = resolveDocStorage(session.getDocumentId());
File destination = docFolder.toPath().resolve(session.getDocumentName()).toFile();
if (destination.exists()) {
destination.delete();
}
destination.createNewFile();
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) {
for (DocumentUploadChunk chunk : session.getChunks()) {
if (!session.getState().equals(DocumentUploadSession.State.MERGING)) {
return null;
}
IOUtils.copy(new FileInputStream(resolveChunkFile(chunk)), out);
}
FileUtils.deleteQuietly(docFolder.toPath().resolve(PARTS_FOLDER).toFile());
}
session.setState(DocumentUploadSession.State.MERGED);
return destination;
}
use of org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk in project kie-wb-common by kiegroup.
the class UploadedDocumentServiceImplTest method testUploadWithError.
@Test
public void testUploadWithError() throws Exception {
doThrow(new RuntimeException("Exception")).when(storage).uploadContentChunk(any());
DocumentUploadChunk chunk = new DocumentUploadChunk(DOC_ID, DOC_NAME, 0, 3, Base64.getEncoder().encodeToString(PART_1.getBytes()));
DocumentUploadResponse response = uploadedDocumentService.uploadContent(chunk);
validateResponse(response, DocumentUploadResponse.DocumentUploadState.FINISH, false);
}
Aggregations