From 055e31c990ba16bba8f9f8f47291fbab7b9cf5f8 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 4 Feb 2015 08:19:55 +0000 Subject: [PATCH] Added a validate method to the FileAttachmentUpload to prevent uploading files with the same name as an already attached file. git-svn-id: https://svn.libreccm.org/ccm/trunk@3115 8810af33-2d31-482b-a856-94f89814c4df --- .../FileAttachmentResources.properties | 1 + .../FileAttachmentResources_de.properties | 1 + .../ui/FileAttachmentUpload.java | 107 ++++++++------ .../ui/FileAttachmentsTable.java | 131 ++++++++++-------- 4 files changed, 143 insertions(+), 97 deletions(-) diff --git a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources.properties b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources.properties index a35c016b8..313bd7a68 100755 --- a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources.properties +++ b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources.properties @@ -6,3 +6,4 @@ cms.contentassets.file_attachment.upload_new_file_label=Upload a new file cms.contentassets.file_attachment.tableheader_file=File cms.contentassets.file_attachment.tableheader_description=Description cms.contentassets.file_attachment.list_label=File attachments +cms.contentassets.file_attachment.already_attached=A file with the same name as the selected file has already been attached. diff --git a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources_de.properties b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources_de.properties index a2973fc13..2cf63e959 100644 --- a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources_de.properties +++ b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/FileAttachmentResources_de.properties @@ -6,3 +6,4 @@ cms.contentassets.file_attachment.upload_new_file_label=Neue Datei hochladen cms.contentassets.file_attachment.tableheader_file=Datei cms.contentassets.file_attachment.tableheader_description=Beschreibung cms.contentassets.file_attachment.list_label=Dateianh\u00e4nge +cms.contentassets.file_attachment.already_attached=Eine Datei mit dem Namen der ausgew\u00e4hlten Datei wurde bereits hinzugef\u00fcgt. diff --git a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentUpload.java b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentUpload.java index 085664198..ba18129bd 100755 --- a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentUpload.java +++ b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentUpload.java @@ -16,7 +16,6 @@ package com.arsdigita.cms.contentassets.ui; import com.arsdigita.bebop.ColumnPanel; import com.arsdigita.bebop.Form; -import com.arsdigita.bebop.FormData; import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; import com.arsdigita.bebop.PageState; @@ -24,6 +23,7 @@ import com.arsdigita.bebop.SaveCancelSection; import com.arsdigita.bebop.event.FormInitListener; import com.arsdigita.bebop.event.FormProcessListener; import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.FormValidationListener; import com.arsdigita.bebop.form.TextArea; import com.arsdigita.bebop.parameters.NotNullValidationListener; import com.arsdigita.cms.ContentItem; @@ -32,21 +32,26 @@ import com.arsdigita.cms.contentassets.FileAttachment; import com.arsdigita.cms.contentassets.FileAttachmentGlobalize; import com.arsdigita.cms.ui.FileUploadSection; import com.arsdigita.cms.util.GlobalizationUtil; +import com.arsdigita.dispatcher.DispatcherHelper; import com.arsdigita.mimetypes.ImageMimeType; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.DataObject; + import java.io.File; + import org.apache.log4j.Logger; +import java.io.IOException; /** - * A form for uploading file attachments. Displays a mime-type selection - * box. + * A form for uploading file attachments. Displays a mime-type selection box. * * @author Scott Seago (sseago@redhat.com) * @version $Revision: #2 $ $DateTime: 2004/03/30 18:21:14 $ * @version $Id: FileAttachmentUpload.java 287 2005-02-22 00:29:02Z sskracic $ */ public class FileAttachmentUpload extends Form - implements FormInitListener, FormProcessListener { + implements FormInitListener, FormProcessListener, FormValidationListener { private static final Logger s_log = Logger.getLogger(FileAttachmentUpload.class); @@ -58,15 +63,15 @@ public class FileAttachmentUpload extends Form /** * Construct a new FileAttachmentUpload * - * @param itemModel The {@link ItemSelectionModel} which will - * be responsible for loading the current item + * @param itemModel The {@link ItemSelectionModel} which will be responsible for loading the + * current item * */ public FileAttachmentUpload(ItemSelectionModel itemModel) { super("FileAttachmentUpload", new ColumnPanel(2)); m_itemModel = itemModel; - ColumnPanel panel = (ColumnPanel)getPanel(); + ColumnPanel panel = (ColumnPanel) getPanel(); panel.setBorder(false); panel.setPadColor("#FFFFFF"); panel.setColumnWidth(1, "20%"); @@ -80,6 +85,7 @@ public class FileAttachmentUpload extends Form addInitListener(this); addProcessListener(this); + addValidationListener(this); setMethod(Form.POST); setEncType("multipart/form-data"); } @@ -100,15 +106,15 @@ public class FileAttachmentUpload extends Form /** * @param state The page state + * * @return the currently selected item */ public ContentItem getContentItem(PageState state) { - return (ContentItem)m_itemModel.getSelectedObject(state); + return (ContentItem) m_itemModel.getSelectedObject(state); } /** - * Set the image asset. This will probably be done in the process - * listener + * Set the image asset. This will probably be done in the process listener * * @param state The page state * @param asset The image asset @@ -123,10 +129,9 @@ public class FileAttachmentUpload extends Form // Add the widgets public void addWidgets() { - m_fileUploadSection = - new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(), - "file", - ImageMimeType.MIME_IMAGE_JPEG); + m_fileUploadSection = new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(), + "file", + ImageMimeType.MIME_IMAGE_JPEG); m_fileUploadSection.getFileUploadWidget() .addValidationListener(new NotNullValidationListener()); add(m_fileUploadSection, ColumnPanel.INSERT); @@ -138,45 +143,65 @@ public class FileAttachmentUpload extends Form add(m_description); } + @Override public void init(FormSectionEvent event) throws FormProcessException { // Do nothing. } - + + @Override + public void validate(final FormSectionEvent event) throws FormProcessException { + final PageState state = event.getPageState(); + final ContentItem item = getContentItem(state); + final String fileName = m_fileUploadSection.getFileName(event); + + final DataCollection attachments = FileAttachment.getAttachments(item); + while (attachments.next()) { + final DataObject attachment = attachments.getDataObject(); + if (attachment.get(FileAttachment.NAME).equals(fileName)) { + attachments.close(); + throw new FormProcessException(GlobalizationUtil.globalize( + "cms.contentassets.file_attachment.already_attached", new String[]{fileName})); + } + } + attachments.close(); + + } + // process: update the mime type and content - public void process(FormSectionEvent event) throws FormProcessException { + @Override + public void process(final FormSectionEvent event) throws FormProcessException { s_log.debug("Uploading File"); - FormData data = event.getFormData(); - PageState state = event.getPageState(); - ContentItem item = getContentItem(state); - FileAttachment a; + final PageState state = event.getPageState(); + final ContentItem item = getContentItem(state); +// try { + // Get the text asset or create a new one + final String fileName = m_fileUploadSection.getFileName(event); + final File file = m_fileUploadSection.getFile(event); + final FileAttachment attachment = new FileAttachment(); + + // Load the asset from file + attachment.setFileOwner(item); try { - // Get the text asset or create a new one - String fileName = m_fileUploadSection.getFileName(event); + attachment.loadFromFile(fileName, file, "application/octet-stream"); + } catch (IOException ex) { + throw new FormProcessException(ex); + } + attachment.setDescription((String) m_description.getValue(state)); + attachment.save(); - File file = m_fileUploadSection.getFile(event); - - a = new FileAttachment(); - - // Load the asset from file - a.setFileOwner(item); - a.loadFromFile(fileName, file, "application/octet-stream"); - a.setDescription((String)m_description.getValue(state)); - a.save(); - - item.save(); + item.save(); // Save everything + this.setFileAttachment(state); + DispatcherHelper.cacheDisable(state.getResponse()); + s_log.debug("File Uploaded"); - this.setFileAttachment(state); - // Utilities.disableBrowserCache(state.getResponse()); - s_log.debug("File Uploaded"); - - } catch (Exception e) { - e.printStackTrace(); - throw new FormProcessException(e); - } +// } catch (Exception e) { +// e.printStackTrace(); +// throw new FormProcessException(e); +// } } /** diff --git a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentsTable.java b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentsTable.java index e55234170..77a44f119 100755 --- a/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentsTable.java +++ b/ccm-cms-assets-fileattachment/src/com/arsdigita/cms/contentassets/ui/FileAttachmentsTable.java @@ -40,8 +40,10 @@ import org.apache.log4j.Logger; * @author Scott Seago (sseago@redhat.com) * @version $Id: FileAttachmentsTable.java 1592 2007-06-21 16:48:55Z lbcfrancois $ * - **/ + * + */ public class FileAttachmentsTable extends Table { + private static final Logger s_log = Logger.getLogger(FileAttachmentsTable.class); public static final String[] s_tableHeaders = {"File", " ", " ", " ", "Description", " "}; @@ -67,18 +69,20 @@ public class FileAttachmentsTable extends Table { getColumn(5).setCellRenderer(new DeleteLinkCellRenderer()); m_size = new RequestLocal(); m_editor = new RequestLocal() { - public Object initialValue(PageState state) { - SecurityManager sm = Utilities.getSecurityManager(state); - ContentItem item = (ContentItem) m_model - .getSelectedObject(state); - Boolean val = new Boolean(sm.canAccess( - state.getRequest(), - SecurityManager.EDIT_ITEM, - item - )); - return val; - } - }; + + public Object initialValue(PageState state) { + SecurityManager sm = Utilities.getSecurityManager(state); + ContentItem item = (ContentItem) m_model + .getSelectedObject(state); + Boolean val = new Boolean(sm.canAccess( + state.getRequest(), + SecurityManager.EDIT_ITEM, + item + )); + return val; + } + + }; setWidth("100%"); } @@ -92,24 +96,21 @@ public class FileAttachmentsTable extends Table { if (Boolean.TRUE.equals(m_editor.get(state))) { if (DELETE_EVENT.equals(event)) { try { - FileAttachment attachment = - new FileAttachment(new BigDecimal(value)); + FileAttachment attachment = new FileAttachment(new BigDecimal(value)); attachment.delete(); } catch (DataObjectNotFoundException e) { e.printStackTrace(); } } else if (UP_EVENT.equals(event)) { try { - FileAttachment attachment = - new FileAttachment(new BigDecimal(value)); + FileAttachment attachment = new FileAttachment(new BigDecimal(value)); attachment.swapWithPrevious(); } catch (DataObjectNotFoundException e) { e.printStackTrace(); } } else if (DOWN_EVENT.equals(event)) { try { - FileAttachment attachment = - new FileAttachment(new BigDecimal(value)); + FileAttachment attachment = new FileAttachment(new BigDecimal(value)); attachment.swapWithNext(); } catch (DataObjectNotFoundException e) { e.printStackTrace(); @@ -125,8 +126,8 @@ public class FileAttachmentsTable extends Table { } } - private class DeleteLinkCellRenderer implements TableCellRenderer { + public Component getComponent(final Table table, PageState state, Object value, @@ -136,23 +137,26 @@ public class FileAttachmentsTable extends Table { int column) { SimpleContainer sc = new SimpleContainer(); - final String modKey = (String)key; + final String modKey = (String) key; if (Boolean.TRUE.equals(m_editor.get(state))) { ControlLink delLink = new ControlLink("Delete") { - public void setControlEvent(PageState s) { - s.setControlEvent(table, DELETE_EVENT, modKey); - } - }; + + public void setControlEvent(PageState s) { + s.setControlEvent(table, DELETE_EVENT, modKey); + } + + }; sc.add(delLink); } return sc; } + } - private class EditLinkCellRenderer implements TableCellRenderer { + public Component getComponent(final Table table, PageState state, Object value, @@ -162,17 +166,19 @@ public class FileAttachmentsTable extends Table { int column) { SimpleContainer sc = new SimpleContainer(); - final String modKey = (String)key; + final String modKey = (String) key; if (Boolean.TRUE.equals(m_editor.get(state))) { if (isSelected) { sc.add(new Label("edit", Label.BOLD)); } else { ControlLink delLink = new ControlLink("edit") { - public void setControlEvent(PageState s) { - s.setControlEvent(table, EDIT_EVENT, modKey); - } - }; + + public void setControlEvent(PageState s) { + s.setControlEvent(table, EDIT_EVENT, modKey); + } + + }; sc.add(delLink); } @@ -180,10 +186,11 @@ public class FileAttachmentsTable extends Table { return sc; } + } - private class MoveUpLinkCellRenderer implements TableCellRenderer { + public Component getComponent(final Table table, PageState state, Object value, @@ -194,23 +201,26 @@ public class FileAttachmentsTable extends Table { boolean isFirst = (row == 0); SimpleContainer sc = new SimpleContainer(); - final String modKey = (String)key; + final String modKey = (String) key; - if (!isFirst && Boolean.TRUE.equals(m_editor.get(state))) { + if (!isFirst && Boolean.TRUE.equals(m_editor.get(state))) { ControlLink delLink = new ControlLink("up") { - public void setControlEvent(PageState s) { - s.setControlEvent(table, UP_EVENT, modKey); - } - }; + + public void setControlEvent(PageState s) { + s.setControlEvent(table, UP_EVENT, modKey); + } + + }; sc.add(delLink); } return sc; } + } - private class MoveDownLinkCellRenderer implements TableCellRenderer { + public Component getComponent(final Table table, PageState state, Object value, @@ -219,31 +229,34 @@ public class FileAttachmentsTable extends Table { int row, int column) { if (m_size.get(state) == null) { - m_size.set(state, - new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel) - table.getTableModel(state)).size())); - } - boolean isLast = (row == ((Long)m_size.get(state)).intValue() - 1); + m_size.set(state, + new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel) table + .getTableModel(state)).size())); + } + boolean isLast = (row == ((Long) m_size.get(state)).intValue() - 1); SimpleContainer sc = new SimpleContainer(); - final String modKey = (String)key; + final String modKey = (String) key; - if (!isLast && Boolean.TRUE.equals(m_editor.get(state))) { + if (!isLast && Boolean.TRUE.equals(m_editor.get(state))) { ControlLink delLink = new ControlLink("down") { - public void setControlEvent(PageState s) { - s.setControlEvent(table, DOWN_EVENT, modKey); - } - }; + + public void setControlEvent(PageState s) { + s.setControlEvent(table, DOWN_EVENT, modKey); + } + + }; sc.add(delLink); } return sc; } + } + private class FileLinkCellRenderer implements TableCellRenderer { - private class FileLinkCellRenderer implements TableCellRenderer{ public Component getComponent(final Table table, PageState state, Object value, @@ -251,11 +264,17 @@ public class FileAttachmentsTable extends Table { Object key, int row, int column) { - final String downloadKey = (String)key; - FileAttachment attachment = - new FileAttachment(new BigDecimal(downloadKey)); - return new Link(attachment.getDisplayName(), - Utilities.getAssetURL(attachment)); + final String downloadKey = (String) key; + FileAttachment attachment = new FileAttachment(new BigDecimal(downloadKey)); + final Link link = new Link(attachment.getDisplayName(), + Utilities.getAssetURL(attachment)); + link.setTargetFrame("fileview"); + return link; + // Utilities.getAssetURL(attachment)); + //return new Link(attachment.getDisplayName(), + // Utilities.getAssetURL(attachment)); } + } + }