Limiting Upload File Size

From EPrints Documentation
Jump to: navigation, search

There are various things that may affect the maximum size of file you can upload to EPrints. One of the most common is Apache's LimitRequestBody which by default limits the maximum file upload to 1 GiB. However, when uploading a file larger than 1 GiB with EPrints file uploader (i.e. on the Upload stage of the eprint workflow) rather than telling you this is not possible, it just sits there any never uploads the file.

For both the reason above and that more generally you may want to stop users from uploading excessively large files, you may want to restrict the maximum file size that the EPrints file uploader will permit for upload and warn users if files are too large to upload rather than leaving them wondering what went wrong. This can be done by copy 88_uploadmethod_file.js to you archive's cfg/static/javascript/auto/ (creating such a directory if it does not already exist) and editing it as follows:

1. At the start of the file add the line, which sets max_size to 1 GiB (you can set this higher or lower if you want):

max_size = 1*1024*1024*1024; //max file size allowed in bytes.

2. Around the two occurrences of this.createFile (files[i]); at an if statement to change it to the following:

if (this.checkFilesize(files[i]))
{
    this.createFile (files[i]);
}

3. After the handleFiles function block, add another function called checkFilesize with the following code:

/*
 * Check file size before uploading.
 */
checkFilesize: function(file){
    if (file.size < max_size)
    {
        return 1;
    }
    eprints.currentRepository().phrase( {'Plugin/Screen/EPrint/UploadMethod/File:filesize_too_big':{} },function(phrase){ alert (file.name+": \n"+ phrase["Plugin/Screen/EPrint/UploadMethod/File:filesize_too_big"]);  } ) ;
    return 0;
},


4. After the line var percent = json.received / json.size; add the following if block:

if (json.size > max_size)
{
    UploadMethod_cancel(uuid);
    eprints.currentRepository().phrase( {'Plugin/Screen/EPrint/UploadMethod/File:filesize_too_big':{} },function(phrase){ alert (phrase["Plugin/Screen/EPrint/UploadMethod/File:filesize_too_big"]);  } ) ;
}

After these changes you will now need to add a phrase to warn the user when the file is to large. You can add this to either an existing or by creating you own new phrases file in your archive's cfg/lang/en/phrases/ directory.

<epp:phrase id="Plugin/Screen/EPrint/UploadMethod/File:filesize_too_big">The file size exceeds the limit allowed and cannot be uploaded.</epp:phrase>