Search in sources :

Example 1 with BaseState

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

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

Example 2 with BaseState

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

the class ImageHunter method captureRemoteData.

public State captureRemoteData(String urlStr) {
    HttpURLConnection connection = null;
    URL url = null;
    String suffix = null;
    try {
        url = new URL(urlStr);
        if (!validHost(url.getHost())) {
            return new BaseState(false, AppInfo.PREVENT_HOST);
        }
        connection = (HttpURLConnection) url.openConnection();
        connection.setInstanceFollowRedirects(true);
        connection.setUseCaches(true);
        if (!validContentState(connection.getResponseCode())) {
            return new BaseState(false, AppInfo.CONNECTION_ERROR);
        }
        suffix = MIMEType.getSuffix(connection.getContentType());
        if (!validFileType(suffix)) {
            return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
        }
        if (!validFileSize(connection.getContentLength())) {
            return new BaseState(false, AppInfo.MAX_SIZE);
        }
        String savePath = this.getPath(this.savePath, this.filename, suffix);
        String physicalPath = this.rootPath + savePath;
        State state = StorageManager.saveFileByInputStream(connection.getInputStream(), physicalPath);
        if (state.isSuccess()) {
            state.putInfo("url", PathFormat.format(savePath));
            state.putInfo("source", urlStr);
        }
        return state;
    } catch (Exception e) {
        return new BaseState(false, AppInfo.REMOTE_FAIL);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BaseState(com.baidu.ueditor.define.BaseState) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) MultiState(com.baidu.ueditor.define.MultiState) URL(java.net.URL)

Example 3 with BaseState

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

the class Base64Uploader method save.

public static State save(HttpServletRequest request, Map<String, Object> conf) {
    String filedName = (String) conf.get("fieldName");
    String fileName = request.getParameter(filedName);
    byte[] data = decode(fileName);
    long maxSize = ((Long) conf.get("maxSize")).longValue();
    if (!validSize(data, maxSize)) {
        return new BaseState(false, AppInfo.MAX_SIZE);
    }
    String suffix = FileType.getSuffix("JPG");
    String savePath = PathFormat.parse((String) conf.get("savePath"), (String) conf.get("filename"));
    savePath = savePath + suffix;
    String rootPath = ConfigManager.getRootPath(request, conf);
    String physicalPath = rootPath + savePath;
    State storageState = StorageManager.saveBinaryFile(data, physicalPath);
    if (storageState.isSuccess()) {
        storageState.putInfo("url", PathFormat.format(savePath));
        storageState.putInfo("type", suffix);
        storageState.putInfo("original", "");
    }
    return storageState;
}
Also used : BaseState(com.baidu.ueditor.define.BaseState) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State)

Example 4 with BaseState

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

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.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)

Example 5 with BaseState

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

the class StorageManager method saveTmpFile.

private static State saveTmpFile(File tmpFile, String path) {
    State state = null;
    File targetFile = new File(path);
    if (targetFile.canWrite()) {
        return new BaseState(false, AppInfo.PERMISSION_DENIED);
    }
    try {
        FileUtils.moveFile(tmpFile, targetFile);
    } catch (IOException e) {
        return new BaseState(false, AppInfo.IO_ERROR);
    }
    state = new BaseState(true);
    state.putInfo("size", targetFile.length());
    state.putInfo("title", targetFile.getName());
    return state;
}
Also used : BaseState(com.baidu.ueditor.define.BaseState) BaseState(com.baidu.ueditor.define.BaseState) State(com.baidu.ueditor.define.State) IOException(java.io.IOException) File(java.io.File)

Aggregations

BaseState (com.baidu.ueditor.define.BaseState)10 State (com.baidu.ueditor.define.State)9 File (java.io.File)6 IOException (java.io.IOException)5 MultiState (com.baidu.ueditor.define.MultiState)3 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 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