Search in sources :

Example 6 with BaseState

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

the class FileManager method getState.

private State getState(Object[] files) {
    MultiState state = new MultiState(true);
    BaseState fileState = null;
    File file = null;
    for (Object obj : files) {
        if (obj == null) {
            break;
        }
        file = (File) obj;
        fileState = new BaseState(true);
        fileState.putInfo("url", PathFormat.format(this.getPath(file)));
        state.addState(fileState);
    }
    return state;
}
Also used : BaseState(com.bc.pmpheep.ueditor.define.BaseState) MultiState(com.bc.pmpheep.ueditor.define.MultiState) File(java.io.File)

Example 7 with BaseState

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

the class Base64Uploader method save.

public State save(String content, Map<String, Object> conf, HttpServletRequest request) {
    byte[] data = decode(content);
    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 physicalPath = (String) conf.get("rootPath") + savePath;
    State storageState = StorageManager.saveBinaryFile(data, physicalPath);
    if (storageState.isSuccess()) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
        FileService fileService = (FileService) ctx.getBean("fileService");
        File file = FileUpload.getFileByFilePath(physicalPath);
        String picId;
        try {
            picId = fileService.saveLocalFile(file, ImageType.SYS_MESSAGE, 1L);
        } catch (IOException e) {
            return new BaseState(false, AppInfo.IO_ERROR);
        }
        storageState.putInfo("url", "/image/" + picId);
        // storageState.putInfo("url", PathFormat.format(savePath));
        storageState.putInfo("type", suffix);
        storageState.putInfo("original", "");
    }
    return storageState;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) FileService(com.bc.pmpheep.general.service.FileService) BaseState(com.bc.pmpheep.ueditor.define.BaseState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) IOException(java.io.IOException) File(java.io.File)

Example 8 with BaseState

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

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.bc.pmpheep.ueditor.define.BaseState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) IOException(java.io.IOException) File(java.io.File)

Example 9 with BaseState

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

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.bc.pmpheep.ueditor.define.BaseState) 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 10 with BaseState

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

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.bc.pmpheep.ueditor.define.BaseState) MultiState(com.bc.pmpheep.ueditor.define.MultiState) BaseState(com.bc.pmpheep.ueditor.define.BaseState) State(com.bc.pmpheep.ueditor.define.State) URL(java.net.URL) UnknownHostException(java.net.UnknownHostException)

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