Search in sources :

Example 6 with State

use of com.baidu.ueditor.define.State in project HongsCORE by ihongs.

the class StorageManager method saveBinaryFile.

public static State saveBinaryFile(byte[] data, String path) {
    File file = new File(path);
    State state = valid(file);
    if (!state.isSuccess()) {
        return state;
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bos.write(data);
        bos.flush();
        bos.close();
    } catch (IOException ioe) {
        return new BaseState(false, AppInfo.IO_ERROR);
    }
    state = new BaseState(true, file.getAbsolutePath());
    state.putInfo("size", data.length);
    state.putInfo("title", file.getName());
    return state;
}
Also used : BaseState(com.baidu.ueditor.define.BaseState) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with State

use of com.baidu.ueditor.define.State in project HongsCORE by ihongs.

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.baidu.ueditor.hunter.ImageHunter) BaseState(com.baidu.ueditor.define.BaseState) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) Uploader(com.baidu.ueditor.upload.Uploader) FileManager(com.baidu.ueditor.hunter.FileManager)

Example 8 with State

use of com.baidu.ueditor.define.State in project HongsCORE by ihongs.

the class BinaryUploader method save.

public static final 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);
        // modified by Ternence
        String rootPath = ConfigManager.getRootPath(request, conf);
        String physicalPath = rootPath + savePath;
        InputStream is = fileStream.openStream();
        State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
        is.close();
        if (storageState.isSuccess()) {
            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 : BaseState(com.baidu.ueditor.define.BaseState) InputStream(java.io.InputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 9 with State

use of com.baidu.ueditor.define.State in project HongsCORE by ihongs.

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.baidu.ueditor.define.BaseState) BufferedInputStream(java.io.BufferedInputStream) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BaseState (com.baidu.ueditor.define.BaseState)9 State (com.baidu.ueditor.define.State)9 File (java.io.File)5 IOException (java.io.IOException)5 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 MultiState (com.baidu.ueditor.define.MultiState)2 BufferedInputStream (java.io.BufferedInputStream)2 FileManager (com.baidu.ueditor.hunter.FileManager)1 ImageHunter (com.baidu.ueditor.hunter.ImageHunter)1 Uploader (com.baidu.ueditor.upload.Uploader)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1