Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ resources:
# Enable/disable documents browse
browse: true
# Enable/disable HTMl rendering mode, false for image mode
htmlMode: true
htmlMode: true
# Enable/disable rewrite for uploaded file if file with the same name already exists
rewrite: true
12 changes: 12 additions & 0 deletions src/main/java/com/aliensoft/quickview/config/QuickViewConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public class Resources{
@JsonProperty
private boolean htmlMode;

@Valid
@JsonProperty
private boolean rewrite;

public Boolean getRunAsService() {
return runAsService;
}
Expand Down Expand Up @@ -251,6 +255,14 @@ public boolean isHtmlMode() {
public void setHtmlMode(boolean htmlMode) {
this.htmlMode = htmlMode;
}

public boolean isRewrite() {
return rewrite;
}

public void setRewrite(boolean rewrite) {
this.rewrite = rewrite;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
* @author Alex Bobkov
*/
public class ErrorMsgWrapper {
private String error;
private String message;
private Exception exception;

public String getError() {
return error;
public String getMessage() {
return message;
}

public void setError(String error) {
this.error = error;
public void setMessage(String message) {
this.message = message;
}

public Exception getException() {
return exception;
}

public void setException(Exception exception) {
this.exception = exception;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import com.groupdocs.viewer.domain.options.DocumentInfoOptions;
import com.groupdocs.viewer.domain.options.FileListOptions;
import com.groupdocs.viewer.domain.options.RotatePageOptions;
import com.groupdocs.viewer.exception.GroupDocsViewerException;
import com.groupdocs.viewer.exception.InvalidPasswordException;
import com.groupdocs.viewer.handler.ViewerHtmlHandler;
import com.groupdocs.viewer.handler.ViewerImageHandler;
import com.groupdocs.viewer.licensing.License;
import com.groupdocs.viewer.localization.ILocalizationHandler;
import io.dropwizard.jetty.ConnectorFactory;
import io.dropwizard.jetty.HttpConnectorFactory;
import io.dropwizard.server.SimpleServerFactory;
Expand Down Expand Up @@ -150,7 +153,8 @@ public Object loadFileTree(@Context HttpServletRequest request, @Context HttpSer
}catch (Exception ex){
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setError(ex.getMessage());
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}
}
Expand All @@ -166,31 +170,50 @@ public Object loadFileTree(@Context HttpServletRequest request, @Context HttpSer
public Object loadDocumentDescription(@Context HttpServletRequest request, @Context HttpServletResponse response){
// set response content type
setResponseContentType(response, MediaType.APPLICATION_JSON);
String password = "";
try {
// get request body
String requestBody = getRequestBody(request);
// get/set parameters
String documentGuid = getJsonString(requestBody, "guid");
boolean htmlMode = getJsonBoolean(requestBody, "htmlMode");
password = getJsonString(requestBody, "password");
// check if documentGuid contains path or only file name
if(!Paths.get(documentGuid).isAbsolute()){
documentGuid = quickViewConfig.getApplication().getFilesDirectory() + "/" + documentGuid;
}
DocumentInfoContainer documentInfoContainer = new DocumentInfoContainer();
// get document info options
DocumentInfoOptions documentInfoOptions = new DocumentInfoOptions(documentGuid);
// set password for protected document
if(!password.isEmpty() && password != null) {
documentInfoOptions.setPassword(password);
}
// get document info container
if(htmlMode) {
if (htmlMode) {
documentInfoContainer = viewerHtmlHandler.getDocumentInfo(documentGuid, documentInfoOptions);
} else {
documentInfoContainer = viewerImageHandler.getDocumentInfo(documentGuid, documentInfoOptions);
}
// return document description
return objectToJson(documentInfoContainer.getPages());
}catch (GroupDocsViewerException ex){
// Set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
if(GroupDocsViewerException.class.isAssignableFrom(InvalidPasswordException.class) && password.isEmpty()) {
errorMsgWrapper.setMessage("Password Required");
}else if(GroupDocsViewerException.class.isAssignableFrom(InvalidPasswordException.class) && !password.isEmpty()){
errorMsgWrapper.setMessage("Incorrect password");
}else{
errorMsgWrapper.setMessage(ex.getMessage());
}
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}catch (Exception ex){
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setError(ex.getMessage());
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}
}
Expand All @@ -213,6 +236,7 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
String documentGuid = getJsonString(requestBody, "guid");
int pageNumber = getJsonInteger(requestBody, "page");
boolean htmlMode = getJsonBoolean(requestBody, "htmlMode");
String password = getJsonString(requestBody, "password");
LoadedPageWrapper loadedPage = new LoadedPageWrapper();
String angle = "0";
// set options
Expand All @@ -221,6 +245,10 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
htmlOptions.setPageNumber(pageNumber);
htmlOptions.setCountPagesToRender(1);
htmlOptions.setResourcesEmbedded(true);
// set password for protected document
if(!password.isEmpty() && password != null) {
htmlOptions.setPassword(password);
}
// get page HTML
loadedPage.setPageHtml(viewerHtmlHandler.getPages(documentGuid, htmlOptions).get(0).getHtmlContent());
// get page rotation angle
Expand All @@ -229,6 +257,10 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
ImageOptions imageOptions = new ImageOptions();
imageOptions.setPageNumber(pageNumber);
imageOptions.setCountPagesToRender(1);
// set password for protected document
if(!password.isEmpty()) {
imageOptions.setPassword(password);
}
// get page image
byte[] bytes = IOUtils.toByteArray(viewerImageHandler.getPages(documentGuid, imageOptions).get(0).getStream());
// encode ByteArray into String
Expand All @@ -245,7 +277,8 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
setResponseContentType(response, MediaType.APPLICATION_JSON);
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setError(ex.getMessage());
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}
}
Expand All @@ -269,6 +302,7 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
int angle = Integer.parseInt(getJsonString(requestBody, "angle"));
JSONArray pages = new JSONObject(requestBody).getJSONArray("pages");
boolean htmlMode = getJsonBoolean(requestBody, "htmlMode");
String password = getJsonString(requestBody, "password");
// a list of the rotated pages info
ArrayList<RotatedPageWrapper> rotatedPages = new ArrayList<RotatedPageWrapper>();
// rotate pages
Expand All @@ -279,6 +313,10 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
RotatePageOptions rotateOptions = new RotatePageOptions(pageNumber, angle);
// perform page rotation
String resultAngle = "0";
// set password for protected document
if(!password.isEmpty() && password != null) {
rotateOptions.setPassword(password);
}
if(htmlMode) {
viewerHtmlHandler.rotatePage(documentGuid, rotateOptions);
resultAngle = String.valueOf(viewerHtmlHandler.getDocumentInfo(documentGuid).getPages().get(pageNumber - 1).getAngle());
Expand All @@ -299,7 +337,8 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
setResponseContentType(response, MediaType.APPLICATION_JSON);
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setError(ex.getMessage());
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}
}
Expand All @@ -311,7 +350,7 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
*/
@GET
@Path(value = "/downloadDocument")
public void downloadDocument(@Context HttpServletRequest request, @Context HttpServletResponse response) throws ServletException, IOException {
public Object downloadDocument(@Context HttpServletRequest request, @Context HttpServletResponse response) throws ServletException, IOException {
int bytesRead = 0;
int count = 0;
byte[] buff = new byte[16 * 1024];
Expand All @@ -329,9 +368,18 @@ public void downloadDocument(@Context HttpServletRequest request, @Context HttpS
// download the document
inputStream = new BufferedInputStream(new FileInputStream(documentGuid));
outStream = new BufferedOutputStream(out);
while((count = inputStream.read(buff)) != -1) {
while ((count = inputStream.read(buff)) != -1) {
outStream.write(buff, 0, count);
}
return outStream;
} catch (Exception ex){
// set response content type
setResponseContentType(response, MediaType.APPLICATION_JSON);
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
} finally {
// close streams
if (inputStream != null)
Expand Down Expand Up @@ -360,6 +408,8 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
Part filePart = request.getPart("file");
// get document URL
String documentUrl = request.getParameter("url");
// get rewrite mode
boolean rewrite = Boolean.parseBoolean(request.getParameter("rewrite"));
InputStream uploadedInputStream = null;
String fileName = "";
if(documentUrl.isEmpty() || documentUrl == null) {
Expand All @@ -376,7 +426,19 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
String documentStoragePath = quickViewConfig.getApplication().getFilesDirectory();
// save the file
File file = new File(documentStoragePath + "/" + fileName);
Files.copy(uploadedInputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
// check rewrite mode
if(rewrite) {
// save file with rewrite if exists
Files.copy(uploadedInputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
if (file.exists())
{
// get file with new name
file = getFreeFileName(documentStoragePath, fileName);
}
// save file with out rewriting
Files.copy(uploadedInputStream, file.toPath());
}
UploadedDocumentWrapper uploadedDocument = new UploadedDocumentWrapper();
uploadedDocument.setGuid(documentStoragePath + "/" + fileName);
return objectToJson(uploadedDocument);
Expand All @@ -385,7 +447,8 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
setResponseContentType(response, MediaType.APPLICATION_JSON);
// set exception message
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper();
errorMsgWrapper.setError(ex.getMessage());
errorMsgWrapper.setMessage(ex.getMessage());
errorMsgWrapper.setException(ex);
return objectToJson(errorMsgWrapper);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gson.Gson;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -12,6 +14,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -33,10 +36,9 @@ protected void setResponseContentType(HttpServletResponse response, String conte

protected String objectToJson(Object object){
try {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String auxJson = ow.writeValueAsString(object);
String auxJson = new Gson().toJson(object);
return auxJson;
} catch (JsonProcessingException ex) {
} catch (Exception ex) {
Logger.getLogger(QuickViewResourcesBase.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
Expand Down Expand Up @@ -113,4 +115,25 @@ protected boolean getJsonBoolean(String json, String key){
return value;
}

protected File getFreeFileName(String directory, String fileName){
File file = null;
try {
File folder = new File(directory);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
int number = i + 1;
String newFileName = FilenameUtils.removeExtension(fileName) + "-Copy(" + number + ")." + FilenameUtils.getExtension(fileName);
file = new File(directory + "/" + newFileName);
if(file.exists()) {
continue;
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<link type="text/css" rel="stylesheet" href="assets/css/quickview.css"/>
<link type="text/css" rel="stylesheet" href="assets/css/quickview.mobile.css"/>
<link type="text/css" rel="stylesheet" href="assets/css/quickview-dark.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.2/css/swiper.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.2/js/swiper.min.js"></script>
<script type="text/javascript" src="assets/js/quickview.js"></script>
</head>
<body>
Expand All @@ -26,7 +28,8 @@
print: ${config.resources.print?c},
defaultDocument: '${config.resources.defaultDocument}',
browse: ${config.resources.browse?c},
htmlMode: ${config.resources.htmlMode?c}
htmlMode: ${config.resources.htmlMode?c},
rewrite: ${config.resources.rewrite?c}
});
</script>
</body>
Expand Down