Skip to content

Commit

Permalink
Added Exceptions and Static PDF upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sarinali committed Nov 2, 2023
1 parent a0a4934 commit d5e5dff
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/callhub/connect/MainController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package callhub.connect;

import callhub.connect.entities.Message;
import callhub.connect.use_case.MessageRepository;
import callhub.connect.data_access.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.web.bind.annotation.*;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/callhub/connect/controllers/FileController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package callhub.connect.controllers;
import callhub.connect.data_access.DocumentRepository;
import callhub.connect.data_access.LocalDataAccess;
import callhub.connect.entities.FileDocument;
import callhub.connect.entities.exceptions.FileLimitExceededException;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

@RestController
@RequestMapping("/files")
public class FileController {

@Autowired
DocumentRepository documentRepository;
@GetMapping("/upload")
public String uploadPDF () {
String currentDirectory = System.getProperty("user.dir");
String pathName = currentDirectory + "/src/main/java/callhub/connect/pdfs/fall2023.pdf";
FileDocument result;
// file limit exceed
try {
byte[] pdfEncoded = LocalDataAccess.convertPDFToByteArray(pathName);
Binary data = LocalDataAccess.byteArrayToBinary(pdfEncoded);
result = documentRepository.save(new FileDocument("timetable", data, LocalDate.now()));
return "Uploaded! " + result.id;
} catch (IOException e) {
return e.getMessage();
}
}

@GetMapping("/checkFiles")
public void checkPDFS() {
String currentDirectory = System.getProperty("user.dir");
File directory = new File(currentDirectory + "/src/main/java/callhub/connect");

// Check if the given path is a directory
if (directory.isDirectory()) {
File[] subdirectories = directory.listFiles(File::isDirectory);

if (subdirectories != null) {
System.out.println("Directories in the current working directory:");
for (File subdir : subdirectories) {
System.out.println(subdir.getName());
}
}
} else {
System.out.println("The current working directory is not a directory.");
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/callhub/connect/data_access/DocumentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package callhub.connect.data_access;
import callhub.connect.entities.FileDocument;
import callhub.connect.entities.Message;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.List;

public interface DocumentRepository extends MongoRepository<FileDocument, String>{
// to implement, not sure what to have for custom queries
}
31 changes: 31 additions & 0 deletions src/main/java/callhub/connect/data_access/LocalDataAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package callhub.connect.data_access;
import callhub.connect.entities.exceptions.FileLimitExceededException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.bson.types.Binary;

public class LocalDataAccess {
public static byte[] convertPDFToByteArray(String filePath) throws IOException {
File pdfFile = new File(filePath);
// 1 mb = 1024 * 1024

try (FileInputStream fileInputStream = new FileInputStream(pdfFile)) {
if (pdfFile.length() > 16 * 1024 * 1024) {
throw new FileLimitExceededException("PDF document exceeds 16MB.");
}

byte[] pdfBytes = new byte[(int) pdfFile.length()];
int bytesRead = fileInputStream.read(pdfBytes);
if (bytesRead < 0) {
throw new IOException("Failed to read PDF file");
}
return pdfBytes;
}
}

public static Binary byteArrayToBinary(byte[] byteArray) {
return new Binary(byteArray);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package callhub.connect.use_case;
package callhub.connect.data_access;
import callhub.connect.entities.Message;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/callhub/connect/entities/FileDocument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package callhub.connect.entities;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDate;
import org.bson.types.Binary;

@Document("documents")
public class FileDocument {
@Id
public String id;

public String name;
public Binary content;
public LocalDate timeStamp;

public FileDocument(String name, Binary content, LocalDate date) {
this.content = content;
this.name = name;
this.timeStamp = date;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package callhub.connect.entities.exceptions;

import java.io.IOException;

public class FileLimitExceededException extends IOException {
public FileLimitExceededException(String message) {
super(message);
}
}
Binary file added src/main/java/callhub/connect/pdfs/fall2023.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

spring.data.mongodb.uri=mongodb+srv://callhub_server:[email protected]/?retryWrites=true&w=majority&appName=AtlasApp
spring.data.mongodb.database=metadata
azure=https://connect.greenplant-1b2a73a7.eastus.azurecontainerapps.io

0 comments on commit d5e5dff

Please sign in to comment.