This commit is contained in:
Xudong 2022-05-23 00:24:40 +08:00
parent d1339e6c94
commit ab19eeebf8
4 changed files with 124 additions and 36 deletions

View File

@ -15,9 +15,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import cn.edu.zjvtit.cloudstorage.apiservice.result.Result;
import org.springframework.web.client.RestTemplate;
@ -31,6 +33,7 @@ import java.io.OutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
@RestController
@ -50,13 +53,15 @@ public class ObjectsController {
private ObjectRepository objectRepository;
@PutMapping("/objects/{filename}")
public String putObject(@RequestBody byte[] requestBody, @RequestHeader Map<String, String> headers, @PathVariable("filename") String filename) throws IOException {
public String putObject(@RequestBody byte[] requestBody,
@RequestHeader Map<String, String> headers,
@PathVariable("filename") String filename) throws IOException {
String server = this.dataServerStore.selectRandomServer();
logger.info("put object to server: " + server);
if (server == null) {
return new Result("no server available", "10000").toString();
}
String md5 = DigestUtils.md5DigestAsHex(requestBody);
String md5 = headers.get("md5");
ObjectEntity object = this.objectRepository.findFirstByNameOrderByVersionDesc(filename);
if (object == null) {
object = new ObjectEntity();
@ -65,10 +70,10 @@ public class ObjectsController {
object.setVersion(1);
object.setSize(requestBody.length);
} else {
if (md5.equals(object.getHash())){
if (md5.equals(object.getHash())) {
return new Result("object already exists", "10001").toString();
}
Integer version = object.getVersion()+1;
Integer version = object.getVersion() + 1;
object = new ObjectEntity();
object.setName(filename);
object.setHash(md5);
@ -76,11 +81,26 @@ public class ObjectsController {
object.setSize(requestBody.length);
}
this.objectRepository.save(object);
String url = "http://" + server + "/objects/" + md5;
// 发送post请求
String apiUrl = "http://"+server;
MultiValueMap<String, String> haderWithMd5 = new HttpHeaders();;
haderWithMd5.add("md5", md5);
// 获取uuid
ResponseEntity<Result> ret = this.restTemplate.exchange(apiUrl + "/temp", HttpMethod.POST, new HttpEntity<>(null, haderWithMd5), Result.class);
String uuid = (String) Objects.requireNonNull(ret.getBody()).getData().get("uuid");
// 发送patch请求上传文件
HttpEntity<byte[]> request = new HttpEntity<>(requestBody);
this.restTemplate.exchange(url, HttpMethod.PUT, request, String.class);
return (new Result("success", "10000")).toString();
ret = this.restTemplate.exchange(apiUrl+"/temp/"+uuid, HttpMethod.PATCH, request, Result.class);
String retMd5 = (String) Objects.requireNonNull(ret.getBody()).getData().get("md5");
if (Objects.equals(md5, retMd5)){
// 将临时文件转为正式文件
this.restTemplate.exchange(apiUrl+"/temp/"+uuid, HttpMethod.PUT, new HttpEntity<>(null, haderWithMd5), Result.class);
return new Result("success", "10000").toString();
} else {
// 删除文件
this.restTemplate.exchange(apiUrl+"/temp/"+uuid, HttpMethod.DELETE, new HttpEntity<>(null, haderWithMd5), Result.class);
return new Result("md5 not match", "10002").toString();
}
}

View File

@ -7,10 +7,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import result.Result;
import cn.edu.zjvtit.cloudstorage.dataservice.result.Result;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@ -20,24 +18,6 @@ import java.nio.file.Files;
public class ObjectsController {
@Autowired
Logger logger;
@PutMapping("/objects/{filename}")
public String putObject(HttpServletRequest request, @PathVariable("filename") String filename) throws IOException {
// 存储文件
logger.info("received put object request");
File fileToSave = new File("./uploads/" + filename);
InputStream is =request.getInputStream();
FileOutputStream os = new FileOutputStream(fileToSave);
byte[] buff = new byte[1024];
int i = 0;
while ((i = is.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
os.close();
return (new Result( "success","10000")).toString();
}
@GetMapping("/objects/{filename}")
public String getObject(HttpServletResponse response, @PathVariable("filename") String filename) {
// 读取文件

View File

@ -0,0 +1,77 @@
package cn.edu.zjvtit.cloudstorage.dataservice.controller;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import cn.edu.zjvtit.cloudstorage.dataservice.result.Result;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
public class TempController {
@Autowired
Logger logger;
@PostMapping("/temp")
public String postTemp(@RequestHeader Map<String, String> requestHeader) {
String filename = UUID.randomUUID().toString();
String size = requestHeader.get("Size");
//todo check disk free space
Result ret = new Result("success", "10000");
Map data = new HashMap<String, String>();
data.put("uuid", filename);
ret.setData(data);
return ret.toString();
}
@PatchMapping("/temp/{uuid}")
public String patchTemp(@PathVariable("uuid") String uuid,
HttpServletRequest request) throws IOException {
// 存储文件
logger.info("received put object request");
File fileToSave = new File("./temp/" + uuid);
InputStream is = request.getInputStream();
FileOutputStream os = new FileOutputStream(fileToSave);
byte[] buff = new byte[1024];
int i = 0;
while ((i = is.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
os.close();
String md5 = DigestUtils.md5DigestAsHex(is);
return (new Result("success", "10000").withData((Map<String, Object>) new HashMap<>().put("md5", md5))).toString();
}
@PutMapping("/temp/{uuid}")
public String putTemp(@PathVariable("uuid") String uuid,
@RequestHeader Map<String, String> requestHeader) throws IOException {
String md5 = requestHeader.get("md5");
File fileToMove = new File("./temp/" + uuid);
File fileToSave = new File("./objects/" + md5);
if (fileToMove.renameTo(fileToSave)) {
return (new Result("success", "10000")).toString();
} else {
return (new Result("fail", "10002")).toString();
}
}
@DeleteMapping("/temp/{uuid}")
public String delTemp(@PathVariable("uuid") String uuid) throws IOException {
File fileToDelete = new File("./temp/" + uuid);
if (fileToDelete.delete()) {
return (new Result("success", "10000")).toString();
} else {
return (new Result("fail", "10002")).toString();
}
}
}

View File

@ -1,24 +1,30 @@
package result;
package cn.edu.zjvtit.cloudstorage.dataservice.result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class Result {
private String Message;
private String message;
private String code;
private Map<String, Object> data;
public Result(String msg, String code) {
this.Message = msg;
this.message = msg;
this.code = code;
}
public Result withData(Map<String, Object> data) {
this.data = data;
return this;
}
public String getMessage() {
return Message;
return message;
}
public void setMessage(String message) {
this.Message = message;
this.message = message;
}
public String getCode() {
@ -28,7 +34,12 @@ public class Result {
public void setCode(String code) {
this.code = code;
}
public void setData(Map data) {
this.data = data;
}
public Map<String,Object> getData() {
return this.data;
}
public String toString(){
ObjectMapper mapper = new ObjectMapper();
try {