76 lines
2.7 KiB
Plaintext
76 lines
2.7 KiB
Plaintext
putObject函数:
|
||
String md5 = DigestUtils.md5DigestAsHex(requestBody);// 判断文件是否存在
|
||
ObjectEntity object = this.objectRepository.findByName(filename);// 若为新文件则上传
|
||
if (object == null) {
|
||
object = new ObjectEntity();
|
||
object.setName(filename); // 文件名
|
||
object.setHash(md5); // 文件内容,用 MD5 加密内容
|
||
object.setVersion(1); // 表示第一个文件版本
|
||
object.setSize(requestBody.length);// 若上传的文件已经存在,就会在文件名中增加版本号再存储文件。
|
||
} else {
|
||
if (md5.equals(object.getHash())){
|
||
return new Result("object already exists", "10001").toString();
|
||
}
|
||
Integer version = object.getVersion()+1;// 版本号 + 1,表示文件被修改
|
||
object = new ObjectEntity();
|
||
object.setName(filename);//文件名变更
|
||
object.setHash(md5);//文件内容变更,导致 MD5 编码也变更
|
||
object.setVersion(version);//文件版本变更
|
||
object.setSize(requestBody.length);
|
||
}
|
||
this.objectRepository.save(object);
|
||
|
||
|
||
|
||
|
||
|
||
getObject函数:
|
||
ObjectEntity obj;// 如果版本号为0,就返回文件最新版本
|
||
if (version == 0) {
|
||
obj = this.objectRepository.findFirstByNameOrderByVersionDesc(filename);// 否则就返回指定版本的文件
|
||
} else {
|
||
obj = this.objectRepository.findByVersionAndName(version, filename);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
getVersion函数:
|
||
@GetMapping("/versions/{filename}")// 传入参数: 文件名、分页参数
|
||
public String getVersions(@PathVariable("filename") String filename,
|
||
@RequestParam("offset") int offset, @RequestParam("limit") int limit) {// 检查分页偏移量和页面大小是否为 0
|
||
if (offset <= 0) {
|
||
offset = 0;
|
||
}
|
||
if (limit <= 0) {
|
||
limit = 10;
|
||
}
|
||
// 从数据库中获取文件
|
||
Page<ObjectEntity> result =
|
||
this.objectRepository.findAllByNameOrderByVersionDesc(filename,
|
||
PageRequest.of(offset, limit));// 建立列表,以页的形式查找文件
|
||
Map<String, Object> map = new HashMap<>();// 建立分页信息,每一页信息包含十几条数据
|
||
map.put("total", result.getTotalElements());// 总共多少条数据,总共有几页
|
||
map.put("rows", result.getContent());// 当前为第几页
|
||
return new Result("file not exists", "10000").withData(map).toString();
|
||
}// 如果数据库中找不到任何对应的文件,则表示文件不存在
|
||
@GetMapping("/versions")
|
||
public String getVersionList(@RequestParam("offset") int offset,
|
||
@RequestParam("limit") int limit) {
|
||
if (offset <= 0) {
|
||
offset = 0;
|
||
}
|
||
if (limit <= 0) {
|
||
limit = 10;
|
||
}
|
||
// 从数据库中获取文件
|
||
Page<ObjectEntity> result =
|
||
this.objectRepository.findAll(PageRequest.of(offset, limit));// 建立列表,以页的形式查找文件
|
||
Map<String, Object> map = new HashMap<>();// 建立分页信息,每一页信息包含十几条数据
|
||
map.put("total", result.getTotalElements());// 总共多少条数据,总共有几页
|
||
map.put("rows", result.getContent());// 当前为第几页
|
||
return new Result("file not exists", "10000").withData(map).toString();
|
||
}// 如果数据库中找不到任何对应的文件,则表示文件不存在
|
||
|