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_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.

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_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.

View File

@ -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,8 +63,8 @@ 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) {
@ -80,6 +85,7 @@ public class FileAttachmentUpload extends Form
addInitListener(this);
addProcessListener(this);
addValidationListener(this);
setMethod(Form.POST);
setEncType("multipart/form-data");
}
@ -100,6 +106,7 @@ public class FileAttachmentUpload extends Form
/**
* @param state The page state
*
* @return the currently selected item
*/
public ContentItem getContentItem(PageState state) {
@ -107,8 +114,7 @@ public class FileAttachmentUpload extends Form
}
/**
* 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,8 +129,7 @@ public class FileAttachmentUpload extends Form
// Add the widgets
public void addWidgets() {
m_fileUploadSection =
new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(),
m_fileUploadSection = new FileUploadSection(FileAttachmentGlobalize.FileTypeLabel(),
"file",
ImageMimeType.MIME_IMAGE_JPEG);
m_fileUploadSection.getFileUploadWidget()
@ -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 {
// try {
// Get the text asset or create a new one
String fileName = m_fileUploadSection.getFileName(event);
File file = m_fileUploadSection.getFile(event);
a = new FileAttachment();
final String fileName = m_fileUploadSection.getFileName(event);
final File file = m_fileUploadSection.getFile(event);
final FileAttachment attachment = 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();
attachment.setFileOwner(item);
try {
attachment.loadFromFile(fileName, file, "application/octet-stream");
} catch (IOException ex) {
throw new FormProcessException(ex);
}
attachment.setDescription((String) m_description.getValue(state));
attachment.save();
item.save();
// Save everything
this.setFileAttachment(state);
// Utilities.disableBrowserCache(state.getResponse());
DispatcherHelper.cacheDisable(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);
// }
}
/**

View File

@ -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,6 +69,7 @@ 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
@ -78,6 +81,7 @@ public class FileAttachmentsTable extends Table {
));
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,
@ -140,19 +141,22 @@ public class FileAttachmentsTable extends Table {
if (Boolean.TRUE.equals(m_editor.get(state))) {
ControlLink delLink = new ControlLink("Delete") {
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,
@ -169,9 +173,11 @@ public class FileAttachmentsTable extends Table {
sc.add(new Label("edit", Label.BOLD));
} else {
ControlLink delLink = new ControlLink("edit") {
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,
@ -198,19 +205,22 @@ public class FileAttachmentsTable extends Table {
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);
}
};
sc.add(delLink);
}
return sc;
}
}
private class MoveDownLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table,
PageState state,
Object value,
@ -220,8 +230,8 @@ public class FileAttachmentsTable extends Table {
int column) {
if (m_size.get(state) == null) {
m_size.set(state,
new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel)
table.getTableModel(state)).size()));
new Long(((FileAttachmentModelBuilder.FileAttachmentTableModel) table
.getTableModel(state)).size()));
}
boolean isLast = (row == ((Long) m_size.get(state)).intValue() - 1);
SimpleContainer sc = new SimpleContainer();
@ -230,9 +240,11 @@ public class FileAttachmentsTable extends Table {
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);
}
};
sc.add(delLink);
}
@ -240,10 +252,11 @@ public class FileAttachmentsTable extends Table {
return sc;
}
}
private class FileLinkCellRenderer implements TableCellRenderer {
public Component getComponent(final Table table,
PageState state,
Object value,
@ -252,10 +265,16 @@ public class FileAttachmentsTable extends Table {
int row,
int column) {
final String downloadKey = (String) key;
FileAttachment attachment =
new FileAttachment(new BigDecimal(downloadKey));
return new Link(attachment.getDisplayName(),
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));
}
}
}