Skip to content

Conversation

@hamadiddi
Copy link

This pull request implements pagination support for the file and folder listing APIs. It introduces pageNumber, pageSize, and optional sort query parameters to allow clients to retrieve content in pages rather than loading the entire dataset at once.

Key changes include:

Updated /content, /download, /view, and root folder endpoints to support pagination.

Added response metadata: totalElements, totalPages, and currentPage.

Optional sorting for files.

Improved consistency and usability for large directories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that its smart to save the files in the db.
Assume the Server changes => then the db must synchronize with the corresponding folder.
Cloud Page intentionally manages files stored on disk — introducing a JPA layer means the filesystem and database will quickly get out of sync.


@GetMapping("/content")
public ResponseEntity<String> getFileContent(@RequestParam String path) throws IOException {
public ResponseEntity<?> getFileContent(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method mixes two very different responsibilities — fetching file content (current design) vs paginated listing from a database (new behavior).
File listings should remain filesystem-based, not database-backed.
Pagination can be implemented in-memory by slicing the list of files in FolderService without persisting them.

Comment on lines +100 to +135
public ResponseEntity<?> listFilesForDownload(
@RequestParam(required = false) String path,
@RequestParam(defaultValue = "0") int pageNumber,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(required = false) String sort) {

@GetMapping("/view")
public ResponseEntity<Resource> viewFile(@RequestParam String path) throws IOException {
var user = userService.getCurrentUser();
Path fullPath = Paths.get(user.getRootFolderPath(), path).normalize();
folderService.validatePath(user.getRootFolderPath(), fullPath);
Pageable pageable;
if (sort != null && !sort.isEmpty()) {
pageable = PageRequest.of(pageNumber, pageSize, Sort.by(sort));
} else {
pageable = PageRequest.of(pageNumber, pageSize, Sort.by("fileId").descending());
}

if (!fullPath.toFile().exists() || !fullPath.toFile().isFile()) {
throw new IllegalArgumentException("File does not exist: " + fullPath);
Page<File> filePage;

if (path == null || path.isEmpty()) {
filePage = fileRepository.findAllFiles(pageable);
} else {
filePage = fileRepository.searchAllFiles(pageable, path.toUpperCase());
}

Resource resource = new UrlResource(fullPath.toUri());
String mimeType = Files.probeContentType(fullPath);
if (mimeType == null) {
mimeType = "application/octet-stream";
Map<String, Object> response = new HashMap<>();
response.put("message", "File(s) available for download");
response.put("status", "success");
response.put("data", filePage.getContent());
response.put("totalFiles", filePage.getTotalElements());
response.put("totalPages", filePage.getTotalPages());
response.put("currentPage", filePage.getNumber());
response.put("code", 200);

return ResponseEntity.ok(response);
}


@GetMapping("/view")
public ResponseEntity<?> listFilesForView(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These endpoints currently return paginated JSON lists instead of serving actual files or file streams.
This completely changes the API semantics — /download should send the file as an attachment, /view should stream it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn’t exist unless the project’s architecture explicitly shifts from filesystem-based storage to database-indexed file metadata.

# PostgreSQL DB Connection
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.password=12345
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't push your password.

@DenizAltunkapan
Copy link
Member

@hamadiddi Thank you for your contribution

@DenizAltunkapan
Copy link
Member

@hamadiddi still working on it?

@hamadiddi
Copy link
Author

@DenizAltunkapan currently occupied, might do it later but not anytime soon.

@DenizAltunkapan DenizAltunkapan linked an issue Dec 16, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Pagination to File/Folder Listing API

2 participants