-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Exceptions and Static PDF upload
- Loading branch information
Showing
9 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/callhub/connect/controllers/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/main/java/callhub/connect/data_access/DocumentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/java/callhub/connect/data_access/LocalDataAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...b/connect/use_case/MessageRepository.java → ...onnect/data_access/MessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/callhub/connect/entities/exceptions/FileLimitExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |