- Add "Save file as" menu that forces browser to download file instead of opening...
authorDimitris Routsis <droutsis@ebs.gr>
Fri, 12 Jun 2009 15:17:13 +0000 (18:17 +0300)
committerDimitris Routsis <droutsis@ebs.gr>
Fri, 12 Jun 2009 15:17:13 +0000 (18:17 +0300)
- Fix broken international characters in browser Save As dialog.

src/gr/ebs/gss/client/FileContextMenu.java
src/gr/ebs/gss/client/FileMenu.java
src/gr/ebs/gss/server/rest/FilesHandler.java

index 0d0f6dd..db558c6 100644 (file)
@@ -71,6 +71,8 @@ public class FileContextMenu extends PopupPanel implements ClickListener {
 
        private MenuItem downloadItem;
 
+       private MenuItem saveAsItem;
+
        /**
         * The image bundle for this widget's images that reuses images defined in
         * other menus.
@@ -161,9 +163,12 @@ public class FileContextMenu extends PopupPanel implements ClickListener {
                        if(currentFolder!=null && currentFolder.getUserObject() instanceof FolderResource)
                                contextMenu.addItem(updateItem);
                        String[] link = {"", ""};
-                       gss.getTopPanel().getFileMenu().createDownloadLink(link);
+                       gss.getTopPanel().getFileMenu().createDownloadLink(link, false);
                        downloadItem = new MenuItem("<span>" + link[0] + newImages.download().getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
                        contextMenu.addItem(downloadItem);
+                       gss.getTopPanel().getFileMenu().createDownloadLink(link, true);
+                       saveAsItem = new MenuItem("<span>" + link[0] + newImages.download().getHTML() + " Save file as" + link[1] + "</span>", true, downloadCmd);
+                       contextMenu.addItem(saveAsItem);
                        contextMenu.addItem(cutItem);
                        contextMenu.addItem(copyItem);
                        if(currentFolder!=null && currentFolder.getUserObject() instanceof FolderResource)
index 3eb72e9..0529543 100644 (file)
@@ -118,8 +118,10 @@ public class FileMenu extends PopupPanel implements ClickListener {
         * @param link a String array with two elements that is modified so that the\r
         *            first position contains the opening tag and the second one the\r
         *            closing tag\r
+        * @param forceDownload If true, link will be such that browser should ask for filename\r
+        *                              and save location\r
         */\r
-       void createDownloadLink(String[] link) {\r
+       void createDownloadLink(String[] link, boolean forceDownload) {\r
                GSS app = GSS.get();\r
                Object selection = app.getCurrentSelection();\r
                if (selection != null && selection instanceof FileResource) {\r
@@ -127,7 +129,8 @@ public class FileMenu extends PopupPanel implements ClickListener {
                        String dateString = RestCommand.getDate();\r
                        String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());\r
                        String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));\r
-                       link[0] = "<a class='hidden-link' href='" + file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString) + "' target='_blank'>";\r
+                       link[0] = "<a class='hidden-link' href='" + file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString)\r
+                                       + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";\r
                        link[1] = "</a>";\r
                }\r
        }\r
@@ -151,8 +154,10 @@ public class FileMenu extends PopupPanel implements ClickListener {
                if (uploadVisible) contextMenu.addItem("<span>" + images.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));\r
                if (downloadVisible) {\r
                        String[] link = {"", ""};\r
-                       createDownloadLink(link);\r
+                       createDownloadLink(link, false);\r
                        contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);\r
+                       createDownloadLink(link, true);\r
+                       contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Save file as" + link[1] + "</span>", true, downloadCmd);\r
                }\r
                contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));\r
                contextMenu.addItem("<span>" + images.refresh().getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));\r
index f11d086..60a5ace 100644 (file)
@@ -426,8 +426,11 @@ public class FilesHandler extends RequestHandler {
                                // Silent catch
                        }
                        try {
-                               if(file != null && needsContentDisposition(req))
-                                               resp.setHeader("Content-Disposition","attachment; filename=\""+file.getName()+"\"");
+                               if(file != null)
+                                               if (needsContentDisposition(req))
+                                               resp.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8"));
+                                       else
+                                               resp.setHeader("Content-Disposition","inline; filename*=UTF-8''"+URLEncoder.encode(file.getName(),"UTF-8"));
                                if (ostream != null)
                                                copy(file, renderResult, ostream, req, oldBody);
                                        else
@@ -529,8 +532,11 @@ public class FilesHandler extends RequestHandler {
         * @return true if the Content-Disposition HTTP header must be set
         */
        private boolean needsContentDisposition(HttpServletRequest req) {
-               String agent = req.getHeader("user-agent");
+               /*String agent = req.getHeader("user-agent");
                if (agent != null && agent.contains("MSIE"))
+                       return true;*/
+               String dl = req.getParameter("dl");
+               if ("1".equals(dl))
                        return true;
                return false;
        }