Skip to content

Commit

Permalink
Upload limiter for Sword and Web UI, including Dropbox
Browse files Browse the repository at this point in the history
  • Loading branch information
raprasad committed May 13, 2015
1 parent 87455f6 commit c146f46
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
56 changes: 43 additions & 13 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ public enum DisplayMode {

private DataFile selectedDownloadFile;

private Integer maxFileUploadSizeInBytes = null;


/*
Save the setting locally so db isn't hit repeatedly
This may be "null", signifying unlimited download size
*/
public Integer getMaxFileUploadSizeInBytes(){
return this.maxFileUploadSizeInBytes;
}

public boolean isUnlimitedUploadFileSize(){

if (this.maxFileUploadSizeInBytes == null){
return true;
}
return false;
}

public DataFile getSelectedDownloadFile() {
return selectedDownloadFile;
}
Expand Down Expand Up @@ -487,6 +507,7 @@ public String getDropBoxSelection() {
public String getDropBoxKey() {
// Site-specific DropBox application registration key is configured
// via a JVM option under glassfish.

String configuredDropBoxKey = System.getProperty("dataverse.dropbox.key");
if (configuredDropBoxKey != null) {
return configuredDropBoxKey;
Expand Down Expand Up @@ -939,7 +960,8 @@ private void loadMapLayerMetadataLookup() {
public String init() {
// System.out.println("_YE_OLDE_QUERY_COUNTER_"); // for debug purposes
String nonNullDefaultIfKeyNotFound = "";

this.maxFileUploadSizeInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes);

guestbookResponse = new GuestbookResponse();
protocol = settingsService.getValueForKey(SettingsServiceBean.Key.Protocol, nonNullDefaultIfKeyNotFound);
authority = settingsService.getValueForKey(SettingsServiceBean.Key.Authority, nonNullDefaultIfKeyNotFound);
Expand Down Expand Up @@ -1994,18 +2016,21 @@ private HttpClient getClient() {
return new HttpClient();
}

public boolean showFileUploadFileComponent(){

if ((this.editMode == this.editMode.FILE) || (this.editMode == this.editMode.CREATE)){
return true;
}
return false;
}


public void handleDropBoxUpload(ActionEvent e) {
// Read JSON object from the output of the DropBox Chooser:
JsonReader dbJsonReader = Json.createReader(new StringReader(dropBoxSelection));
JsonArray dbArray = dbJsonReader.readArray();
dbJsonReader.close();

Integer maxUploadInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes);
if (maxUploadInBytes == null){
maxUploadInBytes = -1;
}



for (int i = 0; i < dbArray.size(); i++) {
JsonObject dbObject = dbArray.getJsonObject(i);

Expand All @@ -2015,11 +2040,16 @@ public void handleDropBoxUpload(ActionEvent e) {
int fileSize = dbObject.getInt("bytes");

logger.fine("DropBox url: " + fileLink + ", filename: " + fileName + ", size: " + fileSize);

// If the file is too big, skip this upload
//
if ((maxUploadInBytes > -1)&&(fileSize > maxUploadInBytes)){
continue; // continue and add error mesage

/* ----------------------------
If the file is too big:
- Add error mesage
- Go to the next file
// ---------------------------- */
if ((!this.isUnlimitedUploadFileSize())&&(fileSize > this.getMaxFileUploadSizeInBytes())){
String warningMessage = "Dropbox file \"" + fileName + "\" exceeded the limit of " + fileSize + " bytes and was not uploaded.";
FacesContext.getCurrentInstance().addMessage(e.getComponent().getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "upload failure", warningMessage));
continue; // skip to next file, and add error mesage
}

DataFile dFile = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,15 @@ public String getTempDirectory() {

@Override
public int getMaxUploadSize() {

int unlimited = -1;

Integer maxUploadInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes);
if (maxUploadInBytes == null){
return unlimited;
return unlimited;
}

return maxUploadInBytes;
/*
String maxUploadInBytes = settingsService.getValueForKey(SettingsServiceBean.Key.MaxFileUploadSizeInBytes);
if (maxUploadInBytes != null) {
try {
int maxUploadSizeInBytes = Integer.parseInt(maxUploadInBytes);
return maxUploadSizeInBytes;
} catch (NumberFormatException ex) {
logger.info("Could not convert " + maxUploadInBytes + " from setting " + SettingsServiceBean.Key.MaxFileUploadSizeInBytes + " to int. Setting Data Deposit API max upload size limit to unlimited.");
return unlimited;
}
} else {
logger.fine("Setting " + SettingsServiceBean.Key.MaxFileUploadSizeInBytes + " is undefined. Setting Data Deposit API max upload size limit to unlimited.");
return unlimited;
}
*/
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.NotFoundException;

/**
* Service bean accessing a persistent hash map, used as settings in the application.
Expand All @@ -22,6 +24,8 @@
@Named
public class SettingsServiceBean {

private static final Logger logger = Logger.getLogger(SettingsServiceBean.class.getCanonicalName());

/**
* Some convenient keys for the settings. Note that the setting's
* name is really a {@code String}, but it's good to have the compiler look
Expand Down Expand Up @@ -174,6 +178,7 @@ public String getValueForKey( Key key ) {
* Attempt to convert the value to an integer
* - Applicable for keys such as MaxFileUploadSizeInBytes
*
* On failure (key not found or string not convertible to an int), returns null
* @param key
* @return
*/
Expand All @@ -189,7 +194,7 @@ public Integer getValueForKeyAsInt(Key key){
int valAsInt = Integer.parseInt(val);
return valAsInt;
} catch (NumberFormatException ex) {
//logger.info("Could not convert " + key + " from setting " + key.toString() + " to int.");
logger.warning("Incorrect setting. Could not convert \"" + val + "\" from setting " + key.toString() + " to int.");
return null;
}

Expand Down Expand Up @@ -257,4 +262,5 @@ public Set<Setting> listAll() {
return new HashSet<>(em.createNamedQuery("Setting.findAll", Setting.class).getResultList());
}


}
8 changes: 5 additions & 3 deletions src/main/webapp/dataset.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
<p:tab id="dataFilesTab" title="#{bundle.files}" rendered="#{!DatasetPage.workingVersion.deaccessioned and
(empty DatasetPage.editMode or DatasetPage.editMode == 'FILE' or DatasetPage.editMode == 'CREATE')}" >
<!-- Upload -->
<ui:fragment rendered="#{DatasetPage.editMode == 'FILE' or DatasetPage.editMode == 'CREATE'}">
<ui:fragment rendered="#{DatasetPage.showFileUploadFileComponent()}">
<script>
function uploadWidgetDropMsg() {
var fileUpload = $('div[id$="fileUpload"] div.ui-fileupload-content');
Expand All @@ -469,9 +469,11 @@
});
</script>
<p class="help-block"><span class="glyphicon glyphicon-info-sign"/> <h:outputText value=" #{bundle['file.selectToAdd.tip']}" escape="false"/></p>
<p:fileUpload id="fileUpload" styleClass="margin-bottom" dragDropSupport="true" auto="true" multiple="true" rendered="#{DatasetPage.editMode == 'FILE' or DatasetPage.editMode == 'CREATE'}"

<p:fileUpload id="fileUpload" styleClass="margin-bottom" dragDropSupport="true" auto="true" multiple="true" rendered="#{DatasetPage.showFileUploadFileComponent()}"
fileUploadListener="#{DatasetPage.handleFileUpload}" process="filesTable" update="filesTable,uploadMessage" label="#{bundle['file.selectToAddBtn']}"
oncomplete="javascript:bind_bsui_components();uploadWidgetDropMsg();" onstart="javascript:uploadWidgetDropRemoveMsg();"/>
oncomplete="javascript:bind_bsui_components();uploadWidgetDropMsg();" onstart="javascript:uploadWidgetDropRemoveMsg();"
sizeLimit="#{DatasetPage.getMaxFileUploadSizeInBytes()}" invalidSizeMessage="File exceeds our limits. Please contact support. " />
<!-- Dropbox Upload -->
<div id="dropboxUploadBlock" class="panel panel-default margin-bottom" jsf:rendered="#{!empty DatasetPage.dropBoxKey}">
<div class="panel-body">
Expand Down

1 comment on commit c146f46

@raprasad
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For #2149

Please sign in to comment.