Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 0dfed72

Browse files
author
Pavel-Teplitsky
committed
Added rewrite option
1 parent 5b1d498 commit 0dfed72

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

configuration.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ resources:
5353
# Enable/disable documents browse
5454
browse: true
5555
# Enable/disable HTMl rendering mode, false for image mode
56-
htmlMode: true
56+
htmlMode: true
57+
# Enable/disable rewrite for uploaded file if file with the same name already exists
58+
rewrite: true

src/main/java/com/aliensoft/quickview/config/QuickViewConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public class Resources{
140140
@JsonProperty
141141
private boolean htmlMode;
142142

143+
@Valid
144+
@JsonProperty
145+
private boolean rewrite;
146+
143147
public Boolean getRunAsService() {
144148
return runAsService;
145149
}
@@ -251,6 +255,14 @@ public boolean isHtmlMode() {
251255
public void setHtmlMode(boolean htmlMode) {
252256
this.htmlMode = htmlMode;
253257
}
258+
259+
public boolean isRewrite() {
260+
return rewrite;
261+
}
262+
263+
public void setRewrite(boolean rewrite) {
264+
this.rewrite = rewrite;
265+
}
254266
}
255267

256268
/**

src/main/java/com/aliensoft/quickview/resources/QuickViewResource.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
379379
Part filePart = request.getPart("file");
380380
// get document URL
381381
String documentUrl = request.getParameter("url");
382+
// get rewrite mode
383+
boolean rewrite = Boolean.parseBoolean(request.getParameter("rewrite"));
382384
InputStream uploadedInputStream = null;
383385
String fileName = "";
384386
if(documentUrl.isEmpty() || documentUrl == null) {
@@ -395,7 +397,19 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
395397
String documentStoragePath = quickViewConfig.getApplication().getFilesDirectory();
396398
// save the file
397399
File file = new File(documentStoragePath + "/" + fileName);
398-
Files.copy(uploadedInputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
400+
// check rewrite mode
401+
if(rewrite) {
402+
// save file with rewrite if exists
403+
Files.copy(uploadedInputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
404+
} else {
405+
if (file.exists())
406+
{
407+
// get file with new name
408+
file = getFreeFileName(documentStoragePath, fileName);
409+
}
410+
// save file with out rewriting
411+
Files.copy(uploadedInputStream, file.toPath());
412+
}
399413
UploadedDocumentWrapper uploadedDocument = new UploadedDocumentWrapper();
400414
uploadedDocument.setGuid(documentStoragePath + "/" + fileName);
401415
return objectToJson(uploadedDocument);

src/main/java/com/aliensoft/quickview/resources/QuickViewResourcesBase.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import org.apache.commons.io.FilenameUtils;
67
import org.json.JSONException;
78
import org.json.JSONObject;
89

@@ -12,6 +13,7 @@
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.io.InputStreamReader;
16+
import java.io.File;
1517
import java.util.logging.Level;
1618
import java.util.logging.Logger;
1719

@@ -113,4 +115,25 @@ protected boolean getJsonBoolean(String json, String key){
113115
return value;
114116
}
115117

118+
protected File getFreeFileName(String directory, String fileName){
119+
File file = null;
120+
try {
121+
File folder = new File(directory);
122+
File[] listOfFiles = folder.listFiles();
123+
for (int i = 0; i < listOfFiles.length; i++) {
124+
int number = i + 1;
125+
String newFileName = FilenameUtils.removeExtension(fileName) + "-Copy(" + number + ")." + FilenameUtils.getExtension(fileName);
126+
file = new File(directory + "/" + newFileName);
127+
if(file.exists()) {
128+
continue;
129+
} else {
130+
break;
131+
}
132+
}
133+
} catch (Exception e) {
134+
e.printStackTrace();
135+
}
136+
return file;
137+
}
138+
116139
}

src/main/resources/com/aliensoft/quickview/views/quickview.ftl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<link type="text/css" rel="stylesheet" href="assets/css/quickview.css"/>
88
<link type="text/css" rel="stylesheet" href="assets/css/quickview.mobile.css"/>
99
<link type="text/css" rel="stylesheet" href="assets/css/quickview-dark.css"/>
10-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
10+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1111
<script type="text/javascript" src="assets/js/quickview.js"></script>
1212
</head>
1313
<body>
@@ -26,7 +26,8 @@
2626
print: ${config.resources.print?c},
2727
defaultDocument: '${config.resources.defaultDocument}',
2828
browse: ${config.resources.browse?c},
29-
htmlMode: ${config.resources.htmlMode?c}
29+
htmlMode: ${config.resources.htmlMode?c},
30+
rewrite: ${config.resources.rewrite?c}
3031
});
3132
</script>
3233
</body>

0 commit comments

Comments
 (0)