
package de.ivu.elect.jee.faces.upload;

import java.util.List;

/**
 * @deprecated
 * @see FileTypeResolver
 */
@Deprecated
public class AllowedTypesFilter {

  public AllowedTypesFilter() {
    super();
  }

  /**
   * @return Ein regulärer Ausdruck (IMMER startend und endend mit '/') zum Filtern von erlaubten Dateien, bspw. "/(\.|\/)(gif|jpe?g|png)$/".
   *
   * @see PrimeFaces-Komponente FileUpload#allowTypes
   */
  public String getAllowedTypesRegEx(List<String> fileExtensions) {
    StringBuilder result = new StringBuilder();

    if (fileExtensions == null || fileExtensions.isEmpty() || fileExtensions.stream().anyMatch("*"::equals)) {
      result.append("/.*/");
    } else {
      result.append("/(\\.|\\/)(");

      for (int i = 0; i < fileExtensions.size(); i++) {
        result.append(fileExtensions.get(i).toLowerCase());

        if (i < fileExtensions.size() - 1) {
          result.append("|");
        }
      }

      result.append(")$/");
    }

    return result.toString();
  }
}
