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

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FilenameUtils;
import org.primefaces.model.file.UploadedFile;

/**
 * This class wraps an instance of {@link org.primefaces.model.file.UploadedFile UploadedFile} and is used to provide a canonical filename for the file uploads since the filename format varies between
 * browsers.
 *
 * The normalization is done using {@link org.apache.commons.io.FilenameUtils FilenameUtils}.
 */
public class UploadedFileWithCanonicalFilename implements UploadedFile {

  private final UploadedFile uploadedFile;

  public UploadedFileWithCanonicalFilename(UploadedFile uploadedFile) {
    super();

    this.uploadedFile = uploadedFile;
  }

  @Override
  public String getFileName() {
    return FilenameUtils.getName(uploadedFile.getFileName());
  }

  @Override
  public InputStream getInputStream() throws IOException {
    return uploadedFile.getInputStream();
  }

  @Override
  public long getSize() {
    return uploadedFile.getSize();
  }

  @Override
  public byte[] getContent() {
    return uploadedFile.getContent();
  }

  @Override
  public String getContentType() {
    return uploadedFile.getContentType();
  }

  @Override
  public void write(String s) throws Exception {
    uploadedFile.write(s);
  }

  @Override
  public void delete() throws IOException {
    uploadedFile.delete();
  }
}
