Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions src/screens/Transaction/Disputes/DisputesUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ let getFileTypeFromFileName = fileName => {
afterDotFileType
}

@get external getFileMimeType: 'a => string = "type"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since the accept filter constrains uploads to known extensions, deriving the MIME from the filename is always sufficient.
we can drop this @get external getFileMimeType


let getMimeTypeFromFileName = fileName => {
switch fileName->getFileTypeFromFileName->String.toLowerCase {
| "pdf" => "application/pdf"
| "csv" => "text/csv"
| "jpeg" | "jpg" => "image/jpeg"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good defensive coding with the isNonEmptyString check before falling back to extension-based MIME type detection. This matches the pattern used in CSV uploads elsewhere.

| "png" => "image/png"
| _ => "application/octet-stream"
}
}

// Some browsers report an empty `File.type` (e.g. for csv or unrecognised files).
// Uploading such a file leaves the multipart part without a Content-Type, which the
// backend rejects with IR_36 ("File content type not found"). Fall back to a MIME
// type derived from the file extension so a Content-Type is always present.
let getEvidenceFileContentType = (fileValue, fileName) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this too can be dropped.

let fileMimeType = fileValue->getFileMimeType
fileMimeType->isNonEmptyString ? fileMimeType : fileName->getMimeTypeFromFileName
}

let (startTimeFilterKey, endTimeFilterKey) = ("start_time", "end_time")

let getFilterTypeFromString = filterType => {
Expand Down
11 changes: 7 additions & 4 deletions src/screens/Transaction/Disputes/UploadEvidenceForDisputes.res
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module EvidenceUploadForm = {
<input
key={Int.toString(index)}
type_="file"
accept=".pdf,.csv,.img,.jpeg"
accept=".pdf,.csv,.jpeg,.jpg,.png"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good fix removing .img (invalid extension) and adding .png/.jpg. Consider extracting this to a constant if the same filter is used elsewhere, or add a comment documenting the supported file types for future maintainers.

onChange={ev => ev->handleBrowseChange(uploadEvidenceType)}
hidden=true
/>
Expand Down Expand Up @@ -83,12 +83,14 @@ module UploadDisputeEvidenceModal = {
open LogicUtils
let getURL = useGetURL()
let updateDetails = useUpdateMethod()
let acceptFile = (keyValue, fileValue) => {
let acceptFile = (keyValue, fileValue, fileName) => {
let url = getURL(~entityName=V1(DISPUTES_ATTACH_EVIDENCE), ~methodType=Put)
let formData = formData()
append(formData, "dispute_id", disputeId)
append(formData, "evidence_type", keyValue)
append(formData, "file", fileValue)
let contentType = DisputesUtils.getEvidenceFileContentType(fileValue, fileName)
let fileBlob = blob([fileValue], {"type": contentType})
appendBlob(formData, "file", fileBlob, fileName)

updateDetails(
~bodyFormData=formData,
Expand All @@ -111,7 +113,8 @@ module UploadDisputeEvidenceModal = {
let promisesOfAttachEvidence = dictToIterate->Array.map(ele => {
let jsonObject = fileUploadedDict->Dict.get(ele)->Option.getOr(JSON.Encode.null)
let fileValue = jsonObject->getDictFromJsonObject->getJsonObjectFromDict("uploadedFile")
let res = acceptFile(ele, fileValue)
let fileName = jsonObject->getDictFromJsonObject->getString("fileName", "")
let res = acceptFile(ele, fileValue, fileName)
res
})

Expand Down
Loading