diff --git a/pom.xml b/pom.xml index baec0d9..ac274a5 100644 --- a/pom.xml +++ b/pom.xml @@ -18,13 +18,29 @@ <relativePath/> <!-- lookup parent from repository --> </parent> - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> - <java.version>11</java.version> - </properties> - <dependencies> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>1.18.30</version> + <scope>provided</scope> + </dependency> + + + <!--log4j2 dependencies --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j-api.version}</version> + </dependency> + + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>${slf4j.version}</version> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> @@ -53,6 +69,14 @@ <scope>test</scope> </dependency> </dependencies> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <java.version>17</java.version> + <slf4j.version>1.7.36</slf4j.version> + <log4j-api.version>2.17.1</log4j-api.version> + <disruptor.version>3.3.6</disruptor.version> + </properties> <build> <plugins> @@ -62,6 +86,4 @@ </plugin> </plugins> </build> - - </project> diff --git a/src/main/java/com/example/easynotes/controller/IndexController.java b/src/main/java/com/example/easynotes/controller/IndexController.java index f9c5ea4..7b15e60 100644 --- a/src/main/java/com/example/easynotes/controller/IndexController.java +++ b/src/main/java/com/example/easynotes/controller/IndexController.java @@ -1,16 +1,25 @@ package com.example.easynotes.controller; - - +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/") +@Slf4j public class IndexController { @GetMapping public String sayHello() { - return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint."; + try{ + log.info("/ API hit"); + return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint."; + } catch (Exception e ){ + log.error("Error:{}",e); + return "Contact Administrator"; + } + } } diff --git a/src/main/java/com/example/easynotes/controller/NoteController.java b/src/main/java/com/example/easynotes/controller/NoteController.java index 52f2ef0..51763f2 100644 --- a/src/main/java/com/example/easynotes/controller/NoteController.java +++ b/src/main/java/com/example/easynotes/controller/NoteController.java @@ -1,13 +1,20 @@ 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; /** @@ -15,11 +22,21 @@ */ @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(); @@ -59,4 +76,49 @@ public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) { 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); + } + + } + } diff --git a/src/main/java/com/example/easynotes/exception/Messages.java b/src/main/java/com/example/easynotes/exception/Messages.java new file mode 100644 index 0000000..64041b0 --- /dev/null +++ b/src/main/java/com/example/easynotes/exception/Messages.java @@ -0,0 +1,12 @@ +package com.example.easynotes.exception; + +import org.springframework.stereotype.Component; + +@Component +public class Messages { + public final String FileNotFound="File Not Found"; + public final String InvalidFileName="Invalid FileName"; + public final String SuccessFullyUploaded="File uploaded successfully"; + public final String ErrorSavingFile ="Error saving the file"; + public final String FileLimit=" File must be not empty and less than 2Gb"; +} diff --git a/src/main/java/config/AppConfig.java b/src/main/java/config/AppConfig.java new file mode 100644 index 0000000..5e462df --- /dev/null +++ b/src/main/java/config/AppConfig.java @@ -0,0 +1,20 @@ +package config; + +import com.example.easynotes.exception.Messages; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@ComponentScan +@Configuration +public class AppConfig { + private String messages; + private HttpStatus status; + @Bean + public Messages messages(){ + return new Messages(); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d357d46..2baa7dd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,4 +10,16 @@ spring.datasource.password = callicoder spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update \ No newline at end of file +spring.jpa.hibernate.ddl-auto = update + +# Loggers and Levels +logging.level.root=INFO +logging.level.org.springframework.web=DEBUG +logging.level.org.hibernate=WARN +logging.level.org.hibernate.SQL=DEBUG +logging.level.org.hibernate.exception=ERROR + +# File Logging Configuration +logging.file.path=logs/ +logging.file.name=application.log +logging.file.max-size=10MB \ No newline at end of file diff --git a/src/main/resources/notes/2023-01-05 (1).png b/src/main/resources/notes/2023-01-05 (1).png new file mode 100644 index 0000000..3d9a205 Binary files /dev/null and b/src/main/resources/notes/2023-01-05 (1).png differ diff --git a/src/main/resources/notes/SortServer2008Compat.nls b/src/main/resources/notes/SortServer2008Compat.nls new file mode 100644 index 0000000..7592532 Binary files /dev/null and b/src/main/resources/notes/SortServer2008Compat.nls differ diff --git a/src/main/resources/notes/SortWindows62.nls b/src/main/resources/notes/SortWindows62.nls new file mode 100644 index 0000000..d0ff391 Binary files /dev/null and b/src/main/resources/notes/SortWindows62.nls differ diff --git a/src/main/resources/notes/thoughts.txt b/src/main/resources/notes/thoughts.txt new file mode 100644 index 0000000..4049dd4 --- /dev/null +++ b/src/main/resources/notes/thoughts.txt @@ -0,0 +1 @@ +you need to experience the bad to enjoy the good and the good to identify the bad \ No newline at end of file