Skip to content

Commit

Permalink
finished file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
BitInit committed May 1, 2019
1 parent f6325e0 commit 8ef19cf
Show file tree
Hide file tree
Showing 40 changed files with 155 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public static void notEmpty(String str, String msg){
throw new IllegalDataException(msg);
}
}

}
2 changes: 1 addition & 1 deletion distribution/conf/META-INFO/schema-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS `pnd_resource` (
`uuid` varchar(255) NOT NULL,
`gmt_create` datetime DEFAULT '2019-01-01 00:00:00',
`status` varchar(45) NOT NULL COMMENT '资源状态,表示是否完整等',
`finger_print` varchar(32) NOT NULL,
`md5` varchar(32) NOT NULL,
`gmt_modified` datetime DEFAULT '2019-01-01 00:00:00',
`link` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
Expand Down
2 changes: 1 addition & 1 deletion distribution/conf/META-INFO/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ CREATE TABLE pnd_resource (
gmt_modified timestamp NOT NULL DEFAULT '2019-01-01 00:00:00',
gmt_create timestamp NOT NULL DEFAULT '2019-01-01 00:00:00',
status varchar(45) NOT NULL,
finger_print varchar(32) NOT NULL,
md5 varchar(32) NOT NULL,
link int DEFAULT 0,
constraint pnd_resource_id_key PRIMARY KEY (id));
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void init(){
setDbMaxActive(Integer.parseInt(getEnvProperty(EnvironmentConstants.DB_MAX_ACTIVE, "20")));
setDbMaxIdle(Integer.parseInt(getEnvProperty(EnvironmentConstants.DB_MAX_IDLE, "50")));

setMaxConcurrentUploadNumbers(Integer.parseInt(getEnvProperty(EnvironmentConstants.MAX_CONCURRENT_UPLOAD_NUMBERS, "4")));
setMaxConcurrentUploadNumbers(Integer.parseInt(getEnvProperty(EnvironmentConstants.MAX_CONCURRENT_UPLOAD_NUMBERS, "1")));
setMaxFileUploadSize(getEnvProperty(EnvironmentConstants.MAX_FILE_UPLOAD_SIZE, "12MB"));
setMaxRequestSize(getEnvProperty(EnvironmentConstants.MAX_REQUEST_SIZE, "15MB"));
// 10MB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public enum FileType{
/**
* 文件类型,例如FOLDER表示文件夹
*/
DEFAULT, FOLDER, PDF, COMPRESS_FILE, VIDEO, AUDIO, PICTURE, DOC, PPT, TXT, TORRENT;
DEFAULT, FOLDER, PDF, COMPRESS_FILE,
VIDEO, AUDIO, PICTURE, DOC, PPT, TXT,
TORRENT, WEB, CODE;

@Override
public String toString() {
Expand Down Expand Up @@ -71,6 +73,7 @@ public enum ResourceState {
fileTypeAccordingToSuffix.put(".jpg", FileType.PICTURE);
fileTypeAccordingToSuffix.put(".jpeg", FileType.PICTURE);
fileTypeAccordingToSuffix.put(".gif", FileType.PICTURE);
fileTypeAccordingToSuffix.put(".ico", FileType.PICTURE);

// doc
fileTypeAccordingToSuffix.put(".doc", FileType.DOC);
Expand All @@ -85,5 +88,19 @@ public enum ResourceState {

// torrent
fileTypeAccordingToSuffix.put(".torrent", FileType.TORRENT);

// web
fileTypeAccordingToSuffix.put(".html", FileType.WEB);
fileTypeAccordingToSuffix.put(".htm", FileType.WEB);

// code
fileTypeAccordingToSuffix.put(".js", FileType.CODE);
fileTypeAccordingToSuffix.put(".json", FileType.CODE);
fileTypeAccordingToSuffix.put(".java", FileType.CODE);
fileTypeAccordingToSuffix.put(".c", FileType.CODE);
fileTypeAccordingToSuffix.put(".cpp", FileType.CODE);
fileTypeAccordingToSuffix.put(".h", FileType.CODE);
fileTypeAccordingToSuffix.put(".py", FileType.CODE);
fileTypeAccordingToSuffix.put(".go", FileType.CODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public ResponseEntity getConfig(){
}

@PostMapping("/rs/preparation")
public ResponseEntity prepareFileUpload(String clientId, String fileFingerPrint,
public ResponseEntity prepareFileUpload(String clientId, String md5,
Long size, long parentId, String fileName){
return ResponseUtils.ok(resourceService.prepareFileUpload(clientId, fileFingerPrint, size, parentId, fileName));
return ResponseUtils.ok(resourceService.prepareFileUpload(clientId, md5, size, parentId, fileName));
}

@GetMapping("/rs/fingerPrint")
Expand All @@ -51,8 +51,9 @@ public void resourceUpload(String clientId, Long resourceId, @RequestParam("file
}

@PutMapping("/rs/state")
public ResponseEntity pauseFileUpload(String clientId, Long resourceId){
resourceService.pauseResource(clientId, resourceId);
public ResponseEntity pauseFileUpload(String clientId, Long resourceId, String type){
resourceService.changeResourceState(clientId, resourceId, type);
return ResponseUtils.ok("");
}

}
37 changes: 17 additions & 20 deletions web/src/main/java/site/bitinit/pnd/web/dao/ResourceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,30 @@ public PndResource findById(long id){
return daoUtils.queryForObject(sql, RESOURCE_ROW_MAPPER, id);
}

public PndResource findByFingerPrint(String crc, SystemConstants.ResourceState state){
String sql = "select " + RESOURCE_ALL_FIELDS + " from " + RESOURCE_TABLE_NAME + " where finger_print = ? and status = ?";
logger.debug("[resource query] crc-{} {}", crc, sql);
List<PndResource> resources = daoUtils.queryForList(sql, RESOURCE_ROW_MAPPER, crc, state.name());
public PndResource findByFingerPrint(String md5, SystemConstants.ResourceState state){
String sql = "select " + RESOURCE_ALL_FIELDS + " from " + RESOURCE_TABLE_NAME + " where md5 = ? and status = ?";
logger.debug("[resource query] md5-{} {}", md5, sql);
List<PndResource> resources = daoUtils.queryForList(sql, RESOURCE_ROW_MAPPER, md5, state.name());
if (!Objects.isNull(resources) && resources.size() >= 1){
return resources.get(0);
}
return null;
}

public long save(PndResource resource){
String sql = "insert into " + RESOURCE_TABLE_NAME + " (size, path, uuid, gmt_create, gmt_modified, status, finger_print) "
String sql = "insert into " + RESOURCE_TABLE_NAME + " (size, path, uuid, gmt_create, gmt_modified, status, md5) "
+ "values (?, ?, ?, ?, ?, ?, ?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setLong(1, resource.getSize());
ps.setString(2, resource.getPath());
ps.setString(3, resource.getUuid());
ps.setString(4, CommonUtils.formatDate(new Date(resource.getGmtCreate())));
ps.setString(5, CommonUtils.formatDate(new Date(resource.getGmtModified())));
ps.setString(6, resource.getStatus());
ps.setString(7, resource.getFingerPrint());
return ps;
}
jdbcTemplate.update(con -> {
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setLong(1, resource.getSize());
ps.setString(2, resource.getPath());
ps.setString(3, resource.getUuid());
ps.setString(4, CommonUtils.formatDate(new Date(resource.getGmtCreate())));
ps.setString(5, CommonUtils.formatDate(new Date(resource.getGmtModified())));
ps.setString(6, resource.getStatus());
ps.setString(7, resource.getMd5());
return ps;
}, keyHolder);

return keyHolder.getKey().longValue();
Expand All @@ -84,7 +81,7 @@ public int updateIndex(long id, long expected, long val){
}

public static final String RESOURCE_TABLE_NAME = " pnd_resource ";
private static final String RESOURCE_ALL_FIELDS = " id, size, path, uuid, gmt_create, gmt_modified, status, finger_print, link ";
private static final String RESOURCE_ALL_FIELDS = " id, size, path, uuid, gmt_create, gmt_modified, status, md5, link ";
private static final ResourceRowMapper RESOURCE_ROW_MAPPER = new ResourceRowMapper();
static class ResourceRowMapper implements RowMapper<PndResource> {

Expand All @@ -98,7 +95,7 @@ public PndResource mapRow(ResultSet rs, int rowNum) throws SQLException {
resource.setGmtCreate(rs.getTimestamp("gmt_create").getTime());
resource.setGmtModified(rs.getTimestamp("gmt_modified").getTime());
resource.setStatus(rs.getString("status"));
resource.setFingerPrint(rs.getString("finger_print"));
resource.setMd5(rs.getString("md5"));
resource.setLink(rs.getInt("link"));
return resource;
}
Expand Down
10 changes: 5 additions & 5 deletions web/src/main/java/site/bitinit/pnd/web/model/PndResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PndResource {
private Long size;
private String path;
private String uuid;
private String fingerPrint;
private String md5;
private String status;
private Long gmtCreate;
private Long gmtModified;
Expand Down Expand Up @@ -49,12 +49,12 @@ public void setUuid(String uuid) {
this.uuid = uuid;
}

public String getFingerPrint() {
return fingerPrint;
public String getMd5() {
return md5;
}

public void setFingerPrint(String fingerPrint) {
this.fingerPrint = fingerPrint;
public void setMd5(String md5) {
this.md5 = md5;
}

public String getStatus() {
Expand Down
19 changes: 11 additions & 8 deletions web/src/main/java/site/bitinit/pnd/web/model/PndResourceState.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
*/
public class PndResourceState extends PndResource {

public static final String PAUSE = "pause";
public static final String RESUME = "resume";

private long finishedUploadBytes = 0;
private volatile boolean paused = false;
private boolean paused = false;
private String fileName;
private long parentId;
private File file;
Expand Down Expand Up @@ -41,11 +44,11 @@ public synchronized void addFinishedUploadBytes(long bytes){
this.finishedUploadBytes += bytes;
}

public boolean isPaused() {
public synchronized boolean isPaused() {
return paused;
}

public void setPaused(boolean paused) {
public synchronized void setPaused(boolean paused) {
this.paused = paused;
}

Expand Down Expand Up @@ -84,7 +87,7 @@ public void setOutputStream(OutputStream outputStream) {
private void fillData(PndResourceStateBuilder builder){
setId(builder.id);
setLink(builder.link);
setFingerPrint(builder.fingerPrint);
setMd5(builder.md5);
setGmtModified(builder.gmtModified);
setGmtCreate(builder.gmtCreate);
setPath(builder.path);
Expand Down Expand Up @@ -114,7 +117,7 @@ public static class PndResourceStateBuilder{
Long size;
String path;
String uuid;
String fingerPrint;
String md5;
SystemConstants.ResourceState status;
Long gmtCreate;
Long gmtModified;
Expand All @@ -129,7 +132,7 @@ public PndResourceStateBuilder pndResource(PndResource resource){
this.size = resource.getSize();
this.path = resource.getPath();
this.uuid = resource.getUuid();
this.fingerPrint = resource.getFingerPrint();
this.md5 = resource.getMd5();
this.status = SystemConstants.ResourceState.valueOf(resource.getStatus());
this.gmtCreate = resource.getGmtCreate();
this.gmtModified = resource.getGmtModified();
Expand Down Expand Up @@ -157,8 +160,8 @@ public PndResourceStateBuilder uuid(String uuid){
return this;
}

public PndResourceStateBuilder fingerPrint(String fingerPrint){
this.fingerPrint = fingerPrint;
public PndResourceStateBuilder md5(String md5){
this.md5 = md5;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Void call() throws Exception {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = inputStream.read(buffer);
if (bytesRead != -1){
state.getOutputStream().write(buffer);
state.getOutputStream().write(buffer, 0, bytesRead);
state.addFinishedUploadBytes(bytesRead);

resourcePersistPools.submit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public interface ResourceService {
/**
* 预处理文件上传
* @param clientId
* @param fingerPrint
* @param md5
* @param size
* @param parentId
* @param fileName
* @return
*/
Map<String, Object> prepareFileUpload(String clientId, String fingerPrint, Long size, Long parentId, String fileName);
Map<String, Object> prepareFileUpload(String clientId, String md5, Long size, Long parentId, String fileName);

/**
* 更新资源状态
Expand All @@ -56,9 +56,11 @@ public interface ResourceService {
void fileUpload(String clientId, Long resourceId, InputStream is, HttpServletRequest request);

/**
* 中断文件上传
* 中断/恢复 文件上传
* @param clientId
* @param resourceId
* @param type
*/
void pauseResource(String clientId, Long resourceId);
void changeResourceState(String clientId, Long resourceId, String type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public Map<String, Object> resourceExists(String fingerPrint) {
}

@Override
public Map<String, Object> prepareFileUpload(String clientId, String fingerPrint, Long size, Long parentId, String fileName) {
Assert.notEmpty(fingerPrint, "文件指纹不能为空");
public Map<String, Object> prepareFileUpload(String clientId, String md5, Long size, Long parentId, String fileName) {
Assert.notEmpty(md5, "文件md5不能为空");
Assert.notEmpty(clientId, "客户端id不能为空");
Assert.notNull(size, "size不能为空");
Assert.notEmpty(fileName, "文件名不能为空");

PndResource resource = new PndResource();
resource.setFingerPrint(fingerPrint);
resource.setMd5(md5);
resource.setStatus(SystemConstants.ResourceState.pending.toString());
resource.setUuid(CommonUtils.uuid());
long currentTime = System.currentTimeMillis();
Expand Down Expand Up @@ -135,7 +135,6 @@ public void updateResourceState(long resourceId, SystemConstants.ResourceState r
public void fileUpload(String clientId, Long resourceId
, InputStream is, HttpServletRequest request) {
PndResourceState state = cacheService.getResourceState(clientId, resourceId);
state.setPaused(false);

final AsyncContext context = request.startAsync();
context.addListener(new UploadAsyncListener(state));
Expand Down Expand Up @@ -175,15 +174,21 @@ public void onError(Exception e) throws Exception {
}

@Override
public void pauseResource(String clientId, Long resourceId) {
public void changeResourceState(String clientId, Long resourceId, String type) {
Assert.notEmpty(clientId, "客户端id不能为空");
Assert.notNull(resourceId);
if (!PndResourceState.PAUSE.equals(type) && !PndResourceState.RESUME.equals(type)){
throw new IllegalDataException("type must be pause or resume");
}

PndResourceState state = cacheService.getResourceState(clientId, resourceId);
if (state == null){
throw new IllegalDataException("没有 resourceId=" + resourceId + "的文件正在上传");
} else {
}
if (PndResourceState.PAUSE.equals(type)){
state.setPaused(true);
} else {
state.setPaused(false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/main/resources/META-INFO/schema-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS `pnd_resource` (
`uuid` varchar(255) NOT NULL,
`gmt_create` datetime DEFAULT '2019-01-01 00:00:00',
`status` varchar(45) NOT NULL COMMENT '资源状态,表示是否完整等',
`finger_print` varchar(32) NOT NULL,
`md5` varchar(32) NOT NULL,
`gmt_modified` datetime DEFAULT '2019-01-01 00:00:00',
`link` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
Expand Down
2 changes: 1 addition & 1 deletion web/src/main/resources/META-INFO/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ CREATE TABLE pnd_resource (
gmt_modified timestamp NOT NULL DEFAULT '2019-01-01 00:00:00',
gmt_create timestamp NOT NULL DEFAULT '2019-01-01 00:00:00',
status varchar(45) NOT NULL,
finger_print varchar(32) NOT NULL,
md5 varchar(32) NOT NULL,
link int DEFAULT 0,
constraint pnd_resource_id_key PRIMARY KEY (id));

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ef19cf

Please sign in to comment.