Merge pull request #2739 from paperless-ngx/fix-2738

Fix: only offer log files that exist
This commit is contained in:
shamoon 2023-02-23 15:32:29 -08:00 committed by GitHub
commit dbea2acc8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 7 deletions

View File

@ -38,14 +38,14 @@ export class LogsComponent implements OnInit, AfterViewChecked {
}
reloadLogs() {
this.logService.get(this.activeLog).subscribe(
(result) => {
this.logService.get(this.activeLog).subscribe({
next: (result) => {
this.logs = result
},
(error) => {
error: () => {
this.logs = []
}
)
},
})
}
getLogLevel(log: string) {

View File

@ -1590,10 +1590,23 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertEqual(v1.filter_rules.count(), 0)
def test_get_logs(self):
log_data = "test\ntest2\n"
with open(os.path.join(settings.LOGGING_DIR, "mail.log"), "w") as f:
f.write(log_data)
with open(os.path.join(settings.LOGGING_DIR, "paperless.log"), "w") as f:
f.write(log_data)
response = self.client.get("/api/logs/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertCountEqual(response.data, ["mail", "paperless"])
def test_get_logs_only_when_exist(self):
log_data = "test\ntest2\n"
with open(os.path.join(settings.LOGGING_DIR, "paperless.log"), "w") as f:
f.write(log_data)
response = self.client.get("/api/logs/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertCountEqual(response.data, ["paperless"])
def test_get_invalid_log(self):
response = self.client.get("/api/logs/bogus_log/")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

View File

@ -593,11 +593,14 @@ class LogViewSet(ViewSet):
log_files = ["paperless", "mail"]
def get_log_filename(self, log):
return os.path.join(settings.LOGGING_DIR, f"{log}.log")
def retrieve(self, request, pk=None, *args, **kwargs):
if pk not in self.log_files:
raise Http404()
filename = os.path.join(settings.LOGGING_DIR, f"{pk}.log")
filename = self.get_log_filename(pk)
if not os.path.isfile(filename):
raise Http404()
@ -608,7 +611,10 @@ class LogViewSet(ViewSet):
return Response(lines)
def list(self, request, *args, **kwargs):
return Response(self.log_files)
exist = [
log for log in self.log_files if os.path.isfile(self.get_log_filename(log))
]
return Response(exist)
class SavedViewViewSet(ModelViewSet, PassUserMixin):