first commit
This commit is contained in:
112
src/main/java/com/baidu/ueditor/hunter/FileManager.java
Normal file
112
src/main/java/com/baidu/ueditor/hunter/FileManager.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package com.baidu.ueditor.hunter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import com.baidu.ueditor.PathFormat;
|
||||
import com.baidu.ueditor.define.AppInfo;
|
||||
import com.baidu.ueditor.define.BaseState;
|
||||
import com.baidu.ueditor.define.MultiState;
|
||||
import com.baidu.ueditor.define.State;
|
||||
|
||||
public class FileManager {
|
||||
|
||||
private String dir = null;
|
||||
private String rootPath = null;
|
||||
private String[] allowFiles = null;
|
||||
private int count = 0;
|
||||
|
||||
public FileManager ( Map<String, Object> conf ) {
|
||||
|
||||
this.rootPath = (String)conf.get( "rootPath" );
|
||||
this.dir = this.rootPath + (String)conf.get( "dir" );
|
||||
this.allowFiles = this.getAllowFiles( conf.get("allowFiles") );
|
||||
this.count = (Integer)conf.get( "count" );
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
private String getPath ( File file ) {
|
||||
|
||||
String path = file.getAbsolutePath();
|
||||
|
||||
return path.replace( this.rootPath, "/" );
|
||||
|
||||
}
|
||||
|
||||
private String[] getAllowFiles ( Object fileExt ) {
|
||||
|
||||
String[] exts = null;
|
||||
String ext = null;
|
||||
|
||||
if ( fileExt == null ) {
|
||||
return new String[ 0 ];
|
||||
}
|
||||
|
||||
exts = (String[])fileExt;
|
||||
|
||||
for ( int i = 0, len = exts.length; i < len; i++ ) {
|
||||
|
||||
ext = exts[ i ];
|
||||
exts[ i ] = ext.replace( ".", "" );
|
||||
|
||||
}
|
||||
|
||||
return exts;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
144
src/main/java/com/baidu/ueditor/hunter/ImageHunter.java
Normal file
144
src/main/java/com/baidu/ueditor/hunter/ImageHunter.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package com.baidu.ueditor.hunter;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baidu.ueditor.PathFormat;
|
||||
import com.baidu.ueditor.define.AppInfo;
|
||||
import com.baidu.ueditor.define.BaseState;
|
||||
import com.baidu.ueditor.define.MIMEType;
|
||||
import com.baidu.ueditor.define.MultiState;
|
||||
import com.baidu.ueditor.define.State;
|
||||
import com.baidu.ueditor.upload.StorageManager;
|
||||
|
||||
/**
|
||||
* 图片抓取器
|
||||
* @author hancong03@baidu.com
|
||||
*
|
||||
*/
|
||||
public class ImageHunter {
|
||||
|
||||
private String filename = null;
|
||||
private String savePath = null;
|
||||
private String rootPath = null;
|
||||
private List<String> allowTypes = null;
|
||||
private long maxSize = -1;
|
||||
|
||||
private List<String> filters = null;
|
||||
|
||||
public ImageHunter ( Map<String, Object> conf ) {
|
||||
|
||||
this.filename = (String)conf.get( "filename" );
|
||||
this.savePath = (String)conf.get( "savePath" );
|
||||
this.rootPath = (String)conf.get( "rootPath" );
|
||||
this.maxSize = (Long)conf.get( "maxSize" );
|
||||
this.allowTypes = Arrays.asList( (String[])conf.get( "allowFiles" ) );
|
||||
this.filters = Arrays.asList( (String[])conf.get( "filter" ) );
|
||||
|
||||
}
|
||||
|
||||
public State capture ( String[] list ) {
|
||||
|
||||
MultiState state = new MultiState( true );
|
||||
|
||||
for ( String source : list ) {
|
||||
state.addState( captureRemoteData( source ) );
|
||||
}
|
||||
|
||||
return state;
|
||||
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getPath ( String savePath, String filename, String suffix ) {
|
||||
|
||||
return PathFormat.parse( savePath + suffix, filename );
|
||||
|
||||
}
|
||||
|
||||
private boolean validHost ( String hostname ) {
|
||||
try {
|
||||
InetAddress ip = InetAddress.getByName(hostname);
|
||||
|
||||
if (ip.isSiteLocalAddress()) {
|
||||
return false;
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !filters.contains( hostname );
|
||||
|
||||
}
|
||||
|
||||
private boolean validContentState ( int code ) {
|
||||
|
||||
return HttpURLConnection.HTTP_OK == code;
|
||||
|
||||
}
|
||||
|
||||
private boolean validFileType ( String type ) {
|
||||
|
||||
return this.allowTypes.contains( type );
|
||||
|
||||
}
|
||||
|
||||
private boolean validFileSize ( int size ) {
|
||||
return size < this.maxSize;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user