Skip to content

Commit

Permalink
Added virus scanner interface for scanning uploaded files.
Browse files Browse the repository at this point in the history
  • Loading branch information
gWestenberger committed Aug 28, 2024
1 parent d334e9d commit db17c18
Show file tree
Hide file tree
Showing 23 changed files with 722 additions and 470 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ GUI_UPLOAD_FILE_INVALID_NAME_2 =Der Dateiname {0} ({1}) ist ungültig!
GUI_UPLOAD_FILE_EXISTING_DELETED_1 =Die Datei {0} existiert bereits und ist als gelöscht markiert.
GUI_UPLOAD_CLIENT_LOADING_0 =Bitte warten: Dateien werden in den Speicher geladen.

GUI_UPLOAD_VIRUSES_FOUND_WARNING_0 =Die folgenden Dateien wurden nicht hochgeladen, da der Virenscanner in ihnen Viren gefunden hat.
GUI_UPLOAD_VIRUSES_FOUND_TITLE_0 =Viren gefunden


GUI_UPLOAD_BUTTON_NO_TARGET_0 =Es wurde kein Zielordner konfiguriert

GUI_UPLOAD_HOOK_DIALOG_TITLE_0 =Editieren
Expand Down
4 changes: 0 additions & 4 deletions src-gwt/org/opencms/GwtBaseCommon.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
<replace-with class="org.opencms.ade.upload.client.ui.CmsUploadDialogFormDataImpl">
<when-type-is class="org.opencms.ade.upload.client.ui.CmsUploadDialogImpl" />
</replace-with>

<replace-with class="org.opencms.gwt.client.ui.input.upload.impl.CmsUploaderFormData">
<when-type-is class="org.opencms.gwt.client.ui.input.upload.impl.CmsUploaderDefault" />
</replace-with>

<replace-with class="org.opencms.gwt.client.ui.CmsScrollPanelImpl">
<when-type-assignable class="org.opencms.gwt.client.ui.CmsScrollPanel"/>
Expand Down
6 changes: 6 additions & 0 deletions src-gwt/org/opencms/ade/upload/client/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public final class Messages {
/** Message constant for key in the resource bundle. */
public static final String GUI_UPLOAD_UNZIP_FILE_0 = "GUI_UPLOAD_UNZIP_FILE_0";

/** Message constant for key in the resource bundle. */
public static final String GUI_UPLOAD_VIRUSES_FOUND_TITLE_0 = "GUI_UPLOAD_VIRUSES_FOUND_TITLE_0";

/** Message constant for key in the resource bundle. */
public static final String GUI_UPLOAD_VIRUSES_FOUND_WARNING_0 = "GUI_UPLOAD_VIRUSES_FOUND_WARNING_0";

/** Name of the used resource bundle. */
private static final String BUNDLE_NAME = "org.opencms.ade.upload.clientmessages";

Expand Down
126 changes: 84 additions & 42 deletions src-gwt/org/opencms/ade/upload/client/ui/A_CmsUploadDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ public void execute() {
}

/**
* Parses the upload response of the server and decides what to do.<p>
*
* @param results a JSON Object
* @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog#parseResponse(java.lang.String)
*/
public void parseResponse(String results) {

Expand All @@ -406,11 +404,12 @@ public void parseResponse(String results) {
false);
JSONValue uploadedFilesVal = jsonObject.get(I_CmsUploadConstants.KEY_UPLOADED_FILE_NAMES);
JSONValue uploadHook = jsonObject.get(I_CmsUploadConstants.KEY_UPLOAD_HOOK);
JSONValue uploadedFileIdsVal = jsonObject.get(I_CmsUploadConstants.KEY_UPLOADED_FILES);
JSONArray uploadedFileIdsArray = uploadedFileIdsVal.isArray();

String hookUri = null;
if ((uploadHook != null) && (uploadHook.isString() != null)) {
hookUri = uploadHook.isString().stringValue();
JSONValue uploadedFileIdsVal = jsonObject.get(I_CmsUploadConstants.KEY_UPLOADED_FILES);
JSONArray uploadedFileIdsArray = uploadedFileIdsVal.isArray();
if (uploadedFileIdsArray != null) {
for (int i = 0; i < uploadedFileIdsArray.size(); i++) {
JSONString entry = uploadedFileIdsArray.get(i).isString();
Expand All @@ -420,6 +419,10 @@ public void parseResponse(String results) {
}
}
}
if (uploadedFileIds.size() == 0) {
// no files uploaded, probably because of virus scanner - don't show upload hook dialog
hookUri = null;
}
JSONArray uploadedFilesArray = uploadedFilesVal.isArray();
if (uploadedFilesArray != null) {
for (int i = 0; i < uploadedFilesArray.size(); i++) {
Expand All @@ -429,45 +432,29 @@ public void parseResponse(String results) {
}
}
}
m_progressInfo.finish();
Map<String, List<String>> viruses = CmsVirusReport.getVirusWarnings(jsonObject);
final I_CmsUploadContext context = m_context;
closeOnSuccess();
if (hookUri != null) {
// Set the context to be null so that it isn't called when the upload dialog closed;
// we want it to be called when the upload property dialog is closed instead.<p>
final String finalHookUri = hookUri;
m_progressInfo.finish();
if (!viruses.isEmpty()) {
m_context = null;
CloseHandler<PopupPanel> closeHandler;
closeHandler = new CloseHandler<PopupPanel>() {

public void onClose(CloseEvent<PopupPanel> event) {



if (context != null) {
List<CmsUUID> actualIds = uploadedFileIds.stream().map(id -> new CmsUUID(id)).collect(Collectors.toList());
CmsCoreProvider.getVfsService().getSitePaths(actualIds, new AsyncCallback<List<String>>() {

@Override
public void onFailure(Throwable caught) {
}

@Override
public void onSuccess(List<String> result) {
for (int i = 0; i < result.size(); i++) {
String path = result.get(i);
if (path.startsWith(m_targetFolder)) {
result.set(i, path.substring(m_targetFolder.length()));
}
}
context.onUploadFinished(result);
}
});
}
hide();
CmsPopup virusPopup = CmsVirusReport.createPopup(viruses, () -> {
if (finalHookUri != null) {
openHookDialog(uploadedFileIds, finalHookUri, context);
} else {
context.onUploadFinished(m_uploadedFiles);
}
};

String title = Messages.get().key(Messages.GUI_UPLOAD_HOOK_DIALOG_TITLE_0);
CmsUploadHookDialog.openDialog(title, hookUri, uploadedFileIds, closeHandler);
});
virusPopup.center();
} else {
if (hookUri != null) {
// Set the context to be null so that it isn't called when the upload dialog closed;
// we want it to be called when the upload property dialog is closed instead.<p>
m_context = null;
openHookDialog(uploadedFileIds, hookUri, context);
}
closeOnSuccess();
}
} else {
String message = jsonObject.get(I_CmsUploadConstants.KEY_MESSAGE).isString().stringValue();
Expand All @@ -484,7 +471,19 @@ public void onSuccess(List<String> result) {
*/
public void setContext(I_CmsUploadContext context) {

m_context = context;
if (context != null) {
m_context = new I_CmsUploadContext() {

@Override
public void onUploadFinished(List<String> uploadedFiles) {

context.onUploadFinished(uploadedFiles);
}
};

} else {
m_context = context;
}
}

/**
Expand Down Expand Up @@ -1424,6 +1423,49 @@ private String getTargetRootPath() {
}
}

/**
* Helper method to upload the hook dialog after an upload.
*
* @param uploadedFileIds the uploaded file ids
* @param hookUri the hook URI
* @param context the upload context
*/
private void openHookDialog(List<String> uploadedFileIds, String hookUri, final I_CmsUploadContext context) {

CloseHandler<PopupPanel> closeHandler;
closeHandler = new CloseHandler<PopupPanel>() {

public void onClose(CloseEvent<PopupPanel> event) {

if (context != null) {
List<CmsUUID> actualIds = uploadedFileIds.stream().map(id -> new CmsUUID(id)).collect(
Collectors.toList());
// post-upload hook may rename files
CmsCoreProvider.getVfsService().getSitePaths(actualIds, new AsyncCallback<List<String>>() {

@Override
public void onFailure(Throwable caught) {}

@Override
public void onSuccess(List<String> result) {

for (int i = 0; i < result.size(); i++) {
String path = result.get(i);
if (path.startsWith(m_targetFolder)) {
result.set(i, path.substring(m_targetFolder.length()));
}
}
context.onUploadFinished(result);
}
});
}
}
};

String title = Messages.get().key(Messages.GUI_UPLOAD_HOOK_DIALOG_TITLE_0);
CmsUploadHookDialog.openDialog(title, hookUri, uploadedFileIds, closeHandler);
}

/**
* Removes all widgets from the content wrapper.<p>
*/
Expand Down
158 changes: 158 additions & 0 deletions src-gwt/org/opencms/ade/upload/client/ui/CmsVirusReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

package org.opencms.ade.upload.client.ui;

import org.opencms.gwt.client.CmsCoreProvider;
import org.opencms.gwt.client.ui.CmsFieldSet;
import org.opencms.gwt.client.ui.CmsList;
import org.opencms.gwt.client.ui.CmsListItem;
import org.opencms.gwt.client.ui.CmsListItemWidget;
import org.opencms.gwt.client.ui.CmsMessageWidget;
import org.opencms.gwt.client.ui.CmsPopup;
import org.opencms.gwt.client.ui.CmsPushButton;
import org.opencms.gwt.client.ui.CmsScrollPanel;
import org.opencms.gwt.shared.CmsListInfoBean;
import org.opencms.gwt.shared.I_CmsUploadConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Joiner;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
* Shows which viruses were found in files the user tried to upload.
*/
public class CmsVirusReport extends Composite {

/** The UiBinder interface for this widget. */
protected interface I_CmsVirusReportUiBinder extends UiBinder<Widget, CmsVirusReport> {
// empty
}

/** The UIBinder for this class. */
static final I_CmsVirusReportUiBinder UI_BINDER = GWT.create(I_CmsVirusReportUiBinder.class);

/** Displays the warning message to the user. */
@UiField
protected CmsMessageWidget m_message;

/** Contains the resource boxes with the virus information. */
@UiField
protected CmsFieldSet m_fieldset;

/**
* Creates a new virus report widget.
*
* @param viruses a map from file names to lists of virus names found in the corresponding files
*/
public CmsVirusReport(Map<String, List<String>> viruses) {

Widget content = UI_BINDER.createAndBindUi(this);
initWidget(content);
CmsScrollPanel scroll = new CmsScrollPanel();
scroll.getElement().getStyle().setProperty("maxHeight", 500, Unit.PX);
CmsList<?> list = new CmsList<>();
m_fieldset.add(scroll);
scroll.add(list);
m_message.setMessageText(
org.opencms.ade.upload.client.Messages.get().key(
org.opencms.ade.upload.client.Messages.GUI_UPLOAD_VIRUSES_FOUND_WARNING_0));

for (Map.Entry<String, List<String>> entry : viruses.entrySet()) {
String commaSeparatedViruses = Joiner.on(", ").join(entry.getValue());
CmsListInfoBean infoBean = new CmsListInfoBean(entry.getKey(), commaSeparatedViruses, null);
CmsListItemWidget listItemWidget = new CmsListItemWidget(infoBean);
listItemWidget.setIcon(CmsCoreProvider.get().getResourceTypeIcon(entry.getKey()));
list.add(new CmsListItem(listItemWidget));
}
}

/**
* Creates a popup containing a virus report.
*
* @param viruses a map from file names to lists of viruses found in the corresponding files
* @param callback the callback to executing after closing the dialog
* @return
*/
public static CmsPopup createPopup(Map<String, List<String>> viruses, Runnable callback) {

CmsPopup popup = new CmsPopup();
popup.setModal(true);
popup.setGlassEnabled(true);
popup.setMainContent(new CmsVirusReport(viruses));
popup.setCaption(
org.opencms.ade.upload.client.Messages.get().key(
org.opencms.ade.upload.client.Messages.GUI_UPLOAD_VIRUSES_FOUND_TITLE_0));
CmsPushButton ok = new CmsPushButton();
ok.setText(org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_OK_0));
ok.addClickHandler(event -> {
popup.hide();
callback.run();
});
popup.addButton(ok);
return popup;
}

/**
* Extracts virus warnings from a JSON object.
*
* @param jsonObject the JSON object
* @return the map of virus warnings, which maps file names to lists of viruses found in the corresponding file
*/
public static Map<String, List<String>> getVirusWarnings(JSONObject jsonObject) {

Map<String, List<String>> viruses = new HashMap<>();
JSONValue virusWarnings = jsonObject.get(I_CmsUploadConstants.ATTR_VIRUS_WARNINGS);
if (virusWarnings != null) {
JSONObject virusWarningsObj = (JSONObject)virusWarnings;
for (String key : virusWarningsObj.keySet()) {
List<String> virusList = new ArrayList<>();
viruses.put(key, virusList);
JSONArray virusesForFile = (JSONArray)virusWarningsObj.get(key);
for (int i = 0; i < virusesForFile.size(); i++) {
JSONString jsv = (JSONString)virusesForFile.get(i);
virusList.add(jsv.stringValue());
}
}
}
return viruses;
}

}
15 changes: 15 additions & 0 deletions src-gwt/org/opencms/ade/upload/client/ui/CmsVirusReport.ui.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:cmsi="urn:import:org.opencms.gwt.client.ui.input"
xmlns:cms="urn:import:org.opencms.gwt.client.ui">

<ui:with field="input" type="org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle" />
<ui:with field="main" type="org.opencms.gwt.client.ui.css.I_CmsLayoutBundle" />
<ui:style>
</ui:style>
<g:FlowPanel>
<cms:CmsMessageWidget ui:field="m_message" />
<cms:CmsFieldSet ui:field="m_fieldset" />
</g:FlowPanel>
</ui:UiBinder>
Loading

0 comments on commit db17c18

Please sign in to comment.