mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Closes #53: Build thumbnails for DOCX and ODT files
This commit is contained in:
parent
1a37d97a61
commit
7708f61343
@ -21,7 +21,7 @@ Features
|
|||||||
|
|
||||||
- Responsive user interface
|
- Responsive user interface
|
||||||
- Optical character recognition
|
- Optical character recognition
|
||||||
- Support image and PDF files
|
- Support image, PDF, ODT and DOCX files
|
||||||
- Flexible search engine
|
- Flexible search engine
|
||||||
- Full text search in image and PDF
|
- Full text search in image and PDF
|
||||||
- 256-bit AES encryption
|
- 256-bit AES encryption
|
||||||
|
@ -28,58 +28,43 @@ public class FileCreatedAsyncEvent {
|
|||||||
private InputStream inputStream;
|
private InputStream inputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter of file.
|
* Unencrypted input stream containing a PDF representation
|
||||||
*
|
* of the file. May be null if the PDF conversion is not
|
||||||
* @return the file
|
* necessary or not possible.
|
||||||
*/
|
*/
|
||||||
|
private InputStream pdfInputStream;
|
||||||
|
|
||||||
public File getFile() {
|
public File getFile() {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter of file.
|
|
||||||
*
|
|
||||||
* @param file file
|
|
||||||
*/
|
|
||||||
public void setFile(File file) {
|
public void setFile(File file) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter of document.
|
|
||||||
*
|
|
||||||
* @return the document
|
|
||||||
*/
|
|
||||||
public Document getDocument() {
|
public Document getDocument() {
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter of document.
|
|
||||||
*
|
|
||||||
* @param document document
|
|
||||||
*/
|
|
||||||
public void setDocument(Document document) {
|
public void setDocument(Document document) {
|
||||||
this.document = document;
|
this.document = document;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter of inputStream.
|
|
||||||
*
|
|
||||||
* @return the inputStream
|
|
||||||
*/
|
|
||||||
public InputStream getInputStream() {
|
public InputStream getInputStream() {
|
||||||
return inputStream;
|
return inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter de inputStream.
|
|
||||||
*
|
|
||||||
* @param inputStream inputStream
|
|
||||||
*/
|
|
||||||
public void setInputStream(InputStream inputStream) {
|
public void setInputStream(InputStream inputStream) {
|
||||||
this.inputStream = inputStream;
|
this.inputStream = inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InputStream getPdfInputStream() {
|
||||||
|
return pdfInputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPdfInputStream(InputStream pdfInputStream) {
|
||||||
|
this.pdfInputStream = pdfInputStream;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -12,7 +12,6 @@ import com.sismics.docs.core.event.FileCreatedAsyncEvent;
|
|||||||
import com.sismics.docs.core.model.jpa.File;
|
import com.sismics.docs.core.model.jpa.File;
|
||||||
import com.sismics.docs.core.util.FileUtil;
|
import com.sismics.docs.core.util.FileUtil;
|
||||||
import com.sismics.docs.core.util.TransactionUtil;
|
import com.sismics.docs.core.util.TransactionUtil;
|
||||||
import com.sismics.util.mime.MimeTypeUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener on file created.
|
* Listener on file created.
|
||||||
@ -39,12 +38,15 @@ public class FileCreatedAsyncListener {
|
|||||||
|
|
||||||
// Guess the mime type a second time, for open document format (first detected as simple ZIP file)
|
// Guess the mime type a second time, for open document format (first detected as simple ZIP file)
|
||||||
final File file = fileCreatedAsyncEvent.getFile();
|
final File file = fileCreatedAsyncEvent.getFile();
|
||||||
file.setMimeType(MimeTypeUtil.guessOpenDocumentFormat(file, fileCreatedAsyncEvent.getInputStream()));
|
|
||||||
|
|
||||||
// Extract text content from the file
|
// Extract text content from the file
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
final String content = FileUtil.extractContent(fileCreatedAsyncEvent.getDocument(), file, fileCreatedAsyncEvent.getInputStream());
|
final String content = FileUtil.extractContent(fileCreatedAsyncEvent.getDocument(), file,
|
||||||
|
fileCreatedAsyncEvent.getInputStream(), fileCreatedAsyncEvent.getPdfInputStream());
|
||||||
fileCreatedAsyncEvent.getInputStream().close();
|
fileCreatedAsyncEvent.getInputStream().close();
|
||||||
|
if (fileCreatedAsyncEvent.getPdfInputStream() != null) {
|
||||||
|
fileCreatedAsyncEvent.getPdfInputStream().close();
|
||||||
|
}
|
||||||
log.info(MessageFormat.format("File content extracted in {0}ms", System.currentTimeMillis() - startTime));
|
log.info(MessageFormat.format("File content extracted in {0}ms", System.currentTimeMillis() - startTime));
|
||||||
|
|
||||||
// Store the text content in the database
|
// Store the text content in the database
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.sismics.docs.core.util;
|
package com.sismics.docs.core.util;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -48,19 +50,16 @@ public class FileUtil {
|
|||||||
* @param document Document linked to the file
|
* @param document Document linked to the file
|
||||||
* @param file File to extract
|
* @param file File to extract
|
||||||
* @param inputStream Unencrypted input stream
|
* @param inputStream Unencrypted input stream
|
||||||
|
* @param pdfInputStream Unencrypted PDF input stream
|
||||||
* @return Content extract
|
* @return Content extract
|
||||||
*/
|
*/
|
||||||
public static String extractContent(Document document, File file, InputStream inputStream) {
|
public static String extractContent(Document document, File file, InputStream inputStream, InputStream pdfInputStream) {
|
||||||
String content = null;
|
String content = null;
|
||||||
|
|
||||||
if (ImageUtil.isImage(file.getMimeType())) {
|
if (ImageUtil.isImage(file.getMimeType())) {
|
||||||
content = ocrFile(inputStream, document);
|
content = ocrFile(inputStream, document);
|
||||||
} else if (file.getMimeType().equals(MimeType.APPLICATION_PDF)) {
|
} else if (pdfInputStream != null) {
|
||||||
content = extractPdf(inputStream);
|
content = extractPdf(pdfInputStream);
|
||||||
} else if (file.getMimeType().equals(MimeType.OPEN_DOCUMENT_TEXT)) {
|
|
||||||
content = extractOpenDocumentText(inputStream);
|
|
||||||
} else if (file.getMimeType().equals(MimeType.OFFICE_DOCUMENT)) {
|
|
||||||
content = extractOfficeDocument(inputStream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
@ -129,92 +128,80 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract text from an open document text file.
|
* Convert a file to PDF if necessary.
|
||||||
*
|
*
|
||||||
* @param inputStream Unencrypted input stream
|
* @param inputStream InputStream
|
||||||
* @return Content extracted
|
* @param file File
|
||||||
|
* @return PDF input stream
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private static String extractOpenDocumentText(InputStream inputStream) {
|
public static InputStream convertToPdf(InputStream inputStream, File file) throws Exception {
|
||||||
String content = null;
|
if (file.getMimeType().equals(MimeType.APPLICATION_PDF)) {
|
||||||
Path tempFile = null;
|
// It's already PDF, just return the input
|
||||||
try {
|
return inputStream;
|
||||||
// Convert the ODT file to a temporary PDF file
|
|
||||||
tempFile = Files.createTempFile("sismicsdocs_", ".pdf");
|
|
||||||
try (OutputStream out = Files.newOutputStream(tempFile)) {
|
|
||||||
OdfTextDocument document = OdfTextDocument.loadDocument(inputStream);
|
|
||||||
PdfOptions options = PdfOptions.create();
|
|
||||||
PdfConverter.getInstance().convert(document, out, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract content from the PDF file
|
|
||||||
try (InputStream pdfInputStream = Files.newInputStream(tempFile)) {
|
|
||||||
content = extractPdf(pdfInputStream);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Error while extracting text from the ODT", e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
Files.delete(tempFile); // Delete the temporary PDF file
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Should not happen
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return content;
|
|
||||||
|
if (file.getMimeType().equals(MimeType.OFFICE_DOCUMENT)) {
|
||||||
|
return convertOfficeDocument(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getMimeType().equals(MimeType.OPEN_DOCUMENT_TEXT)) {
|
||||||
|
return convertOpenDocumentText(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PDF conversion not necessary/possible
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract text from an Office document.
|
* Convert an open document text file to PDF.
|
||||||
*
|
*
|
||||||
* @param inputStream Unencrypted input stream
|
* @param inputStream Unencrypted input stream
|
||||||
* @return Content extracted
|
* @return PDF input stream
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private static String extractOfficeDocument(InputStream inputStream) {
|
private static InputStream convertOpenDocumentText(InputStream inputStream) throws Exception {
|
||||||
String content = null;
|
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
|
||||||
Path tempFile = null;
|
OdfTextDocument document = OdfTextDocument.loadDocument(inputStream);
|
||||||
try {
|
PdfOptions options = PdfOptions.create();
|
||||||
// Convert the DOCX file to a temporary PDF file
|
PdfConverter.getInstance().convert(document, pdfOutputStream, options);
|
||||||
tempFile = Files.createTempFile("sismicsdocs_", ".pdf");
|
inputStream.reset();
|
||||||
try (OutputStream out = Files.newOutputStream(tempFile)) {
|
return new ByteArrayInputStream(pdfOutputStream.toByteArray());
|
||||||
XWPFDocument document = new XWPFDocument(inputStream);
|
}
|
||||||
org.apache.poi.xwpf.converter.pdf.PdfOptions options = org.apache.poi.xwpf.converter.pdf.PdfOptions.create();
|
|
||||||
org.apache.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(document, out, options);
|
/**
|
||||||
}
|
* Convert an Office document to PDF.
|
||||||
|
*
|
||||||
// Extract content from the PDF file
|
* @param inputStream Unencrypted input stream
|
||||||
try (InputStream pdfInputStream = Files.newInputStream(tempFile)) {
|
* @return PDF input stream
|
||||||
content = extractPdf(pdfInputStream);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
private static InputStream convertOfficeDocument(InputStream inputStream) throws Exception {
|
||||||
} catch (Exception e) {
|
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
|
||||||
log.error("Error while extracting text from the DOCX", e);
|
XWPFDocument document = new XWPFDocument(inputStream);
|
||||||
} finally {
|
org.apache.poi.xwpf.converter.pdf.PdfOptions options = org.apache.poi.xwpf.converter.pdf.PdfOptions.create();
|
||||||
try {
|
org.apache.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(document, pdfOutputStream, options);
|
||||||
Files.delete(tempFile); // Delete the temporary PDF file
|
inputStream.reset();
|
||||||
} catch (IOException e) {
|
return new ByteArrayInputStream(pdfOutputStream.toByteArray());
|
||||||
// Should not happen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return content;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a file on the storage filesystem.
|
* Save a file on the storage filesystem.
|
||||||
*
|
*
|
||||||
* @param inputStream Unencrypted input stream
|
* @param inputStream Unencrypted input stream
|
||||||
|
* @param pdf
|
||||||
* @param file File to save
|
* @param file File to save
|
||||||
* @param privateKey Private key used for encryption
|
* @param privateKey Private key used for encryption
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static void save(InputStream inputStream, File file, String privateKey) throws Exception {
|
public static void save(InputStream inputStream, InputStream pdfInputStream, File file, String privateKey) throws Exception {
|
||||||
Cipher cipher = EncryptionUtil.getEncryptionCipher(privateKey);
|
Cipher cipher = EncryptionUtil.getEncryptionCipher(privateKey);
|
||||||
Path path = DirectoryUtil.getStorageDirectory().resolve(file.getId());
|
Path path = DirectoryUtil.getStorageDirectory().resolve(file.getId());
|
||||||
Files.copy(new CipherInputStream(inputStream, cipher), path);
|
Files.copy(new CipherInputStream(inputStream, cipher), path);
|
||||||
|
inputStream.reset();
|
||||||
|
|
||||||
// Generate file variations
|
// Generate file variations
|
||||||
inputStream.reset();
|
saveVariations(file, inputStream, pdfInputStream, cipher);
|
||||||
saveVariations(file, inputStream, cipher);
|
|
||||||
inputStream.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -222,25 +209,27 @@ public class FileUtil {
|
|||||||
*
|
*
|
||||||
* @param file File from database
|
* @param file File from database
|
||||||
* @param inputStream Unencrypted input stream
|
* @param inputStream Unencrypted input stream
|
||||||
|
* @param pdfInputStream Unencrypted PDF input stream
|
||||||
* @param cipher Cipher to use for encryption
|
* @param cipher Cipher to use for encryption
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static void saveVariations(File file, InputStream inputStream, Cipher cipher) throws Exception {
|
public static void saveVariations(File file, InputStream inputStream, InputStream pdfInputStream, Cipher cipher) throws Exception {
|
||||||
BufferedImage image = null;
|
BufferedImage image = null;
|
||||||
if (ImageUtil.isImage(file.getMimeType())) {
|
if (ImageUtil.isImage(file.getMimeType())) {
|
||||||
image = ImageIO.read(inputStream);
|
image = ImageIO.read(inputStream);
|
||||||
} else if(file.getMimeType().equals(MimeType.APPLICATION_PDF)) {
|
inputStream.reset();
|
||||||
|
} else if(pdfInputStream != null) {
|
||||||
// Generate preview from the first page of the PDF
|
// Generate preview from the first page of the PDF
|
||||||
PDDocument pdfDocument = null;
|
PDDocument pdfDocument = null;
|
||||||
try {
|
try {
|
||||||
pdfDocument = PDDocument.load(inputStream);
|
pdfDocument = PDDocument.load(pdfInputStream);
|
||||||
PDFRenderer renderer = new PDFRenderer(pdfDocument);
|
PDFRenderer renderer = new PDFRenderer(pdfDocument);
|
||||||
image = renderer.renderImage(0);
|
image = renderer.renderImage(0);
|
||||||
|
pdfInputStream.reset();
|
||||||
} finally {
|
} finally {
|
||||||
pdfDocument.close();
|
pdfDocument.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO Generate thumbnails for DOCX/ODT documents (guess the MIME type earlier and build a PDF version now?)
|
|
||||||
|
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
// Generate thumbnails from image
|
// Generate thumbnails from image
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package com.sismics.docs.core.util;
|
package com.sismics.docs.core.util;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.io.IOUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.google.common.io.Resources;
|
import com.google.common.io.Resources;
|
||||||
@ -18,19 +20,25 @@ import com.sismics.util.mime.MimeType;
|
|||||||
public class TestFileUtil {
|
public class TestFileUtil {
|
||||||
@Test
|
@Test
|
||||||
public void extractContentOpenDocumentTextTest() throws Exception {
|
public void extractContentOpenDocumentTextTest() throws Exception {
|
||||||
try (InputStream inputStream = Resources.getResource("file/document.odt").openStream()) {
|
try (InputStream inputStream = Resources.getResource("file/document.odt").openStream();
|
||||||
|
InputStream bytesInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream))) {
|
||||||
File file = new File();
|
File file = new File();
|
||||||
file.setMimeType(MimeType.OPEN_DOCUMENT_TEXT);
|
file.setMimeType(MimeType.OPEN_DOCUMENT_TEXT);
|
||||||
Assert.assertEquals("Lorem ipsum dolor sit amen.\r\n", FileUtil.extractContent(null, file, inputStream));
|
try (InputStream pdfInputStream = FileUtil.convertToPdf(bytesInputStream, file)) {
|
||||||
|
Assert.assertEquals("Lorem ipsum dolor sit amen.\r\n", FileUtil.extractContent(null, file, inputStream, pdfInputStream));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void extractContentOfficeDocumentTest() throws Exception {
|
public void extractContentOfficeDocumentTest() throws Exception {
|
||||||
try (InputStream inputStream = Resources.getResource("file/document.docx").openStream()) {
|
try (InputStream inputStream = Resources.getResource("file/document.docx").openStream();
|
||||||
|
InputStream bytesInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream))) {
|
||||||
File file = new File();
|
File file = new File();
|
||||||
file.setMimeType(MimeType.OFFICE_DOCUMENT);
|
file.setMimeType(MimeType.OFFICE_DOCUMENT);
|
||||||
Assert.assertEquals("Lorem ipsum dolor sit amen.\r\n", FileUtil.extractContent(null, file, inputStream));
|
try (InputStream pdfInputStream = FileUtil.convertToPdf(bytesInputStream, file)) {
|
||||||
|
Assert.assertEquals("Lorem ipsum dolor sit amen.\r\n", FileUtil.extractContent(null, file, inputStream, pdfInputStream));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,8 +146,14 @@ public class FileResource extends BaseResource {
|
|||||||
file.setUserId(principal.getId());
|
file.setUserId(principal.getId());
|
||||||
String fileId = fileDao.create(file);
|
String fileId = fileDao.create(file);
|
||||||
|
|
||||||
|
// Guess the mime type a second time, for open document format (first detected as simple ZIP file)
|
||||||
|
file.setMimeType(MimeTypeUtil.guessOpenDocumentFormat(file, fileInputStream));
|
||||||
|
|
||||||
|
// Convert to PDF if necessary (for thumbnail and text extraction)
|
||||||
|
InputStream pdfIntputStream = FileUtil.convertToPdf(fileInputStream, file);
|
||||||
|
|
||||||
// Save the file
|
// Save the file
|
||||||
FileUtil.save(fileInputStream, file, user.getPrivateKey());
|
FileUtil.save(fileInputStream, pdfIntputStream, file, user.getPrivateKey());
|
||||||
|
|
||||||
// Update the user quota
|
// Update the user quota
|
||||||
user.setStorageCurrent(user.getStorageCurrent() + fileData.length);
|
user.setStorageCurrent(user.getStorageCurrent() + fileData.length);
|
||||||
@ -159,6 +165,7 @@ public class FileResource extends BaseResource {
|
|||||||
fileCreatedAsyncEvent.setDocument(document);
|
fileCreatedAsyncEvent.setDocument(document);
|
||||||
fileCreatedAsyncEvent.setFile(file);
|
fileCreatedAsyncEvent.setFile(file);
|
||||||
fileCreatedAsyncEvent.setInputStream(fileInputStream);
|
fileCreatedAsyncEvent.setInputStream(fileInputStream);
|
||||||
|
fileCreatedAsyncEvent.setPdfInputStream(pdfIntputStream);
|
||||||
AppContext.getInstance().getAsyncEventBus().post(fileCreatedAsyncEvent);
|
AppContext.getInstance().getAsyncEventBus().post(fileCreatedAsyncEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,8 @@
|
|||||||
<label class="col-sm-2 control-label" for="inputFiles">New files</label>
|
<label class="col-sm-2 control-label" for="inputFiles">New files</label>
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-6">
|
||||||
<file class="form-control" id="inputFiles" multiple="multiple" ng-model="newFiles"
|
<file class="form-control" id="inputFiles" multiple="multiple" ng-model="newFiles"
|
||||||
accept="image/png,image/jpg,image/jpeg,image/gif,application/pdf" ng-disabled="fileIsUploading"></file>
|
accept="image/png,image/jpg,image/jpeg,image/gif,application/pdf,application/vnd.oasis.opendocument.text,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||||
|
ng-disabled="fileIsUploading"></file>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-4" ng-if="orphanFiles.length > 0">
|
<div class="col-sm-4" ng-if="orphanFiles.length > 0">
|
||||||
+ {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }}
|
+ {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }}
|
||||||
|
@ -267,6 +267,124 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
return json.getJsonArray("documents").size();
|
return json.getJsonArray("documents").size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test ODT extraction.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOdtExtraction() throws Exception {
|
||||||
|
// Login document_odt
|
||||||
|
clientUtil.createUser("document_odt");
|
||||||
|
String documentOdtToken = clientUtil.login("document_odt");
|
||||||
|
|
||||||
|
// Create a document
|
||||||
|
long create1Date = new Date().getTime();
|
||||||
|
JsonObject json = target().path("/document").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentOdtToken)
|
||||||
|
.put(Entity.form(new Form()
|
||||||
|
.param("title", "My super title document 1")
|
||||||
|
.param("description", "My super description for document 1")
|
||||||
|
.param("language", "eng")
|
||||||
|
.param("create_date", Long.toString(create1Date))), JsonObject.class);
|
||||||
|
String document1Id = json.getString("id");
|
||||||
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
|
// Add a PDF file
|
||||||
|
String file1Id = null;
|
||||||
|
try (InputStream is = Resources.getResource("file/document.odt").openStream()) {
|
||||||
|
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.odt");
|
||||||
|
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
||||||
|
json = target()
|
||||||
|
.register(MultiPartFeature.class)
|
||||||
|
.path("/file").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentOdtToken)
|
||||||
|
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
||||||
|
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
||||||
|
file1Id = json.getString("id");
|
||||||
|
Assert.assertNotNull(file1Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search documents by query in full content
|
||||||
|
json = target().path("/document/list")
|
||||||
|
.queryParam("search", "full:ipsum")
|
||||||
|
.request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentOdtToken)
|
||||||
|
.get(JsonObject.class);
|
||||||
|
Assert.assertTrue(json.getJsonArray("documents").size() == 1);
|
||||||
|
|
||||||
|
// Get the file thumbnail data
|
||||||
|
Response response = target().path("/file/" + file1Id + "/data")
|
||||||
|
.queryParam("size", "thumb")
|
||||||
|
.request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentOdtToken)
|
||||||
|
.get();
|
||||||
|
InputStream is = (InputStream) response.getEntity();
|
||||||
|
byte[] fileBytes = ByteStreams.toByteArray(is);
|
||||||
|
Assert.assertTrue(fileBytes.length > 0); // Images rendered from PDF differ in size from OS to OS due to font issues
|
||||||
|
Assert.assertEquals(MimeType.IMAGE_JPEG, MimeTypeUtil.guessMimeType(fileBytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test DOCX extraction.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDocxExtraction() throws Exception {
|
||||||
|
// Login document_docx
|
||||||
|
clientUtil.createUser("document_docx");
|
||||||
|
String documentDocxToken = clientUtil.login("document_docx");
|
||||||
|
|
||||||
|
// Create a document
|
||||||
|
long create1Date = new Date().getTime();
|
||||||
|
JsonObject json = target().path("/document").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentDocxToken)
|
||||||
|
.put(Entity.form(new Form()
|
||||||
|
.param("title", "My super title document 1")
|
||||||
|
.param("description", "My super description for document 1")
|
||||||
|
.param("language", "eng")
|
||||||
|
.param("create_date", Long.toString(create1Date))), JsonObject.class);
|
||||||
|
String document1Id = json.getString("id");
|
||||||
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
|
// Add a PDF file
|
||||||
|
String file1Id = null;
|
||||||
|
try (InputStream is = Resources.getResource("file/document.docx").openStream()) {
|
||||||
|
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.docx");
|
||||||
|
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
||||||
|
json = target()
|
||||||
|
.register(MultiPartFeature.class)
|
||||||
|
.path("/file").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentDocxToken)
|
||||||
|
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
||||||
|
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
||||||
|
file1Id = json.getString("id");
|
||||||
|
Assert.assertNotNull(file1Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search documents by query in full content
|
||||||
|
json = target().path("/document/list")
|
||||||
|
.queryParam("search", "full:dolor")
|
||||||
|
.request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentDocxToken)
|
||||||
|
.get(JsonObject.class);
|
||||||
|
Assert.assertTrue(json.getJsonArray("documents").size() == 1);
|
||||||
|
|
||||||
|
// Get the file thumbnail data
|
||||||
|
Response response = target().path("/file/" + file1Id + "/data")
|
||||||
|
.queryParam("size", "thumb")
|
||||||
|
.request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentDocxToken)
|
||||||
|
.get();
|
||||||
|
InputStream is = (InputStream) response.getEntity();
|
||||||
|
byte[] fileBytes = ByteStreams.toByteArray(is);
|
||||||
|
Assert.assertTrue(fileBytes.length > 0); // Images rendered from PDF differ in size from OS to OS due to font issues
|
||||||
|
Assert.assertEquals(MimeType.IMAGE_JPEG, MimeTypeUtil.guessMimeType(fileBytes));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test PDF extraction.
|
* Test PDF extraction.
|
||||||
*
|
*
|
||||||
@ -274,14 +392,14 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testPdfExtraction() throws Exception {
|
public void testPdfExtraction() throws Exception {
|
||||||
// Login document2
|
// Login document_pdf
|
||||||
clientUtil.createUser("document2");
|
clientUtil.createUser("document_pdf");
|
||||||
String document2Token = clientUtil.login("document2");
|
String documentPdfToken = clientUtil.login("document_pdf");
|
||||||
|
|
||||||
// Create a document
|
// Create a document
|
||||||
long create1Date = new Date().getTime();
|
long create1Date = new Date().getTime();
|
||||||
JsonObject json = target().path("/document").request()
|
JsonObject json = target().path("/document").request()
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document2Token)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPdfToken)
|
||||||
.put(Entity.form(new Form()
|
.put(Entity.form(new Form()
|
||||||
.param("title", "My super title document 1")
|
.param("title", "My super title document 1")
|
||||||
.param("description", "My super description for document 1")
|
.param("description", "My super description for document 1")
|
||||||
@ -298,7 +416,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
json = target()
|
json = target()
|
||||||
.register(MultiPartFeature.class)
|
.register(MultiPartFeature.class)
|
||||||
.path("/file").request()
|
.path("/file").request()
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document2Token)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPdfToken)
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
||||||
file1Id = json.getString("id");
|
file1Id = json.getString("id");
|
||||||
@ -310,7 +428,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
.queryParam("search", "full:vrandecic")
|
.queryParam("search", "full:vrandecic")
|
||||||
.request()
|
.request()
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document2Token)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPdfToken)
|
||||||
.get(JsonObject.class);
|
.get(JsonObject.class);
|
||||||
Assert.assertTrue(json.getJsonArray("documents").size() == 1);
|
Assert.assertTrue(json.getJsonArray("documents").size() == 1);
|
||||||
|
|
||||||
@ -318,7 +436,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Response response = target().path("/file/" + file1Id + "/data")
|
Response response = target().path("/file/" + file1Id + "/data")
|
||||||
.queryParam("size", "thumb")
|
.queryParam("size", "thumb")
|
||||||
.request()
|
.request()
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document2Token)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPdfToken)
|
||||||
.get();
|
.get();
|
||||||
InputStream is = (InputStream) response.getEntity();
|
InputStream is = (InputStream) response.getEntity();
|
||||||
byte[] fileBytes = ByteStreams.toByteArray(is);
|
byte[] fileBytes = ByteStreams.toByteArray(is);
|
||||||
|
BIN
docs-web/src/test/resources/file/document.docx
Normal file
BIN
docs-web/src/test/resources/file/document.docx
Normal file
Binary file not shown.
BIN
docs-web/src/test/resources/file/document.odt
Normal file
BIN
docs-web/src/test/resources/file/document.odt
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user