|
@ -0,0 +1,128 @@
|
|
|
package com.yihu.hos.dfs.service;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.mongodb.gridfs.GridFSDBFile;
|
|
|
import com.mongodb.gridfs.GridFSFile;
|
|
|
import org.bson.types.ObjectId;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.data.mongodb.gridfs.GridFsOperations;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
* Mongodb file system
|
|
|
* Created by l4qiang on 2017-04-10.
|
|
|
*/
|
|
|
@Service
|
|
|
public class DFSService {
|
|
|
@Autowired
|
|
|
private GridFsOperations gridFsOperations;
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
public ResponseEntity info(String fileId) {
|
|
|
try {
|
|
|
Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
|
|
|
if (gridFSDBFile == null) {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
|
|
|
}
|
|
|
|
|
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
|
|
objectNode.put("id", gridFSDBFile.getId().toString());
|
|
|
objectNode.put("filename", gridFSDBFile.getFilename());
|
|
|
objectNode.put("contentType", gridFSDBFile.getContentType());
|
|
|
objectNode.put("length", gridFSDBFile.getLength());
|
|
|
objectNode.put("uploadDate", gridFSDBFile.getUploadDate().toString());
|
|
|
// objectNode.put("aliases", gridFSDBFile.getAliases().toString());
|
|
|
objectNode.put("md5", gridFSDBFile.getMD5());
|
|
|
String info = objectMapper.writeValueAsString(objectNode);
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
.body(info);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public ResponseEntity retrieve(String fileId, HttpServletResponse httpServletResponse) {
|
|
|
try {
|
|
|
Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
|
|
|
if (gridFSDBFile == null) {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
|
|
|
}
|
|
|
|
|
|
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
|
|
|
org.apache.commons.io.IOUtils.copy(gridFSDBFile.getInputStream(), outputStream);
|
|
|
outputStream.close();
|
|
|
|
|
|
return ResponseEntity.ok().build();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public ResponseEntity download(String fileId) {
|
|
|
Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
|
|
|
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
|
|
|
if (gridFSDBFile == null) {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
|
|
|
}
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + gridFSDBFile.getFilename() + "\"")
|
|
|
.body(Optional.of(gridFSDBFile.getInputStream()));
|
|
|
}
|
|
|
|
|
|
public ResponseEntity store(MultipartFile file) {
|
|
|
try {
|
|
|
InputStream inputStream = file.getInputStream();
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
GridFSFile gridFSFile = gridFsOperations.store(inputStream, fileName);
|
|
|
if (gridFSFile != null) {
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(gridFSFile.getId().toString());
|
|
|
}
|
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("file was fail to store");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public ResponseEntity update(String fileId, MultipartFile file) {
|
|
|
ResponseEntity delete = delete(fileId);
|
|
|
if (delete.getStatusCode() != HttpStatus.OK) {
|
|
|
return delete;
|
|
|
}
|
|
|
|
|
|
return store(file);
|
|
|
}
|
|
|
|
|
|
public ResponseEntity delete(String fileId) {
|
|
|
try {
|
|
|
Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
|
|
|
gridFsOperations.delete(query);
|
|
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body("file was deleted");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|