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
master
jensp 2015-02-04 08:19:55 +00:00
parent 773477e321
commit 055e31c990
4 changed files with 143 additions and 97 deletions

View File

@ -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_file=File
cms.contentassets.file_attachment.tableheader_description=Description cms.contentassets.file_attachment.tableheader_description=Description
cms.contentassets.file_attachment.list_label=File attachments 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.

View File

@ -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_file=Datei
cms.contentassets.file_attachment.tableheader_description=Beschreibung cms.contentassets.file_attachment.tableheader_description=Beschreibung
cms.contentassets.file_attachment.list_label=Dateianh\u00e4nge 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.

View File

@ -16,7 +16,6 @@ package com.arsdigita.cms.contentassets.ui;
import com.arsdigita.bebop.ColumnPanel; import com.arsdigita.bebop.ColumnPanel;
import com.arsdigita.bebop.Form; import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState; 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.FormInitListener;
import com.arsdigita.bebop.event.FormProcessListener; import com.arsdigita.bebop.event.FormProcessListener;
import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.FormValidationListener;
import com.arsdigita.bebop.form.TextArea; import com.arsdigita.bebop.form.TextArea;
import com.arsdigita.bebop.parameters.NotNullValidationListener; import com.arsdigita.bebop.parameters.NotNullValidationListener;
import com.arsdigita.cms.ContentItem; 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.contentassets.FileAttachmentGlobalize;
import com.arsdigita.cms.ui.FileUploadSection; import com.arsdigita.cms.ui.FileUploadSection;
import com.arsdigita.cms.util.GlobalizationUtil; import com.arsdigita.cms.util.GlobalizationUtil;
import com.arsdigita.dispatcher.DispatcherHelper;
import com.arsdigita.mimetypes.ImageMimeType; import com.arsdigita.mimetypes.ImageMimeType;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.DataObject;
import java.io.File; import java.io.File;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.IOException;
/** /**
* A form for uploading file attachments. Displays a mime-type selection * A form for uploading file attachments. Displays a mime-type selection box.
* box.
* *
* @author Scott Seago (sseago@redhat.com) * @author Scott Seago (sseago@redhat.com)
* @version $Revision: #2 $ $DateTime: 2004/03/30 18:21:14 $ * @version $Revision: #2 $ $DateTime: 2004/03/30 18:21:14 $
* @version $Id: FileAttachmentUpload.java 287 2005-02-22 00:29:02Z sskracic $ * @version $Id: FileAttachmentUpload.java 287 2005-02-22 00:29:02Z sskracic $
*/ */
public class FileAttachmentUpload extends Form public class FileAttachmentUpload extends Form
implements FormInitListener, FormProcessListener { implements FormInitListener, FormProcessListener, FormValidationListener {
private static final Logger s_log = Logger.getLogger(FileAttachmentUpload.class); private static final Logger s_log = Logger.getLogger(FileAttachmentUpload.class);
@ -58,15 +63,15 @@ public class FileAttachmentUpload extends Form
/** /**
* Construct a new FileAttachmentUpload * Construct a new FileAttachmentUpload
* *
* @param itemModel The {@link ItemSelectionModel} which will * @param itemModel The {@link ItemSelectionModel} which will be responsible for loading the
* be responsible for loading the current item * current item
* *
*/ */
public FileAttachmentUpload(ItemSelectionModel itemModel) { public FileAttachmentUpload(ItemSelectionModel itemModel) {
super("FileAttachmentUpload", new ColumnPanel(2)); super("FileAttachmentUpload", new ColumnPanel(2));
m_itemModel = itemModel; m_itemModel = itemModel;
ColumnPanel panel = (ColumnPanel)getPanel(); ColumnPanel panel = (ColumnPanel) getPanel();
panel.setBorder(false); panel.setBorder(false);
panel.setPadColor("#FFFFFF"); panel.setPadColor("#FFFFFF");
panel.setColumnWidth(1, "20%"); panel.setColumnWidth(1, "20%");
@ -80,6 +85,7 @@ public class FileAttachmentUpload extends Form
addInitListener(this); addInitListener(this);
addProcessListener(this); addProcessListener(this);
addValidationListener(this);
setMethod(Form.POST); setMethod(Form.POST);
setEncType("multipart/form-data"); setEncType("multipart/form-data");
} }
@ -100,15 +106,15 @@ public class FileAttachmentUpload extends Form
/** /**
* @param state The page state * @param state The page state
*
* @return the currently selected item * @return the currently selected item
*/ */
public ContentItem getContentItem(PageState state) { 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 * Set the image asset. This will probably be done in the process listener
* listener
* *
* @param state The page state * @param state The page state
* @param asset The image asset * @param asset The image asset
@ -123,8 +129,7 @@ public class FileAttachmentUpload extends Form
// Add the widgets // Add the widgets
public void addWidgets() { public void addWidgets() {
m_fileUploadSection = m_fileUploadSection = new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(),
new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(),
"file", "file",
ImageMimeType.MIME_IMAGE_JPEG); ImageMimeType.MIME_IMAGE_JPEG);
m_fileUploadSection.getFileUploadWidget() m_fileUploadSection.getFileUploadWidget()
@ -138,45 +143,65 @@ public class FileAttachmentUpload extends Form
add(m_description); add(m_description);
} }
@Override
public void init(FormSectionEvent event) throws FormProcessException { public void init(FormSectionEvent event) throws FormProcessException {
// Do nothing. // 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 // 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"); s_log.debug("Uploading File");
FormData data = event.getFormData(); final PageState state = event.getPageState();
PageState state = event.getPageState(); final ContentItem item = getContentItem(state);
ContentItem item = getContentItem(state);
FileAttachment a;
try { // try {
// Get the text asset or create a new one // Get the text asset or create a new one
String fileName = m_fileUploadSection.getFileName(event); final String fileName = m_fileUploadSection.getFileName(event);
final File file = m_fileUploadSection.getFile(event);
File file = m_fileUploadSection.getFile(event); final FileAttachment attachment = new FileAttachment();
a = new FileAttachment();
// Load the asset from file // Load the asset from file
a.setFileOwner(item); attachment.setFileOwner(item);
a.loadFromFile(fileName, file, "application/octet-stream"); try {
a.setDescription((String)m_description.getValue(state)); attachment.loadFromFile(fileName, file, "application/octet-stream");
a.save(); } catch (IOException ex) {
throw new FormProcessException(ex);
}
attachment.setDescription((String) m_description.getValue(state));
attachment.save();
item.save(); item.save();
// Save everything // Save everything
this.setFileAttachment(state); this.setFileAttachment(state);
// Utilities.disableBrowserCache(state.getResponse()); DispatcherHelper.cacheDisable(state.getResponse());
s_log.debug("File Uploaded"); s_log.debug("File Uploaded");
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
throw new FormProcessException(e); // throw new FormProcessException(e);
} // }
} }
/** /**

View File

@ -40,8 +40,10 @@ import org.apache.log4j.Logger;
* @author Scott Seago (sseago@redhat.com) * @author Scott Seago (sseago@redhat.com)
* @version $Id: FileAttachmentsTable.java 1592 2007-06-21 16:48:55Z lbcfrancois $ * @version $Id: FileAttachmentsTable.java 1592 2007-06-21 16:48:55Z lbcfrancois $
* *
**/ *
*/
public class FileAttachmentsTable extends Table { public class FileAttachmentsTable extends Table {
private static final Logger s_log = Logger.getLogger(FileAttachmentsTable.class); private static final Logger s_log = Logger.getLogger(FileAttachmentsTable.class);
public static final String[] s_tableHeaders = {"File", " ", " ", " ", "Description", " "}; public static final String[] s_tableHeaders = {"File", " ", " ", " ", "Description", " "};
@ -67,6 +69,7 @@ public class FileAttachmentsTable extends Table {
getColumn(5).setCellRenderer(new DeleteLinkCellRenderer()); getColumn(5).setCellRenderer(new DeleteLinkCellRenderer());
m_size = new RequestLocal(); m_size = new RequestLocal();
m_editor = new RequestLocal() { m_editor = new RequestLocal() {
public Object initialValue(PageState state) { public Object initialValue(PageState state) {
SecurityManager sm = Utilities.getSecurityManager(state); SecurityManager sm = Utilities.getSecurityManager(state);
ContentItem item = (ContentItem) m_model ContentItem item = (ContentItem) m_model
@ -78,6 +81,7 @@ public class FileAttachmentsTable extends Table {
)); ));
return val; return val;
} }
}; };
setWidth("100%"); setWidth("100%");
@ -92,24 +96,21 @@ public class FileAttachmentsTable extends Table {
if (Boolean.TRUE.equals(m_editor.get(state))) { if (Boolean.TRUE.equals(m_editor.get(state))) {
if (DELETE_EVENT.equals(event)) { if (DELETE_EVENT.equals(event)) {
try { try {
FileAttachment attachment = FileAttachment attachment = new FileAttachment(new BigDecimal(value));
new FileAttachment(new BigDecimal(value));
attachment.delete(); attachment.delete();
} catch (DataObjectNotFoundException e) { } catch (DataObjectNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else if (UP_EVENT.equals(event)) { } else if (UP_EVENT.equals(event)) {
try { try {
FileAttachment attachment = FileAttachment attachment = new FileAttachment(new BigDecimal(value));
new FileAttachment(new BigDecimal(value));
attachment.swapWithPrevious(); attachment.swapWithPrevious();
} catch (DataObjectNotFoundException e) { } catch (DataObjectNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else if (DOWN_EVENT.equals(event)) { } else if (DOWN_EVENT.equals(event)) {
try { try {
FileAttachment attachment = FileAttachment attachment = new FileAttachment(new BigDecimal(value));
new FileAttachment(new BigDecimal(value));
attachment.swapWithNext(); attachment.swapWithNext();
} catch (DataObjectNotFoundException e) { } catch (DataObjectNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
@ -125,8 +126,8 @@ public class FileAttachmentsTable extends Table {
} }
} }
private class DeleteLinkCellRenderer implements TableCellRenderer { private class DeleteLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table, public Component getComponent(final Table table,
PageState state, PageState state,
Object value, Object value,
@ -136,23 +137,26 @@ public class FileAttachmentsTable extends Table {
int column) { int column) {
SimpleContainer sc = new SimpleContainer(); SimpleContainer sc = new SimpleContainer();
final String modKey = (String)key; final String modKey = (String) key;
if (Boolean.TRUE.equals(m_editor.get(state))) { if (Boolean.TRUE.equals(m_editor.get(state))) {
ControlLink delLink = new ControlLink("Delete") { ControlLink delLink = new ControlLink("Delete") {
public void setControlEvent(PageState s) { public void setControlEvent(PageState s) {
s.setControlEvent(table, DELETE_EVENT, modKey); s.setControlEvent(table, DELETE_EVENT, modKey);
} }
}; };
sc.add(delLink); sc.add(delLink);
} }
return sc; return sc;
} }
} }
private class EditLinkCellRenderer implements TableCellRenderer { private class EditLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table, public Component getComponent(final Table table,
PageState state, PageState state,
Object value, Object value,
@ -162,16 +166,18 @@ public class FileAttachmentsTable extends Table {
int column) { int column) {
SimpleContainer sc = new SimpleContainer(); SimpleContainer sc = new SimpleContainer();
final String modKey = (String)key; final String modKey = (String) key;
if (Boolean.TRUE.equals(m_editor.get(state))) { if (Boolean.TRUE.equals(m_editor.get(state))) {
if (isSelected) { if (isSelected) {
sc.add(new Label("edit", Label.BOLD)); sc.add(new Label("edit", Label.BOLD));
} else { } else {
ControlLink delLink = new ControlLink("edit") { ControlLink delLink = new ControlLink("edit") {
public void setControlEvent(PageState s) { public void setControlEvent(PageState s) {
s.setControlEvent(table, EDIT_EVENT, modKey); s.setControlEvent(table, EDIT_EVENT, modKey);
} }
}; };
sc.add(delLink); sc.add(delLink);
} }
@ -180,10 +186,11 @@ public class FileAttachmentsTable extends Table {
return sc; return sc;
} }
} }
private class MoveUpLinkCellRenderer implements TableCellRenderer { private class MoveUpLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table, public Component getComponent(final Table table,
PageState state, PageState state,
Object value, Object value,
@ -194,23 +201,26 @@ public class FileAttachmentsTable extends Table {
boolean isFirst = (row == 0); boolean isFirst = (row == 0);
SimpleContainer sc = new SimpleContainer(); 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") { ControlLink delLink = new ControlLink("up") {
public void setControlEvent(PageState s) { public void setControlEvent(PageState s) {
s.setControlEvent(table, UP_EVENT, modKey); s.setControlEvent(table, UP_EVENT, modKey);
} }
}; };
sc.add(delLink); sc.add(delLink);
} }
return sc; return sc;
} }
} }
private class MoveDownLinkCellRenderer implements TableCellRenderer { private class MoveDownLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table, public Component getComponent(final Table table,
PageState state, PageState state,
Object value, Object value,
@ -220,19 +230,21 @@ public class FileAttachmentsTable extends Table {
int column) { int column) {
if (m_size.get(state) == null) { if (m_size.get(state) == null) {
m_size.set(state, m_size.set(state,
new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel) new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel) table
table.getTableModel(state)).size())); .getTableModel(state)).size()));
} }
boolean isLast = (row == ((Long)m_size.get(state)).intValue() - 1); boolean isLast = (row == ((Long) m_size.get(state)).intValue() - 1);
SimpleContainer sc = new SimpleContainer(); 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") { ControlLink delLink = new ControlLink("down") {
public void setControlEvent(PageState s) { public void setControlEvent(PageState s) {
s.setControlEvent(table, DOWN_EVENT, modKey); s.setControlEvent(table, DOWN_EVENT, modKey);
} }
}; };
sc.add(delLink); sc.add(delLink);
} }
@ -240,10 +252,11 @@ public class FileAttachmentsTable extends Table {
return sc; return sc;
} }
} }
private class FileLinkCellRenderer implements TableCellRenderer {
private class FileLinkCellRenderer implements TableCellRenderer{
public Component getComponent(final Table table, public Component getComponent(final Table table,
PageState state, PageState state,
Object value, Object value,
@ -251,11 +264,17 @@ public class FileAttachmentsTable extends Table {
Object key, Object key,
int row, int row,
int column) { int column) {
final String downloadKey = (String)key; final String downloadKey = (String) key;
FileAttachment attachment = FileAttachment attachment = new FileAttachment(new BigDecimal(downloadKey));
new FileAttachment(new BigDecimal(downloadKey)); final Link link = new Link(attachment.getDisplayName(),
return new Link(attachment.getDisplayName(),
Utilities.getAssetURL(attachment)); Utilities.getAssetURL(attachment));
link.setTargetFrame("fileview");
return link;
// Utilities.getAssetURL(attachment));
//return new Link(attachment.getDisplayName(),
// Utilities.getAssetURL(attachment));
} }
} }
} }