-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathNoteController.java
124 lines (93 loc) · 4.22 KB
/
NoteController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.example.easynotes.controller;
import com.example.easynotes.exception.Messages;
import com.example.easynotes.exception.ResourceNotFoundException;
import com.example.easynotes.model.Note;
import com.example.easynotes.repository.NoteRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
* Created by rajeevkumarsingh on 27/06/17.
*/
@RestController
@RequestMapping("/api")
@Slf4j
public class NoteController {
@Autowired
NoteRepository noteRepository;
@Autowired
Messages messages;
private static final long MAX_FILE_SIZE = 2L * 1024 * 1024 * 1024; // 10GB
@GetMapping("/notes")
public List<Note> getAllNotes() {
return noteRepository.findAll();
}
@PostMapping("/notes")
public Note createNote(@Valid @RequestBody Note note) {
return noteRepository.save(note);
}
@GetMapping("/notes/{id}")
public Note getNoteById(@PathVariable(value = "id") Long noteId) {
return noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
}
@PutMapping("/notes/{id}")
public Note updateNote(@PathVariable(value = "id") Long noteId,
@Valid @RequestBody Note noteDetails) {
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
note.setTitle(noteDetails.getTitle());
note.setContent(noteDetails.getContent());
Note updatedNote = noteRepository.save(note);
return updatedNote;
}
@DeleteMapping("/notes/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
noteRepository.delete(note);
return ResponseEntity.ok().build();
}
@PostMapping("/upload/file")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
log.info(file.getOriginalFilename());
// #TODO : put this value in properties file
final String uploadLocation="C:\\Users\\harsh\\PROJECTS\\src\\main\\resources\\notes";
// 1. Validate file size (between 2000 and 2000 MB)
if (file.isEmpty() || file.getSize() > MAX_FILE_SIZE) {
log.info("File Size is :{}",file.getSize());
return ResponseEntity.badRequest().body(messages.FileLimit);
}
// 2. Extract filename directly from MultipartFile (assuming no specific format)
String fileName = file.getOriginalFilename();
log.info("File Name uploaded is {}",fileName);
// 3. Sanitize filename
if (fileName == null || fileName.isEmpty() || fileName.contains("..")) {
return new ResponseEntity<>(messages.InvalidFileName, HttpStatus.BAD_REQUEST);
}
// Perform additional processing with the sanitized filename and uploaded file content (not shown for brevity)
Path filePath=Paths.get(uploadLocation).resolve(file.getOriginalFilename());
// Create input stream from uploaded file
try (InputStream inputStream = file.getInputStream();
// Create output stream to write file directly to disk
OutputStream outputStream = new FileOutputStream(filePath.toFile())) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return new ResponseEntity<>(messages.SuccessFullyUploaded,HttpStatus.OK);
} catch(Exception e ){
log.error(messages.ErrorSavingFile);
return new ResponseEntity<>(messages.ErrorSavingFile,HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}