001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.data;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.commons.collections4.ListUtils;
026import org.apache.commons.lang3.StringUtils;
027import org.apache.commons.text.StringTokenizer;
028
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.i18n.I18nizableTextParameter;
031import org.ametys.runtime.parameter.DefaultValidator;
032import org.ametys.runtime.parameter.ValidationResult;
033
034/**
035 * {@link DefaultValidator} extension to enforce the file 'type' or extension based on the widget params.
036 */
037public class FileValidator extends DefaultValidator
038{
039    /** The list of file extension that are consider as image */
040    protected static final List<String> IMAGE_FILTER = List.of("jpg", "jpeg", "gif", "png", "svg");
041    /** The list of file extension that are considered as image */
042    protected static final List<String> VIDEO_FILTER = List.of("ogv", "mp4", "webm", "avi", "mkv", "mov", "mpeg");
043    /** The list of file extension that are consider as sound */
044    protected static final List<String> SOUND_FILTER = List.of("mp3", "oga", "wav");
045    /** The list of file extension that are consider as multimedia */
046    protected static final List<String> MULTIMEDIA_FILTER = ListUtils.union(SOUND_FILTER, VIDEO_FILTER);
047    /** The list of file extension that are consider as pdf */
048    protected static final List<String> PDF_FILTER = List.of("pdf");
049    
050    private List<String> _allowedExtensions;
051
052    @Override
053    public void configure(Configuration configuration) throws ConfigurationException
054    {
055        super.configure(configuration);
056        
057        // Check if filtering are enabled in widget-params
058        Configuration[] params = configuration.getChild("widget-params").getChildren("param");
059        for (Configuration param : params)
060        {
061            String name = param.getAttribute("name");
062            if (StringUtils.equals("allowExtensions", name))
063            {
064                this._allowedExtensions = StringTokenizer.getCSVInstance(param.getValue()).getTokenList();
065            }
066            else if (StringUtils.equals("filter", name))
067            {
068                switch (param.getValue())
069                {
070                    case "image":
071                        this._allowedExtensions = IMAGE_FILTER;
072                        break;
073                    case "video":
074                        this._allowedExtensions = VIDEO_FILTER;
075                        break;
076                    case "multimedia":
077                        this._allowedExtensions = MULTIMEDIA_FILTER;
078                        break;
079                    case "audio":
080                        this._allowedExtensions = SOUND_FILTER;
081                        break;
082                    case "pdf":
083                        this._allowedExtensions = PDF_FILTER;
084                        break;
085                    default:
086                        throw new ConfigurationException("Unsupported file filter value '" + param.getValue() + "'.", configuration);
087                }
088            }
089        }
090    }
091    
092    @Override
093    protected ValidationResult validateSingleValue(Object value)
094    {
095        ValidationResult result = super.validateSingleValue(value);
096        
097        if (this._allowedExtensions != null && value instanceof File file)
098        {
099            result.addResult(_checkFile(file));
100        }
101        
102        return result;
103    }
104
105    private ValidationResult _checkFile(File file)
106    {
107        ValidationResult result = new ValidationResult();
108        
109        String extension = StringUtils.substringAfterLast(file.getName(), ".");
110        extension = StringUtils.lowerCase(extension);
111        if (!this._allowedExtensions.contains(extension))
112        {
113            Map<String, I18nizableTextParameter> params = new HashMap<>();
114            params.put("filename", new I18nizableText(file.getName()));
115            params.put("extension", new I18nizableText(extension));
116            params.put("allowedExtensions", new I18nizableText(this._allowedExtensions.stream().collect(Collectors.joining(", "))));
117            result.addError(new I18nizableText("plugin.cms", "PLUGINS_CMS_FILE_VALIDATOR_INVALID_EXTENSION", params));
118        }
119        
120        return result;
121    }
122    
123    @Override
124    protected ValidationResult validateArrayValues(Object[] values)
125    {
126        ValidationResult result = super.validateArrayValues(values);
127        
128        if (this._allowedExtensions != null && values instanceof File[] files)
129        {
130            for (File file : files)
131            {
132                result.addResult(_checkFile(file));
133            }
134        }
135        
136        return result;
137    }
138}