Search in sources :

Example 1 with DocumentUploadChunk

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;
    };
}
Also used : ErrorCallback(org.jboss.errai.common.client.api.ErrorCallback) FileReader(elemental2.dom.FileReader) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) RemoteCallback(org.jboss.errai.common.client.api.RemoteCallback)

Example 2 with DocumentUploadChunk

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);
}
Also used : DocumentUploadResponse(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadResponse) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) File(java.io.File) Test(org.junit.Test)

Example 3 with DocumentUploadChunk

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);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) DocumentUploadSession(org.kie.workbench.common.forms.jbpm.server.service.impl.documents.DocumentUploadSession) HashMap(java.util.HashMap) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with DocumentUploadChunk

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 5 with DocumentUploadChunk

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);
}
Also used : DocumentUploadResponse(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadResponse) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) Test(org.junit.Test)

Aggregations

DocumentUploadChunk (org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk)7 Test (org.junit.Test)4 DocumentUploadResponse (org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadResponse)4 File (java.io.File)2 FileReader (elemental2.dom.FileReader)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ServletException (javax.servlet.ServletException)1 FileItem (org.apache.commons.fileupload.FileItem)1 ErrorCallback (org.jboss.errai.common.client.api.ErrorCallback)1 RemoteCallback (org.jboss.errai.common.client.api.RemoteCallback)1 DocumentData (org.kie.workbench.common.forms.jbpm.model.document.DocumentData)1 DocumentUploadSession (org.kie.workbench.common.forms.jbpm.server.service.impl.documents.DocumentUploadSession)1