Skip to content

Commit

Permalink
fix: resilience evidence attachment missing (#1455)
Browse files Browse the repository at this point in the history
* fix: avoid response 500 when attachment missing

this is a first attempt, might not be the best for now

* fix: blob.text() too much call + error message only displayed

* fix: few fix for merge

* Localize missing attachment file in french

---------

Co-authored-by: Nassim Tabchiche <[email protected]>
  • Loading branch information
Axxiar and nas-tabchiche authored Feb 7, 2025
1 parent bb7260a commit afd0622
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,9 @@ 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
# get the attachment size with the correct unit
size = self.attachment.size
Expand Down
4 changes: 3 additions & 1 deletion backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3211,7 +3211,9 @@ def attachment(self, request, pk):
response = Response(status=status.HTTP_403_FORBIDDEN)
if UUID(pk) in object_ids_view:
evidence = self.get_object()
if not evidence.attachment:
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 @@ -777,6 +777,7 @@
"ShowAllNodesMessage": "Show all",
"ShowOnlyAssessable": "Only assessable",
"NoPreviewMessage": "No preview available.",
"couldNotFindAttachmentMessage": "Could not find attachment file. Please contact your administrator.",
"licenseExpiredMessage": "Your license has expired. Write operations are disabled. Please contact your administrator to renew it.",
"experimental": "Experimental",
"timeline": "Timeline",
Expand Down
3 changes: 2 additions & 1 deletion frontend/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1150,5 +1150,6 @@
"progressField": "Progrès",
"recap": "Recap",
"sectionMoved": "Section déplacée ici",
"more": "Plus"
"more": "Plus",
"couldNotFindAttachmentMessage": "Impossible de trouver la pièce jointe. Merci de contacter votre administrateur."
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
interface Attachment {
type: string;
url: string;
fileExists: 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),
fileExists: res.ok
};
};
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.fileExists}
<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;
fileExists: 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),
fileExists: res.ok
};
};
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.fileExists}
<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

0 comments on commit afd0622

Please sign in to comment.