• SOLVED Download Document with Revision in Filename

  • We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #31401  by alexwgordon
 
Hi jllort,

Yeah that system check is why I was hoping to have 2 different methods. Turning off the system name check is nice for now, but I think for safety, it would be nice to not be able to upload a different file name and just when "editing" a file, download without the revision name.

Is there a way to have "edit" and "download" use two different Servlets potentially?

Thanks again jllort :)
 #31467  by jllort
 
All is concentrated in ToolBar.java if you follow code from there you'll arrive to one call Util.downloadFileByUUID(getDocument().getUuid(), (checkout ? "checkout" : ""));

And this is the method involved on it, really after edit, you're doing a normal download at Util.java calling servlet defined into var RPCService.DownloadServlet -> public static String DownloadServlet = Main.CONTEXT + "/frontend/Download";.
Code: Select all
/**
 * Download file by UUID
 */
public static void downloadFileByUUID(String uuid, String params) {
	if (!params.equals("") && !params.endsWith("&")) {
		params += "&";
	}
	
	final Element downloadIframe = RootPanel.get("__download").getElement();
	String url = RPCService.DownloadServlet + "?" + params + "uuid=" + URL.encodeQueryString(uuid);
	DOM.setElementAttribute(downloadIframe, "src", url);
}
You could create other method for downloading from edit, or pass extra var indicating downloading is for edit purpose ( probably the most easy ). My suggestion is follow from Toolbar.java the method called executeCheckout().
 #31506  by alexwgordon
 
Hi Jllort,

Okay so I've been trying to parse through all this and i'm at a standstill. I can't figure out how the downloadFileByUUID calls the DownloadServlet. I think once I figure that out I can adjust the downloadservlet by passing the params TRUE or FALSE for checkout. Thanks for your help in advance :)
 #31534  by jllort
 
With eclipse or other java ide you must have and option called "referenced" select the method, right click -> references -> workspace and you'll get who's calling the method etc... and you can follow back. Or if you want to follow from beggining -> Toolbar.java the executeDownload() normal download or executeCheckout() for checkout download.
 #31555  by alexwgordon
 
Thanks jllort!

Finally was able to solve it! Took me some digging and learning, but ended up figuring it out! All I did was add the code below to my DownloadServlet.java and all done :) Hope this helps someone else at some point!
Code: Select all
String fileName = PathUtils.getName(doc.getPath());
String versionToAppend = OKMDocument.getInstance().getProperties(null,uuid).getActualVersion().getName();
String[] nameParts = fileName.split("\\.(?=[^\\.]+$)");
String newFileName = nameParts[0] + " rev " + versionToAppend + "." + nameParts[1];
if (checkout == null)
	WebUtils.sendFile(request, response, newFileName, doc.getMimeType(), inline, is);
else 
	WebUtils.sendFile(request, response, fileName, doc.getMimeType(), inline, is);
 #40707  by mountain73
 
Hi.

Thanks for this solution, it helped us a lot:
We used your solution for revision in filename and improved it a little: Revision filename is added in filename as in your case, but we also add it when downloading document version from history, and also when converting document to pdf.
You will find below the patch.
I think this is worth integrating in community version, what is your opinion about that?

Regards

Mountain73
Code: Select all
diff --git a/src/main/java/com/openkm/servlet/frontend/ConverterServlet.java b/src/main/java/com/openkm/servlet/frontend/ConverterServlet.java
index c4ba980..8b2e9cd 100644
--- a/src/main/java/com/openkm/servlet/frontend/ConverterServlet.java
+++ b/src/main/java/com/openkm/servlet/frontend/ConverterServlet.java
@@ -95,11 +95,12 @@ public class ConverterServlet extends OKMHttpServlet {
 				
 				String path = OKMRepository.getInstance().getNodePath(null, uuid);
 				Document doc = OKMDocument.getInstance().getProperties(null, path);
+				final String versionToAppend = OKMDocument.getInstance().getProperties(null,uuid).getActualVersion().getName();
 				String fileName = PathUtils.getName(doc.getPath());
-				
+				String[] nameParts = fileName.split("\\.(?=[^\\.]+$)");
+				String newFileName = nameParts[0] + " rev " + versionToAppend + "." + nameParts[1];
 				// Save content to temporary file
-				tmp = File.createTempFile("okm", "." + FileUtils.getFileExtension(fileName));
-				
+				tmp = new File(System.getProperty("java.io.tmpdir"),newFileName);
 				if (Config.REPOSITORY_NATIVE) {
 					// If is used to preview, it should workaround the DOWNLOAD extended permission.
 					is = new DbDocumentModule().getContent(null, path, false, !(toSwf));
@@ -124,7 +125,7 @@ public class ConverterServlet extends OKMHttpServlet {
 				// Prepare conversion
 				ConversionData cd = new ConversionData();
 				cd.uuid = uuid;
-				cd.fileName = fileName;
+				cd.fileName = newFileName;
 				cd.mimeType = doc.getMimeType();
 				cd.file = tmp;
 				
diff --git a/src/main/java/com/openkm/servlet/frontend/DownloadServlet.java b/src/main/java/com/openkm/servlet/frontend/DownloadServlet.java
index 309481a..ec4aa96 100644
--- a/src/main/java/com/openkm/servlet/frontend/DownloadServlet.java
+++ b/src/main/java/com/openkm/servlet/frontend/DownloadServlet.java
@@ -150,17 +150,21 @@ public class DownloadServlet extends OKMHttpServlet {
 				if (OKMDocument.getInstance().isValid(null, path)) {
 					// Get document
 					Document doc = OKMDocument.getInstance().getProperties(null, path);
-					
+					String versionToAppend = "";
 					if (ver != null && !ver.equals("")) {
 						is = OKMDocument.getInstance().getContentByVersion(null, path, ver);
+						versionToAppend = " rev " + ver;
 					} else {
 						is = OKMDocument.getInstance().getContent(null, path, checkout != null);
-					}
-					
+						if (checkout == null) {
+							versionToAppend = " rev " + OKMDocument.getInstance().getProperties(null,uuid).getActualVersion().getName();
+						}
+                    }
 					// Send document
 					String fileName = PathUtils.getName(doc.getPath());
-					WebUtils.sendFile(request, response, fileName, doc.getMimeType(), inline, is);
-				} else if (OKMMail.getInstance().isValid(null, path)) {
+					String[] nameParts = fileName.split("\\.(?=[^\\.]+$)");
+					String newFileName = nameParts[0] + versionToAppend + "." + nameParts[1];
+					WebUtils.sendFile(request, response, newFileName, doc.getMimeType(), inline, is);				} else if (OKMMail.getInstance().isValid(null, path)) {
 					// Get mail
 					Mail mail = OKMMail.getInstance().getProperties(null, path);
 #40720  by pavila
 
I've revised the patch but I don't know if this modification is needed by all users. Perhaps, adding a boolean configuration property which can be used to choose if OpenKM should make this change is a better approach.
 #40724  by mountain73
 
Hi pavila.

According to me, this modification could be integrated for the following two reasons:

1- I doubt that this modification will not interest all users: when user download a document from openkm, he does not have any way to know which revision it was, unless he renames it or have implemented some code to write it within the file (quite tricky, and file format dependant...).

2- This modification also has no side effect as it does not affect the commit of new revisions: when performing "edit", original file name is kept so that it can be modified and updated to openkm server without any problem.

Still, if you do not agree, a boolean parameter sound also fine to me.

Thanks for your support.

Regards

Mountain73
 #40811  by mountain73
 
Hi all.

@pavila: what is finally your decision about potential patch integration?

@alexgordon: Yes, I understood and agreed ;-) By the way, did you see we haave added also the case of file download of previous version stored in history?

Thanks all of yoo.
Regards.
mountain73

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.