controllers.attachment.AttachUploadApp.java Source code

Java tutorial

Introduction

Here is the source code for controllers.attachment.AttachUploadApp.java

Source

/*
 * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved.
 * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Created on 2014-1-7
 */
package controllers.attachment;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import models.Attach;
import models.AttachOfFeedback;
import models.AttachOfIndustry;
import models.AttachOfRequire;
import models.AttachOfService;
import models.AttachOfSuggestion;
import models.User;

import org.apache.commons.lang3.StringUtils;

import play.db.jpa.Transactional;
import play.mvc.Http;
import play.mvc.Result;
import utils.Assets;
import utils.HelomeUtil;
import controllers.base.ObjectNodeResult;
import ext.config.ConfigFactory;

/**
 * 
 * @ClassName: AttachUploadApp
 * @Description: TODO(????)
 * @date 2014-1-7 ?11:19:25
 * @author YangXuelin
 * 
 */
public class AttachUploadApp extends AttachmentApp {

    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    //???
    public static final String PROOF_UPLOAD_PATH = getUploadPath();

    //??
    public static final String FEEDBACK_UPLOAD_RELATIVE_PATH = "topx/uploadfile/attachment/feedback/";
    //
    public static final String SUGGESTION_UPLOAD_RELATIVE_PATH = "topx/uploadfile/attachment/suggestion/";
    //?
    public static final String SERVICE_UPLOAD_RELATIVE_PATH = "topx/uploadfile/attachment/service/";
    //
    public static final String REQUIRE_UPLOAD_RELATIVE_PATH = "topx/uploadfile/attachment/require/";
    //
    public static final String INDUSTRY_UPLOAD_RELATIVE_PATH = "topx/uploadfile/attachment/industry/";

    //??
    public static final int PROOF_UPLOAD_COUNT = 5;

    //??
    public static final String[] PROOF_UPLOAD_FILETYPE = { ".jpg", ".jpeg", ".gif", ".png", ".bmp" };
    //
    public static final String[] REQUIRE_UPLOAD_FILETYPE = { ".jpg", ".jpeg", ".gif", ".png", ".bmp", ".xls",
            ".xlsx", ".doc", ".docx" };

    //??,??kb
    public static final long PROOF_UPLOAD_FILESIZE = 2048;
    //?URL
    public static final String PROOF_UPLOAD_URL = ConfigFactory.getString(UPLOAD_URL);

    /**
     * 
     * Direct file upload
     * @param type
     *  feedback?? suggestionservice?requireindustry
     * @return
     */
    @Transactional
    public static Result fileUpload(String type) {
        User currentUser = User.getFromSession(session());
        Http.MultipartFormData body = request().body().asMultipartFormData();
        Http.MultipartFormData.FilePart uploadFile = body.getFile("certificate");
        ObjectNodeResult result = new ObjectNodeResult();
        if (uploadFile != null) {
            if (uploadCheck(result, uploadFile, type)) {
                result = save(uploadFile.getFile(), uploadFile.getFilename(), result, currentUser, type);
            }
        } else {
            result.error("file");
        }
        return ok(result.getObjectNode().toString());
    }

    /**
     * ?
     * @param filePart
     * @return
     */
    public static ObjectNodeResult save(File sourceFile, String fileName, ObjectNodeResult result, User currentUser,
            String attachType) {
        StringBuffer sb = new StringBuffer(PROOF_UPLOAD_PATH);
        String date = dateFormat.format(new java.util.Date());
        sb.append("attachment");

        if (StringUtils.equals("suggestion", attachType)) {
            sb.append(File.separator).append("suggestion");
        } else if (StringUtils.equals("service", attachType)) {
            sb.append(File.separator).append("service");
        } else if (StringUtils.equals("require", attachType)) {
            sb.append(File.separator).append("require");
        } else if (StringUtils.equals("feedback", attachType)) {
            sb.append(File.separator).append("feedback");
        } else {
            sb.append(File.separator).append("industry");
        }
        sb.append(File.separator).append(date);

        //
        File path = new File(sb.toString());
        if (!path.exists()) {
            path.mkdirs();
        }
        //???cdn
        if (!path.exists()) {
            String str = "??";
            if (StringUtils.equals("service", attachType)) {
                str = "??";
            } else if (StringUtils.equals("require", attachType)) {
                str = "?";
            }
            return result.error(str);
        }

        String newFileName = getNewFileName(fileName);
        String uploadPath = sb.append(File.separator).append(newFileName).toString();
        File target = new File(uploadPath);
        BigDecimal sizeTemp = BigDecimal.valueOf(sourceFile.length());
        String suffixTemp = getSuffix(fileName);
        move(sourceFile, target);

        String relativePath = FEEDBACK_UPLOAD_RELATIVE_PATH;
        if (StringUtils.equals("suggestion", attachType)) {
            relativePath = SUGGESTION_UPLOAD_RELATIVE_PATH;
        } else if (StringUtils.equals("service", attachType)) {
            relativePath = SERVICE_UPLOAD_RELATIVE_PATH;
        } else if (StringUtils.equals("require", attachType)) {
            relativePath = REQUIRE_UPLOAD_RELATIVE_PATH;
        } else if (StringUtils.equals("industry", attachType)) {
            relativePath = INDUSTRY_UPLOAD_RELATIVE_PATH;
        }
        String relPath = new StringBuffer(relativePath).append(date).append("/").append(newFileName).toString();

        result.getObjectNode().put("path", relPath);
        result.getObjectNode().put("pathsource", Assets.at(relPath));
        // 
        String pathTemp = relPath;
        Attach attach = new AttachOfFeedback(fileName, pathTemp, suffixTemp, sizeTemp, new Date(),
                currentUser == null ? null : currentUser.getId(), currentUser == null ? "" : currentUser.getName(),
                false);
        if (StringUtils.equals("suggestion", attachType)) {
            attach = new AttachOfSuggestion(fileName, pathTemp, suffixTemp, sizeTemp, new Date(),
                    currentUser == null ? null : currentUser.getId(),
                    currentUser == null ? "" : currentUser.getName(), false);
        } else if (StringUtils.equals("service", attachType)) {
            attach = new AttachOfService(fileName, pathTemp, suffixTemp, sizeTemp, new Date(),
                    currentUser == null ? null : currentUser.getId(),
                    currentUser == null ? "" : currentUser.getName(), false);
        } else if (StringUtils.equals("require", attachType)) {
            attach = new AttachOfRequire(fileName, pathTemp, suffixTemp, sizeTemp, new Date(),
                    currentUser == null ? null : currentUser.getId(),
                    currentUser == null ? "" : currentUser.getName(), false);
        } else if (StringUtils.equals("industry", attachType)) {
            attach = new AttachOfIndustry(fileName, pathTemp, suffixTemp, sizeTemp, new Date(),
                    currentUser == null ? null : currentUser.getId(),
                    currentUser == null ? "" : currentUser.getName(), false);
        }
        attach.saveOrUpdate();
        result.getObjectNode().put("attachId", attach.id);
        result.getObjectNode().put("fileName", fileName);
        return result;
    }

    /**
     * ??
     * 
     * @param filename
     * @return
     */
    private static String getNewFileName(String filename) {
        String suffix = getSuffix(filename);
        return HelomeUtil.uuid() + suffix;
    }

    /**
     * ??
     * @param result
     * @param filePart
     * @return
     */
    public static boolean uploadCheck(ObjectNodeResult result, Http.MultipartFormData.FilePart filePart) {
        String filename = filePart.getFilename();
        String suffix = getSuffix(filename);
        if (suffix == null) {
            result.error("?");
            return false;
        }
        if (!checkFileTypes(suffix)) {
            result.error("???");
            return false;
        }

        long fileSize = getFileSize(filePart.getFile());
        if (fileSize <= 0) {
            result.error("???");
            return false;
        }
        //?kb
        fileSize = fileSize / 1024;
        if (fileSize > PROOF_UPLOAD_FILESIZE) {
            result.error("??[" + PROOF_UPLOAD_FILESIZE + "kb]");
            return false;
        }
        return true;
    }

    /**
     * ??
     * @param result
     * @param filePart
     * @return
     */
    public static boolean uploadCheck(ObjectNodeResult result, Http.MultipartFormData.FilePart filePart,
            String attachType) {
        String filename = filePart.getFilename();
        String suffix = getSuffix(filename);
        if (suffix == null) {
            result.error("?");
            return false;
        }
        if (!checkFileTypes(suffix, attachType)) {
            result.error("???");
            return false;
        }

        long fileSize = getFileSize(filePart.getFile());
        if (fileSize <= 0) {
            result.error("???");
            return false;
        }
        //?kb
        fileSize = fileSize / 1024;
        if (fileSize > PROOF_UPLOAD_FILESIZE) {
            result.error("??[" + PROOF_UPLOAD_FILESIZE + "kb]");
            return false;
        }
        return true;
    }

    /**
     * ?
     * @param suffix
     */
    private static boolean checkFileTypes(String suffix) {
        for (String type : PROOF_UPLOAD_FILETYPE) {
            if (type.equals(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * ?
     * @param suffix
     */
    public static boolean checkFileTypes(String suffix, String attachType) {
        String[] fileTypes = PROOF_UPLOAD_FILETYPE;
        if (StringUtils.equals("require", attachType)) {
            fileTypes = REQUIRE_UPLOAD_FILETYPE;
        }
        for (String type : fileTypes) {
            if (type.equals(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * ????
     * @param filename
     * @return
     */
    public static String getSuffix(String filename) {
        String s = HelomeUtil.trim(filename).toLowerCase();
        int index = s.lastIndexOf(".");
        if (index == -1) {
            return null;
        } else {
            return s.substring(index);
        }
    }

}