Package yalp.data

Source Code of yalp.data.FileUpload

package yalp.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 yalp.Logger;
import yalp.data.parsing.TempFilePlugin;
import yalp.exceptions.UnexpectedException;
import yalp.libs.Files;
import yalp.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();
    }
}
TOP

Related Classes of yalp.data.FileUpload

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.