mirror of
https://github.com/sismics/docs.git
synced 2025-12-15 02:36:24 +00:00
#177: import document from EML file (api done)
This commit is contained in:
@@ -13,6 +13,13 @@ import com.sismics.docs.core.model.jpa.Document;
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DocumentUtil {
|
||||
/**
|
||||
* Create a document and add the base ACLs.
|
||||
*
|
||||
* @param document Document
|
||||
* @param userId User creating the document
|
||||
* @return Created document
|
||||
*/
|
||||
public static Document createDocument(Document document, String userId) {
|
||||
DocumentDao documentDao = new DocumentDao();
|
||||
String documentId = documentDao.create(document, userId);
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
package com.sismics.docs.core.util;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.sismics.docs.core.constant.Constants;
|
||||
import com.sismics.docs.core.dao.jpa.FileDao;
|
||||
import com.sismics.docs.core.dao.jpa.UserDao;
|
||||
import com.sismics.docs.core.event.DocumentUpdatedAsyncEvent;
|
||||
import com.sismics.docs.core.event.FileCreatedAsyncEvent;
|
||||
import com.sismics.docs.core.model.jpa.File;
|
||||
import com.sismics.docs.core.model.jpa.User;
|
||||
import com.sismics.tess4j.Tesseract;
|
||||
import com.sismics.util.ImageDeskew;
|
||||
import com.sismics.util.ImageUtil;
|
||||
import com.sismics.util.Scalr;
|
||||
import com.sismics.util.context.ThreadLocalContext;
|
||||
import com.sismics.util.mime.MimeTypeUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -167,4 +176,92 @@ public class FileUtil {
|
||||
Files.delete(thumbnailFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new file.
|
||||
*
|
||||
* @param name File name, can be null
|
||||
* @param unencryptedFile Path to the unencrypted file
|
||||
* @param fileSize File size
|
||||
* @param language File language, can be null if associated to no document
|
||||
* @param userId User ID creating the file
|
||||
* @param documentId Associated document ID or null if no document
|
||||
* @return File ID
|
||||
* @throws Exception e
|
||||
*/
|
||||
public static String createFile(String name, Path unencryptedFile, long fileSize, String language, String userId, String documentId) throws Exception {
|
||||
// Validate mime type
|
||||
String mimeType;
|
||||
try {
|
||||
mimeType = MimeTypeUtil.guessMimeType(unencryptedFile, name);
|
||||
} catch (IOException e) {
|
||||
throw new IOException("ErrorGuessMime", e);
|
||||
}
|
||||
|
||||
// Validate user quota
|
||||
UserDao userDao = new UserDao();
|
||||
User user = userDao.getById(userId);
|
||||
if (user.getStorageCurrent() + fileSize > user.getStorageQuota()) {
|
||||
throw new IOException("QuotaReached");
|
||||
}
|
||||
|
||||
// Validate global quota
|
||||
String globalStorageQuotaStr = System.getenv(Constants.GLOBAL_QUOTA_ENV);
|
||||
if (!Strings.isNullOrEmpty(globalStorageQuotaStr)) {
|
||||
long globalStorageQuota = Long.valueOf(globalStorageQuotaStr);
|
||||
long globalStorageCurrent = userDao.getGlobalStorageCurrent();
|
||||
if (globalStorageCurrent + fileSize > globalStorageQuota) {
|
||||
throw new IOException("QuotaReached");
|
||||
}
|
||||
}
|
||||
|
||||
// Get files of this document
|
||||
FileDao fileDao = new FileDao();
|
||||
int order = 0;
|
||||
if (documentId != null) {
|
||||
for (File file : fileDao.getByDocumentId(userId, documentId)) {
|
||||
file.setOrder(order++);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the file
|
||||
File file = new File();
|
||||
file.setOrder(order);
|
||||
file.setDocumentId(documentId);
|
||||
file.setName(name);
|
||||
file.setMimeType(mimeType);
|
||||
file.setUserId(userId);
|
||||
String fileId = fileDao.create(file, userId);
|
||||
|
||||
// Guess the mime type a second time, for open document format (first detected as simple ZIP file)
|
||||
file.setMimeType(MimeTypeUtil.guessOpenDocumentFormat(file, unencryptedFile));
|
||||
|
||||
// Convert to PDF if necessary (for thumbnail and text extraction)
|
||||
java.nio.file.Path unencryptedPdfFile = PdfUtil.convertToPdf(file, unencryptedFile);
|
||||
|
||||
// Save the file
|
||||
FileUtil.save(unencryptedFile, unencryptedPdfFile, file, user.getPrivateKey());
|
||||
|
||||
// Update the user quota
|
||||
user.setStorageCurrent(user.getStorageCurrent() + fileSize);
|
||||
userDao.updateQuota(user);
|
||||
|
||||
// Raise a new file created event and document updated event if we have a document
|
||||
if (documentId != null) {
|
||||
FileCreatedAsyncEvent fileCreatedAsyncEvent = new FileCreatedAsyncEvent();
|
||||
fileCreatedAsyncEvent.setUserId(userId);
|
||||
fileCreatedAsyncEvent.setLanguage(language);
|
||||
fileCreatedAsyncEvent.setFile(file);
|
||||
fileCreatedAsyncEvent.setUnencryptedFile(unencryptedFile);
|
||||
fileCreatedAsyncEvent.setUnencryptedPdfFile(unencryptedPdfFile);
|
||||
ThreadLocalContext.get().addAsyncEvent(fileCreatedAsyncEvent);
|
||||
|
||||
DocumentUpdatedAsyncEvent documentUpdatedAsyncEvent = new DocumentUpdatedAsyncEvent();
|
||||
documentUpdatedAsyncEvent.setUserId(userId);
|
||||
documentUpdatedAsyncEvent.setDocumentId(documentId);
|
||||
ThreadLocalContext.get().addAsyncEvent(documentUpdatedAsyncEvent);
|
||||
}
|
||||
|
||||
return fileId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class EmailUtil {
|
||||
* @param subject Email subject
|
||||
* @param paramMap Email parameters
|
||||
*/
|
||||
public static void sendEmail(String templateName, UserDto recipientUser, String subject, Map<String, Object> paramMap) {
|
||||
private static void sendEmail(String templateName, UserDto recipientUser, String subject, Map<String, Object> paramMap) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("Sending email from template=" + templateName + " to user " + recipientUser);
|
||||
}
|
||||
@@ -175,6 +175,15 @@ public class EmailUtil {
|
||||
sendEmail(templateName, recipientUser, subject, paramMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an email content to be imported.
|
||||
*
|
||||
* @param part Email part
|
||||
* @param mailContent Mail content modified by side-effect
|
||||
*
|
||||
* @throws MessagingException e
|
||||
* @throws IOException e
|
||||
*/
|
||||
public static void parseMailContent(Part part, MailContent mailContent) throws MessagingException, IOException {
|
||||
Object content = part.getContent();
|
||||
if (content instanceof Multipart) {
|
||||
@@ -261,5 +270,17 @@ public class EmailUtil {
|
||||
private String name;
|
||||
private Path file;
|
||||
private long size;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Path getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user