Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resilience evidence attachment missing #1455

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,8 +1719,11 @@ def filename(self):
return os.path.basename(self.attachment.name)

def get_size(self):
if not self.attachment:
if not self.attachment or not self.attachment.storage.exists(
self.attachment.name
):
return None
# print("===>GET SIZE RAHHHHHHHH", self, self.attachment, self.attachment.size)
# get the attachment size with the correct unit
size = self.attachment.size
if size < 1024:
Expand Down
6 changes: 5 additions & 1 deletion backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3131,10 +3131,14 @@ def attachment(self, request, pk):
) = RoleAssignment.get_accessible_object_ids(
Folder.get_root_folder(), request.user, Evidence
)
# print("==> ATTACHEMENT CALLED")
response = Response(status=status.HTTP_403_FORBIDDEN)
if UUID(pk) in object_ids_view:
evidence = self.get_object()
if not evidence.attachment:
# print("=============\n",evidence.attachment.name, evidence.attachment.storage.exists(evidence.attachment.name), "\n========")
if not evidence.attachment or not evidence.attachment.storage.exists(
evidence.attachment.name
):
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
content_type = mimetypes.guess_type(evidence.filename())[0]
Expand Down
1 change: 1 addition & 0 deletions frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@
"ShowAllNodesMessage": "Show all",
"ShowOnlyAssessable": "Only assessable",
"NoPreviewMessage": "No preview available.",
"couldNotFindAttachmentMessage": "Error: Could not find attachment.",
"licenseExpiredMessage": "Your license has expired. Write operations are disabled. Please contact your administrator to renew it.",
"experimental": "Experimental",
"timeline": "Timeline",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
interface Attachment {
type: string;
url: string;
couldFetch: boolean;
}

let attachment: Attachment | undefined;

const fetchAttachment = async () => {
const res = await fetch(`/evidences/${meta.id}/attachment`);
const blob = await res.blob();
return { type: blob.type, url: URL.createObjectURL(blob) };
return {
type: blob.type,
url: URL.createObjectURL(blob),
couldFetch: res.ok || JSON.parse(await blob.text()).message != 'Failed to fetch attachment'
};
};

let mounted = false;
Expand All @@ -41,6 +46,8 @@
<img src={attachment.url} alt="attachment" class="h-24" />
{:else if attachment.type === 'application/pdf'}
<embed src={attachment.url} type="application/pdf" class="h-24" />
{:else if !attachment.couldFetch}
<p class="text-error-500 font-bold">{m.couldNotFindAttachmentMessage()}</p>
{:else}
<p>{m.NoPreviewMessage()}</p>
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
interface Attachment {
type: string;
url: string;
couldFetch: boolean;
}

let attachment: Attachment | undefined = undefined;
Expand Down Expand Up @@ -49,7 +50,11 @@
const fetchAttachment = async () => {
const res = await fetch(`./${data.evidence.id}/attachment`);
const blob = await res.blob();
return { type: blob.type, url: URL.createObjectURL(blob) };
return {
type: blob.type,
url: URL.createObjectURL(blob),
couldFetch: res.ok || JSON.parse(await blob.text()).message != 'Failed to fetch attachment'
};
};
attachment = data.evidence.attachment ? await fetchAttachment() : undefined;
});
Expand Down Expand Up @@ -182,7 +187,11 @@
<embed src={attachment.url} type="application/pdf" width="100%" height="600px" />
{:else}
<div class="flex items-center justify-center space-x-4">
<p class="font-bold text-sm">{m.NoPreviewMessage()}</p>
{#if !attachment.couldFetch}
<p class="text-error-500 font-bold">{m.couldNotFindAttachmentMessage()}</p>
{:else}
<p class="font-bold text-sm">{m.NoPreviewMessage()}</p>
{/if}
</div>
{/if}
{:else}
Expand Down
Loading