use of com.baidu.ueditor.define.State 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;
}
use of com.baidu.ueditor.define.State 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);
}
}
use of com.baidu.ueditor.define.State 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;
}
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) {
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);
}
use of com.baidu.ueditor.define.State 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;
}
Aggregations