|
|
|
|
@@ -109,60 +109,84 @@ public class FileController {
|
|
|
|
|
public ResponseEntity<?> deleteFile(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) {
|
|
|
|
|
fileService.moveToTrash(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已移至回收站"));
|
|
|
|
|
try {
|
|
|
|
|
fileService.moveToTrash(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已移至回收站"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/{id}/restore")
|
|
|
|
|
public ResponseEntity<?> restoreFile(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) {
|
|
|
|
|
fileService.restoreFile(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已还原"));
|
|
|
|
|
try {
|
|
|
|
|
fileService.restoreFile(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已还原"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}/deletePermanent")
|
|
|
|
|
public ResponseEntity<?> deletePermanently(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) {
|
|
|
|
|
fileService.deletePermanently(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已彻底删除"));
|
|
|
|
|
try {
|
|
|
|
|
fileService.deletePermanently(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已彻底删除"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/emptyTrash")
|
|
|
|
|
public ResponseEntity<?> emptyTrash(@AuthenticationPrincipal UserPrincipal principal) {
|
|
|
|
|
fileService.emptyTrash(principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已清空回收站"));
|
|
|
|
|
try {
|
|
|
|
|
fileService.emptyTrash(principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已清空回收站"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{id}/download")
|
|
|
|
|
public ResponseEntity<byte[]> downloadFile(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) throws IOException {
|
|
|
|
|
FileEntity file = fileService.getById(id);
|
|
|
|
|
if (file == null) return ResponseEntity.notFound().build();
|
|
|
|
|
byte[] content = fileService.getFileContent(file);
|
|
|
|
|
if (content == null) return ResponseEntity.notFound().build();
|
|
|
|
|
String encodedName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8).replace("+", "%20");
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedName + "\"")
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.body(content);
|
|
|
|
|
try {
|
|
|
|
|
FileEntity file = fileService.getById(id);
|
|
|
|
|
if (file == null) return ResponseEntity.notFound().build();
|
|
|
|
|
byte[] content = fileService.getFileContent(file);
|
|
|
|
|
if (content == null) return ResponseEntity.notFound().build();
|
|
|
|
|
String encodedName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8).replace("+", "%20");
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedName + "\"")
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.body(content);
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{id}/preview")
|
|
|
|
|
public ResponseEntity<byte[]> previewFile(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) throws IOException {
|
|
|
|
|
FileEntity file = fileService.getById(id);
|
|
|
|
|
if (file == null) return ResponseEntity.notFound().build();
|
|
|
|
|
byte[] content = fileService.getFileContent(file);
|
|
|
|
|
if (content == null) return ResponseEntity.notFound().build();
|
|
|
|
|
String encodedName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8).replace("+", "%20");
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + encodedName + "\"")
|
|
|
|
|
.contentType(getMediaType(file.getName()))
|
|
|
|
|
.body(content);
|
|
|
|
|
try {
|
|
|
|
|
FileEntity file = fileService.getById(id);
|
|
|
|
|
if (file == null) return ResponseEntity.notFound().build();
|
|
|
|
|
byte[] content = fileService.getFileContent(file);
|
|
|
|
|
if (content == null) return ResponseEntity.notFound().build();
|
|
|
|
|
String encodedName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8).replace("+", "%20");
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + encodedName + "\"")
|
|
|
|
|
.contentType(getMediaType(file.getName()))
|
|
|
|
|
.body(content);
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/{id}/shareFile")
|
|
|
|
|
@@ -170,18 +194,26 @@ public class FileController {
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id,
|
|
|
|
|
@RequestBody Map<String, Object> request) {
|
|
|
|
|
Long shareToUserId = Long.valueOf(request.get("userId").toString());
|
|
|
|
|
String permission = (String) request.getOrDefault("permission", "view");
|
|
|
|
|
FileShare share = fileService.shareFile(id, principal.getUserId(), shareToUserId, permission);
|
|
|
|
|
return ResponseEntity.ok(Map.of("data", share, "message", "共享成功"));
|
|
|
|
|
try {
|
|
|
|
|
Long shareToUserId = Long.valueOf(request.get("userId").toString());
|
|
|
|
|
String permission = (String) request.getOrDefault("permission", "view");
|
|
|
|
|
FileShare share = fileService.shareFile(id, principal.getUserId(), shareToUserId, permission);
|
|
|
|
|
return ResponseEntity.ok(Map.of("data", share, "message", "共享成功"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}/cancelShare")
|
|
|
|
|
public ResponseEntity<?> cancelShare(
|
|
|
|
|
@AuthenticationPrincipal UserPrincipal principal,
|
|
|
|
|
@PathVariable Long id) {
|
|
|
|
|
fileService.cancelShare(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已取消共享"));
|
|
|
|
|
try {
|
|
|
|
|
fileService.cancelShare(id, principal.getUserId());
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "已取消共享"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|