Skip to content

Commit

Permalink
Merge pull request #141 from LionnoiL/add-logs-endpoint
Browse files Browse the repository at this point in the history
add-logs-endpoint
  • Loading branch information
KovalBohdan-0 authored May 31, 2024
2 parents ae7bc38 + 1d68ab4 commit ecaf109
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/main/java/org/petmarket/logs/LogsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import org.petmarket.utils.annotations.responses.ApiResponseSuccessful;
import org.petmarket.utils.annotations.responses.ApiResponseUnauthorized;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -28,11 +32,22 @@ public class LogsController {
@ApiResponseUnauthorized
@ApiResponseForbidden
@GetMapping
public ResponseEntity<List<String>> getLogs() {
public ResponseEntity<Resource> getLogs() {
try {
return ResponseEntity.ok(Files.readAllLines(Paths.get(logFilePath)));
File file = new File(logFilePath);

if (!file.exists() || !file.isFile()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}

Resource resource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
headers.add(HttpHeaders.CONTENT_TYPE, Files.probeContentType(Paths.get(logFilePath)));

return new ResponseEntity<>(resource, headers, HttpStatus.OK);
} catch (IOException e) {
return ResponseEntity.status(500).body(null);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
}

0 comments on commit ecaf109

Please sign in to comment.