Skip to content

Commit

Permalink
feat(mespapiers): Normalize File usage instead Blob
Browse files Browse the repository at this point in the history
This is the only existing case in Blob,
and it will be easier to work only with Files.
  • Loading branch information
Merkur39 committed May 30, 2023
1 parent 6a5da83 commit ec24675
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FilePicker from 'cozy-ui/transpiled/react/FilePicker'

import Scan from './Scan'
import { PaperDefinitionsStepPropTypes } from '../../../constants/PaperDefinitionsPropTypes'
import { makeBlobWithCustomAttrs } from '../../../helpers/makeBlobWithCustomAttrs'
import { makeFileWithBlob } from '../../../helpers/makeFileWithBlob'
import { useFormData } from '../../Hooks/useFormData'
import ScanResultWrapper from '../ScanResultWrapper'
import {
Expand Down Expand Up @@ -64,10 +64,10 @@ const ScanWrapper = ({ currentStep }) => {

const onChangeFilePicker = async cozyFileId => {
const blobFile = await fetchBlobFileById(client, cozyFileId)
const blobFileCustom = makeBlobWithCustomAttrs(blobFile, {
id: cozyFileId
const file = makeFileWithBlob(blobFile, {
name: cozyFileId
})
onChangeFile(blobFileCustom)
onChangeFile(file)
}

const onOpenFlagshipScan = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Check if a file is already selected in the state of the FormDataProvider
* @param {object} formData - State of the FormDataProvider
* @param {number} stepIndex - Used to know if the file is already selected for this step (Some paper have two Scan steps)
* @param {File|Blob} currentFile - File or Blob object
* @param {File} currentFile - File object
* @returns
*/
export const isFileAlreadySelected = (formData, stepIndex, currentFile) => {
Expand All @@ -17,14 +17,11 @@ export const isFileAlreadySelected = (formData, stepIndex, currentFile) => {
}

/**
* @param {File|Blob} currentFile - File or Blob object
* @param {File|Blob} currentFile - File or Blob object
* @param {File} currentFile - File object
* @param {File} file - File object
* @returns {boolean}
*/
export const isSameFile = (currentFile, file) => {
if (currentFile.constructor.name === 'Blob' && file.id === currentFile.id) {
return true
}
if (
currentFile.constructor.name === 'File' &&
file.name === currentFile.name &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ describe('isFileAlreadySelected', () => {
)
})

it('should return true if the Blob is already selected in the same step', () => {
const blob = new Blob([])
blob.id = '001'
const formData = {
data: [{ stepIndex: 1, file: blob }]
}
const currentStepIndex = 1
const currentFile = blob

expect(isFileAlreadySelected(formData, currentStepIndex, currentFile)).toBe(
true
)
})

it('should return false if the file is not already selected in the same step', () => {
const file01 = new File([], 'abc.pdf', { lastModified: 123456789 })
const formData = {
Expand All @@ -45,21 +31,6 @@ describe('isFileAlreadySelected', () => {
).toBe(false)
})

it('should return false if the Blob is not already selected in the same step', () => {
const blob = new Blob([])
blob.id = '001'
const formData = {
data: [{ stepIndex: 1, file: blob }]
}
const currentStepIndex = 1
const currentFile = new Blob([])
currentFile.id = '002'

expect(isFileAlreadySelected(formData, currentStepIndex, currentFile)).toBe(
false
)
})

it('should return false if the File is already selected in another step', () => {
const file = new File([], 'abc.pdf')
const formData = {
Expand Down

This file was deleted.

This file was deleted.

12 changes: 12 additions & 0 deletions packages/cozy-mespapiers-lib/src/helpers/makeFileWithBlob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {Blob} blobFile - Blob file
* @param {Object} attrs - Custom attributes
* @param {string} attrs.name - File name
* @returns {File}
*/
export const makeFileWithBlob = (blobFile, { name } = {}) => {
const defaultName = `${name || 'temp'}.${blobFile.type.split('/')[1]}`
const newFile = new File([blobFile], defaultName, { type: blobFile.type })

return newFile
}
25 changes: 25 additions & 0 deletions packages/cozy-mespapiers-lib/src/helpers/makeFileWithBlob.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { makeFileWithBlob } from './makeFileWithBlob'

describe('makeFileWithBlob', () => {
const blob = new Blob(['data'], { type: 'image/png' })

it('should return File constructor', () => {
const res = makeFileWithBlob(blob, {})
expect(res.constructor).toBe(File)
})

it('should set the name provided', () => {
const res = makeFileWithBlob(blob, { name: 'my file' })
expect(res.name).toBe('my file.png')
})

it('should set a default name', () => {
const res = makeFileWithBlob(blob)
expect(res.name).toBe('temp.png')
})

it('should return File with original type', () => {
const res = makeFileWithBlob(blob, {})
expect(res.type).toBe('image/png')
})
})

0 comments on commit ec24675

Please sign in to comment.