From: <udi...@us...> - 2021-09-16 07:18:11
|
Revision: 1390 http://sourceforge.net/p/j-trac/code/1390 Author: udittmer Date: 2021-09-16 07:18:08 +0000 (Thu, 16 Sep 2021) Log Message: ----------- open text and image attachments in new tab instead of downloading them Modified Paths: -------------- trunk/jtrac/src/main/java/info/jtrac/util/AttachmentUtils.java trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.html trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.java trunk/jtrac/src/main/resources/messages.properties trunk/jtrac/src/main/resources/messages_de.properties Modified: trunk/jtrac/src/main/java/info/jtrac/util/AttachmentUtils.java =================================================================== --- trunk/jtrac/src/main/java/info/jtrac/util/AttachmentUtils.java 2021-09-15 07:13:53 UTC (rev 1389) +++ trunk/jtrac/src/main/java/info/jtrac/util/AttachmentUtils.java 2021-09-16 07:18:08 UTC (rev 1390) @@ -1,41 +1,53 @@ -/* - * Copyright 2002-2005 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package info.jtrac.util; - -import info.jtrac.domain.Attachment; -import java.io.File; - -/** - * Utils that deal with Attachments, upload / download and - * file name String manipulation - */ -public class AttachmentUtils { - - public static String cleanFileName(String path) { - // the client browser could be on Unix or Windows, we don't know - int index = path.lastIndexOf('/'); - if (index == -1) { - index = path.lastIndexOf('\\'); - } - return (index != -1 ? path.substring(index + 1) : path); - } - - public static File getFile(Attachment attachment, String jtracHome) { - return new File(jtracHome + "/attachments/" + attachment.getFilePrefix() + "_" + attachment.getFileName()); - } - -} +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info.jtrac.util; + +import info.jtrac.domain.Attachment; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +/** + * Utils that deal with Attachments, upload / download and file name String manipulation + */ + +public class AttachmentUtils { + + public static String cleanFileName(String path) { + // the client browser could be on Unix or Windows, we don't know + int index = path.lastIndexOf('/'); + if (index == -1) { + index = path.lastIndexOf('\\'); + } + return (index != -1 ? path.substring(index + 1) : path); + } + + public static File getFile (Attachment attachment, String jtracHome) { + return new File(jtracHome + "/attachments/" + attachment.getFilePrefix() + "_" + attachment.getFileName()); + } + + public static String guessFileType (Attachment attachment, String jtracHome) { + String fileType ="application/octet-stream"; + try { + File file = getFile(attachment, jtracHome); + fileType = Files.probeContentType(file.toPath()); + } catch (IOException ioex) { + //System.out.println("can't determine file type of "+attachment.getFileName()+": "+ioex.getMessage()); + } + return fileType; + } +} Modified: trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.html =================================================================== --- trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.html 2021-09-15 07:13:53 UTC (rev 1389) +++ trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.html 2021-09-16 07:18:08 UTC (rev 1390) @@ -1,3 +1,6 @@ <wicket:panel> + <!-- <span wicket:id="attachment"><a href="#" target="_blank"><img src="resources/attachment.gif" class="nav-link"/><span wicket:id="fileName"></span></a></span> + --> + <a wicket:id="attachment" href="#" target="_blank"><img src="resources/attachment.gif" class="nav-link"/><span wicket:id="fileName"></span></a> </wicket:panel> Modified: trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.java =================================================================== --- trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.java 2021-09-15 07:13:53 UTC (rev 1389) +++ trunk/jtrac/src/main/java/info/jtrac/wicket/AttachmentLinkPanel.java 2021-09-16 07:18:08 UTC (rev 1390) @@ -1,89 +1,98 @@ -/* - * Copyright 2002-2005 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package info.jtrac.wicket; - -import info.jtrac.domain.Attachment; -import info.jtrac.util.AttachmentUtils; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import org.apache.wicket.IRequestTarget; -import org.apache.wicket.RequestCycle; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.html.link.Link; -import org.apache.wicket.protocol.http.WebResponse; -import org.apache.wicket.util.io.Streams; - -/** - * link for downloading an attachment - */ -public class AttachmentLinkPanel extends BasePanel { - - public AttachmentLinkPanel(String id, final Attachment attachment) { - - super(id); - - if(attachment == null) { - add(new Label("attachment", "")); - setVisible(false); - return; - } - - final String fileName = getResponse().encodeURL(attachment.getFileName()).toString(); - - Link link = new Link("attachment") { - // adapted from wicket.markup.html.link.DownloadLink - // with the difference that the File is instantiated only after onClick - public void onClick() { - getRequestCycle().setRequestTarget(new IRequestTarget() { - - public void detach(RequestCycle requestCycle) { - } - - public void respond(RequestCycle requestCycle) { - WebResponse r = (WebResponse) requestCycle.getResponse(); - r.setAttachmentHeader(fileName); - try { - File file = AttachmentUtils.getFile(attachment, getJtrac().getJtracHome()); - InputStream is = new FileInputStream(file); - try { - Streams.copy(is, r.getOutputStream()); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - try { - is.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } - } - }); - } - }; - - link.add(new Label("fileName", fileName)); - add(link); - - } - -} +/* + * Copyright 2002-2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info.jtrac.wicket; + +import info.jtrac.domain.Attachment; +import info.jtrac.util.AttachmentUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.wicket.IRequestTarget; +import org.apache.wicket.RequestCycle; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.link.Link; +import org.apache.wicket.protocol.http.WebResponse; +import org.apache.wicket.util.io.Streams; + +/** + * link for downloading an attachment + */ +public class AttachmentLinkPanel extends BasePanel { + + public AttachmentLinkPanel (String id, final Attachment attachment) { + super(id); + + if (attachment == null) { + add(new Label("attachment", "")); + setVisible(false); + return; + } + + final String fileName = getResponse().encodeURL(attachment.getFileName()).toString(); + + Link link = new Link("attachment") { + // adapted from wicket.markup.html.link.DownloadLink + // with the difference that the File is instantiated only after onClick + public void onClick() { + getRequestCycle().setRequestTarget(new IRequestTarget() { + + @Override + public void detach (RequestCycle requestCycle) { } + + @Override + public void respond (RequestCycle requestCycle) { + WebResponse r = (WebResponse) requestCycle.getResponse(); + String fileType = AttachmentUtils.guessFileType(attachment, getJtrac().getJtracHome()); + System.out.println(fileType); + if (fileType != null && (fileType.startsWith("image") || fileType.startsWith("text"))) { + r.setHeader("Content-Disposition", "inline; filename="+fileName); + } else { + r.setAttachmentHeader(fileName); + } + //r.setInlineHeader(fileName); + // not available in Wicket 1.3.7 + try { + File file = AttachmentUtils.getFile(attachment, getJtrac().getJtracHome()); + InputStream is = new FileInputStream(file); + try { + Streams.copy(is, r.getOutputStream()); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + try { + is.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + }); + } + }; + + link.add(new Label("fileName", fileName)); + add(link); + } + +} Modified: trunk/jtrac/src/main/resources/messages.properties =================================================================== --- trunk/jtrac/src/main/resources/messages.properties 2021-09-15 07:13:53 UTC (rev 1389) +++ trunk/jtrac/src/main/resources/messages.properties 2021-09-16 07:18:08 UTC (rev 1390) @@ -204,6 +204,7 @@ config.attachment.maxsize = Maximum size in MB of file-attachments. (default 5 MB) Use -1 for no-limit config.pwd.minLength = Minimal length of passwords (default is 8) config.time.pretty = Show relative timestamps as on Github, Twitter and Facebook (set to true, default is false) +config.urls.autolink = Auto-link URLs in tickets and comments (set to true, default is false) # config_list (config_form does not have any extra messages) config_list.configurationSettings = Configuration Settings Modified: trunk/jtrac/src/main/resources/messages_de.properties =================================================================== --- trunk/jtrac/src/main/resources/messages_de.properties 2021-09-15 07:13:53 UTC (rev 1389) +++ trunk/jtrac/src/main/resources/messages_de.properties 2021-09-16 07:18:08 UTC (rev 1390) @@ -204,7 +204,7 @@ config.attachment.maxsize = Maximalgr\u00F6\u00DFe f\u00FCr Anlagen in MB, "-1" f\u00FCr Kein-Limit (Default 5) config.pwd.minLength = Minimale L\u00e4nge von Passw\u00f6rtern (Default 8) config.time.pretty = Relative Zeiten anzeigen wie auf Github, Twitter und Facebook (auf true setzen, Default ist false) -config.urls.autolink = URLs in Tickets und Kommentaren automatisch verlinken +config.urls.autolink = URLs in Tickets und Kommentaren automatisch verlinken (auf true setzen, Default ist false) # config_list (config_form does not have any extra messages) config_list.configurationSettings = Einstellungen This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |