Search in sources :

Example 1 with BaseState

use of com.bc.pmpheep.ueditor.define.BaseState in project pmph by BCSquad.

the class BinaryUploader method save.

public State save(HttpServletRequest request, Map<String, Object> conf) {
    FileItemStream fileStream = null;
    boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
    if (!ServletFileUpload.isMultipartContent(request)) {
        return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
    }
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    if (isAjaxUpload) {
        upload.setHeaderEncoding("UTF-8");
    }
    try {
        FileItemIterator iterator = upload.getItemIterator(request);
        while (iterator.hasNext()) {
            fileStream = iterator.next();
            if (!fileStream.isFormField()) {
                break;
            }
            fileStream = null;
        }
        if (fileStream == null) {
            return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
        }
        String savePath = (String) conf.get("savePath");
        String originFileName = fileStream.getName();
        String suffix = FileType.getSuffixByFilename(originFileName);
        originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
        savePath = savePath + suffix;
        long maxSize = ((Long) conf.get("maxSize")).longValue();
        if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
            return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
        }
        savePath = PathFormat.parse(savePath, originFileName);
        String physicalPath = (String) conf.get("rootPath") + savePath;
        InputStream is = fileStream.openStream();
        State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
        is.close();
        if (storageState.isSuccess()) {
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            FileService fileService = (FileService) ctx.getBean("fileService");
            File file = FileUpload.getFileByFilePath(physicalPath);
            String picId = fileService.saveLocalFile(file, ImageType.SYS_MESSAGE, 1L);
            storageState.putInfo("url", "/image/" + picId);
            // storageState.putInfo("url", PathFormat.format(savePath));
            storageState.putInfo("type", suffix);
            storageState.putInfo("original", originFileName + suffix);
        }
        return storageState;
    } catch (FileUploadException e) {
        return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
    } catch (IOException e) {
        return new BaseState(false, AppInfo.IO_ERROR);
    }
}
Also used : FileService(com.bc.pmpheep.general.service.FileService) BaseState(com.bc.pmpheep.ueditor.define.BaseState) InputStream(java.io.InputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ApplicationContext(org.springframework.context.ApplicationContext) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with BaseState

use of com.bc.pmpheep.ueditor.define.BaseState in project pmph by BCSquad.

the class ActionEnter method invoke.

public String invoke() {
    if (actionType == null || !ActionMap.mapping.containsKey(actionType)) {
        return new BaseState(false, AppInfo.INVALID_ACTION).toJSONString();
    }
    if (this.configManager == null || !this.configManager.valid()) {
        return new BaseState(false, AppInfo.CONFIG_ERROR).toJSONString();
    }
    State state = null;
    int actionCode = ActionMap.getType(this.actionType);
    Map<String, Object> conf = null;
    switch(actionCode) {
        case ActionMap.CONFIG:
            return this.configManager.getAllConfig().toString();
        case ActionMap.UPLOAD_IMAGE:
        case ActionMap.UPLOAD_SCRAWL:
        case ActionMap.UPLOAD_VIDEO:
        case ActionMap.UPLOAD_FILE:
            conf = this.configManager.getConfig(actionCode);
            state = new Uploader(request, conf).doExec();
            break;
        case ActionMap.CATCH_IMAGE:
            conf = configManager.getConfig(actionCode);
            String[] list = this.request.getParameterValues((String) conf.get("fieldName"));
            state = new ImageHunter(conf).capture(list);
            break;
        case ActionMap.LIST_IMAGE:
        case ActionMap.LIST_FILE:
            conf = configManager.getConfig(actionCode);
            int start = this.getStartIndex();
            state = new FileManager(conf).listFile(start);
            break;
    }
    return state.toJSONString();
}
Also used : ImageHunter(com.bc.pmpheep.ueditor.hunter.ImageHunter) BaseState(com.bc.pmpheep.ueditor.define.BaseState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) Uploader(com.bc.pmpheep.ueditor.upload.Uploader) FileManager(com.bc.pmpheep.ueditor.hunter.FileManager)

Example 3 with BaseState

use of com.bc.pmpheep.ueditor.define.BaseState in project pmph by BCSquad.

the class FileManager method listFile.

public State listFile(int index) {
    File dir = new File(this.dir);
    State state = null;
    if (!dir.exists()) {
        return new BaseState(false, AppInfo.NOT_EXIST);
    }
    if (!dir.isDirectory()) {
        return new BaseState(false, AppInfo.NOT_DIRECTORY);
    }
    Collection<File> list = FileUtils.listFiles(dir, this.allowFiles, true);
    if (index < 0 || index > list.size()) {
        state = new MultiState(true);
    } else {
        Object[] fileList = Arrays.copyOfRange(list.toArray(), index, index + this.count);
        state = this.getState(fileList);
    }
    state.putInfo("start", index);
    state.putInfo("total", list.size());
    return state;
}
Also used : BaseState(com.bc.pmpheep.ueditor.define.BaseState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) MultiState(com.bc.pmpheep.ueditor.define.MultiState) MultiState(com.bc.pmpheep.ueditor.define.MultiState) File(java.io.File)

Example 4 with BaseState

use of com.bc.pmpheep.ueditor.define.BaseState in project pmph by BCSquad.

the class StorageManager method saveFileByInputStream.

public static State saveFileByInputStream(InputStream is, String path) {
    State state = null;
    File tmpFile = getTmpFile();
    byte[] dataBuf = new byte[2048];
    BufferedInputStream bis = new BufferedInputStream(is, StorageManager.BUFFER_SIZE);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile), StorageManager.BUFFER_SIZE);
        int count = 0;
        while ((count = bis.read(dataBuf)) != -1) {
            bos.write(dataBuf, 0, count);
        }
        bos.flush();
        bos.close();
        state = saveTmpFile(tmpFile, path);
        if (!state.isSuccess()) {
            tmpFile.delete();
        }
        return state;
    } catch (IOException e) {
    }
    return new BaseState(false, AppInfo.IO_ERROR);
}
Also used : BaseState(com.bc.pmpheep.ueditor.define.BaseState) BufferedInputStream(java.io.BufferedInputStream) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with BaseState

use of com.bc.pmpheep.ueditor.define.BaseState in project pmph by BCSquad.

the class StorageManager method saveFileByInputStream.

public static State saveFileByInputStream(InputStream is, String path, long maxSize) {
    State state = null;
    File tmpFile = getTmpFile();
    byte[] dataBuf = new byte[2048];
    BufferedInputStream bis = new BufferedInputStream(is, StorageManager.BUFFER_SIZE);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile), StorageManager.BUFFER_SIZE);
        int count = 0;
        while ((count = bis.read(dataBuf)) != -1) {
            bos.write(dataBuf, 0, count);
        }
        bos.flush();
        bos.close();
        if (tmpFile.length() > maxSize) {
            tmpFile.delete();
            return new BaseState(false, AppInfo.MAX_SIZE);
        }
        state = saveTmpFile(tmpFile, path);
        if (!state.isSuccess()) {
            tmpFile.delete();
        }
        return state;
    } catch (IOException e) {
    }
    return new BaseState(false, AppInfo.IO_ERROR);
}
Also used : BaseState(com.bc.pmpheep.ueditor.define.BaseState) BufferedInputStream(java.io.BufferedInputStream) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BaseState (com.bc.pmpheep.ueditor.define.BaseState)10 State (com.bc.pmpheep.ueditor.define.State)9 File (java.io.File)8 IOException (java.io.IOException)6 MultiState (com.bc.pmpheep.ueditor.define.MultiState)3 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 FileService (com.bc.pmpheep.general.service.FileService)2 BufferedInputStream (java.io.BufferedInputStream)2 ApplicationContext (org.springframework.context.ApplicationContext)2 FileManager (com.bc.pmpheep.ueditor.hunter.FileManager)1 ImageHunter (com.bc.pmpheep.ueditor.hunter.ImageHunter)1 Uploader (com.bc.pmpheep.ueditor.upload.Uploader)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1