留言板

Document Library - Restrict upload file extension based on folder

Mansi Mistry,修改在8 年前。

Document Library - Restrict upload file extension based on folder

Junior Member 帖子: 31 加入日期: 14-9-30 最近的帖子
Hi,

We have a requirement to restrict the upload file extension based on the folder that we are uploading to.

For example, we have folders viz. Video and Text.
File types allowed in each folder:
Video - mp4
Text - txt, doc, docx

We also intend to show a message on the UI for the file types allowed to upload where the user selects the file to be uploaded.

I am aware of the property dl.file.extensions but that will apply to all folders and will not suffice for our requirement.

What would be the best way to achieve this? Is there a configuration that can be done or it requires customization in code?

Thanks,
Mansi
thumbnail
Andew Jardine,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder (答复)

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
I believe that this will have to be achieved using a hook. One option might be to add a custom attribute(s) to the Docuement Folder object (Control Panel > Configuraion > Custom Fields > Document Folder). Once you have done that, there will be a new form field when you edit the Document Folder with the field you specified. For example, I created a custom field called "allowed-file-types" of type "Text Box" so now when I create a new folder I can specify just the file types I want to allow.

Ned you would need to create a Portlet Struts Action hook for the action path /document_library/edit_file_entry and override the com.liferay.portlet.documentlibrary.action.EditFileEntryAction (I think). In there yoou would basically check the folder expando values and compare the "allowed extensions" with the file provided if the CMD value is ADD. If someone is trying to add a bad value then you short circuit and return.

If you want to make it extra pretty, you can include an error message (SessionErrors.add(...)) and then hook the JSP to make sure that you error message is provided to the user.
Mansi Mistry,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder

Junior Member 帖子: 31 加入日期: 14-9-30 最近的帖子
Thanks Andew for a quick reply! emoticon

Andew Jardine:


Ned you would need to create a Portlet Struts Action hook for the action path /document_library/edit_file_entry and override the com.liferay.portlet.documentlibrary.action.EditFileEntryAction (I think). In there yoou would basically check the folder expando values and compare the "allowed extensions" with the file provided if the CMD value is ADD. If someone is trying to add a bad value then you short circuit and return.

If you want to make it extra pretty, you can include an error message (SessionErrors.add(...)) and then hook the JSP to make sure that you error message is provided to the user.



Overriding the struts action as per Portlet Struts Action hook may not work.
I think, cases other than ADD may not get executed correctly since we do something like this in the processAction method


public void processAction(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse)
        throws Exception {
        ThemeDisplay themeDisplay =
            (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

      String cmd = ParamUtil.getString(actionRequest, Constants.CMD);		
		try{
		if (cmd.equals(Constants.ADD)) {
			
			//Code to check the uploaded file extension is allowed to be added to this folder.
			
		}
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, actionRequest,
            actionResponse);
    }



The Window state and any sort of redirection is not handled cleanly if we do this (or shall I say I am not aware how it needs to be done cleanly.)

Please correct me if I am wrong on this.

Can you let me know if we can put this check in a service hook of DLAppService?

Thanks,
Mansi
thumbnail
Andew Jardine,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
I suggest you try it before you decide it doen't work. I've used this approach dozens of times in all areas of the control panel, 90% of the time without fail. If you execute the original struts action, it will run as if your hook were never in place -- that is the point after all. You only want to block it under certain scenarios right?

As for all the render parameters, they will remain unchanged provided you don't do anything overriding of the render* methods.
Mansi Mistry,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder

Junior Member 帖子: 31 加入日期: 14-9-30 最近的帖子
Hey Andew, I did try this approach of custom fields using hook for service and it does solve my problem. But I can try the Action class changes also and based on that decide which approach I would take.

Thanks once again for your prompt reply. emoticon
thumbnail
Andew Jardine,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder (答复)

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Hi Mansi,

Glad to hear that you got it working. The only difference I can think of top of mind, between the service override and the struts action override is how quickly the condition is checked. The action fires first so the check ocurring in there means you don't need to go as far down the execution path before detecting the error condition and returning. Honestly, I doubt that matters emoticon -- and if you wantd to be that picky about it then you could probably also argue that usin gthe struts action hook render method to retrieve the custom attributes and send them into the JSP .. and then use a JSP hook with some custom javascript to validate the fields before the submit even happens would be the best preventative measure.

As I said though, at this stage it'd be turning into more of a technical debate. A problem solved is a problem solved. Thanks for sharing! emoticon
Mansi Mistry,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder

Junior Member 帖子: 31 加入日期: 14-9-30 最近的帖子
The Action approach worked out best for me, since it addressed adding session errors and showing correct error message on the UI.

Thank you for the help! emoticon
thumbnail
Andew Jardine,修改在8 年前。

RE: Document Library - Restrict upload file extension based on folder

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Hey Mansi --

Glad to hear it all worked out for you! and thanks for sharing the result and the other options that you tried (that also worked) in case someone else wanders along with the same challenges.
thumbnail
Achmed Tyrannus Albab,修改在4 年前。

RE: Document Library - Restrict upload file extension based on folder

Regular Member 帖子: 158 加入日期: 10-3-5 最近的帖子
Mansi Mistry - share it !: