All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.data.FileUpload Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package play.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import play.Logger;
import play.data.parsing.TempFilePlugin;
import play.exceptions.UnexpectedException;
import play.libs.Files;
import play.libs.IO;

public class FileUpload implements Upload {

    FileItem fileItem;
    File defaultFile;

    public FileUpload() {
        // Left empty
    }

    public FileUpload(FileItem fileItem) {
        this.fileItem = fileItem;
        File tmp = TempFilePlugin.createTempFolder();
        defaultFile = new File(tmp, FilenameUtils.getName(fileItem.getFieldName()) + File.separator + FilenameUtils.getName(fileItem.getName()));
        try {
            if(!defaultFile.getCanonicalPath().startsWith(tmp.getCanonicalPath())) {
                throw new IOException("Temp file try to override existing file?");
            }
            defaultFile.getParentFile().mkdirs();
            fileItem.write(defaultFile);
        } catch (Exception e) {
            throw new IllegalStateException("Error when trying to write to file " + defaultFile.getAbsolutePath(), e);
        }
    }

    public File asFile() {
        return defaultFile;
    }
    
    public File asFile(File file) {
        try {
            Files.copy(defaultFile, file);
            return file;
        } catch (Exception ex) {
            throw new UnexpectedException(ex);
        }
    }
    
    public File asFile(String name) {
        return asFile(new File(name));
    }

    public byte[] asBytes() {
        return IO.readContent(defaultFile);
    }

    public InputStream asStream() {
        try {
            return new FileInputStream(defaultFile);
        } catch (IOException ex) {
            throw new UnexpectedException(ex);
        }
    }
    
    public String getContentType() {
        return fileItem.getContentType();
    }

    public String getFileName() {
        return fileItem.getName();
    }

    public String getFieldName() {
        return fileItem.getFieldName();
    }

    public Long getSize() {
        return defaultFile.length();
    }
    
    public boolean isInMemory() {
        return fileItem.isInMemory();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy