Skip to content

Commit

Permalink
fix: apply correct mimeType in iOS
Browse files Browse the repository at this point in the history
Directly in file creation so no need to update it later.
Also ensure file name and file path are decoded URI to avoid
encoded spaces (%20) or other encoded characters by the import lib
  • Loading branch information
acezard committed Nov 21, 2023
1 parent 202dfed commit d942558
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 26 deletions.
54 changes: 34 additions & 20 deletions src/app/domain/osReceive/fixtures/AndroidReceivedFile.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
export const AndroidReceivedFileFixture = {
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A23',
extension: 'pdf',
fileName: '123_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR.pdf',
mimeType: 'application/pdf',
subject: null
}
export const AndroidReceivedFileFixture = [
{
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A23',
extension: 'pdf',
fileName: '123_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR.pdf',
mimeType: 'application/pdf',
subject: null
}
]

export const AndroidReceivedFileFixture2 = {
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A32',
extension: 'pdf',
fileName: '321_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/321_foo_bar_baz_45.67EUR.pdf',
mimeType: 'application/pdf',
subject: null
}
export const AndroidReceivedFileFixtures = [
{
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A23',
extension: 'pdf',
fileName: '123_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR.pdf',
mimeType: 'application/pdf',
subject: null
},
{
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A232',
extension: 'pdf',
fileName: '123_foo_bar_baz_452.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR2.pdf',
mimeType: 'application/pdf',
subject: null
}
]
35 changes: 35 additions & 0 deletions src/app/domain/osReceive/fixtures/iOSReceivedFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export const iOSReceivedFileFixture = [
{
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan.pdf',
filePath:
'file:///Users/anc/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan.pdf',
mimeType: '.pdf',
text: null,
weblink: null
}
]

export const iOSReceivedFileFixtures = [
{
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan.pdf',
filePath:
'file:///Users/toto/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan.pdf',
mimeType: '.pdf',
text: null,
weblink: null
},
{
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan2.pdf',
filePath:
'file:///Users/toto/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan2.pdf',
mimeType: '.pdf',
text: null,
weblink: null
}
]
2 changes: 1 addition & 1 deletion src/app/domain/osReceive/models/OsReceiveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export enum OsReceiveFileStatus {

export interface OsReceiveFile {
name: string
file: ReceivedFile
file: ReceivedFile & { fromFlagship: true }
status: OsReceiveFileStatus
handledTimestamp?: number // Unix timestamp representing when the file was handled
source?: string // base64 of the file content
Expand Down
150 changes: 150 additions & 0 deletions src/app/domain/osReceive/services/OsReceiveData.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { _onReceiveFiles } from '/app/domain/osReceive/services/OsReceiveData'
import {
AndroidReceivedFileFixture,
AndroidReceivedFileFixtures
} from '/app/domain/osReceive/fixtures/AndroidReceivedFile'
import {
iOSReceivedFileFixture,
iOSReceivedFileFixtures
} from '/app/domain/osReceive/fixtures/iOSReceivedFile'
import { OsReceiveFile } from '/app/domain/osReceive/models/OsReceiveState'

describe('onReceiveFiles', () => {
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => {
// noop
})
})

it('should process Android file correctly', () => {
const result = _onReceiveFiles(AndroidReceivedFileFixture)

const expected: OsReceiveFile[] = [
{
file: {
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A23',
extension: 'pdf',
fileName: '123_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
subject: null
},
name: '123_foo_bar_baz_45.67EUR.pdf',
status: 0,
type: 'application/pdf'
}
]

expect(result).toEqual(expected)
})

it('should process Android files correctly', () => {
const result = _onReceiveFiles(AndroidReceivedFileFixtures)

const expected: OsReceiveFile[] = [
{
file: {
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A23',
extension: 'pdf',
fileName: '123_foo_bar_baz_45.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
subject: null
},
name: '123_foo_bar_baz_45.67EUR.pdf',
status: 0,
type: 'application/pdf'
},
{
file: {
contentUri:
'content://com.android.providers.downloads.documents/document/foo%1A232',
extension: 'pdf',
fileName: '123_foo_bar_baz_452.67EUR.pdf',
filePath:
'/data/user/0/io.cozy.flagship.mobile/cache/123_foo_bar_baz_45.67EUR2.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
subject: null
},
name: '123_foo_bar_baz_452.67EUR.pdf',
status: 0,
type: 'application/pdf'
}
]

expect(result).toEqual(expected)
})

it('should process iOS file correctly', () => {
const result = _onReceiveFiles(iOSReceivedFileFixture)

const expected: OsReceiveFile[] = [
{
file: {
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan.pdf',
filePath:
'file:///Users/anc/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
text: null,
weblink: null
},
name: 'Carte_Alan.pdf',
status: 0,
type: 'application/pdf'
}
]

expect(result).toEqual(expected)
})

it('should process iOS files correctly', () => {
const result = _onReceiveFiles(iOSReceivedFileFixtures)

const expected: OsReceiveFile[] = [
{
file: {
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan.pdf',
filePath:
'file:///Users/toto/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
text: null,
weblink: null
},
name: 'Carte_Alan.pdf',
status: 0,
type: 'application/pdf'
},
{
file: {
contentUri: null,
extension: 'pdf',
fileName: 'Carte_Alan2.pdf',
filePath:
'file:///Users/toto/Library/Developer/CoreSimulator/Devices/AE73D485-F700-46F2-900B-713E046D6B51/data/Containers/Shared/AppGroup/7B75CC01-AE21-4949-9E2E-0DC1F63D0C15/Carte_Alan2.pdf',
fromFlagship: true,
mimeType: 'application/pdf',
text: null,
weblink: null
},
name: 'Carte_Alan2.pdf',
status: 0,
type: 'application/pdf'
}
]

expect(result).toEqual(expected)
})
})
21 changes: 16 additions & 5 deletions src/app/domain/osReceive/services/OsReceiveData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EventEmitter } from 'events'

import OsReceiveIntent from '@mythologi/react-native-receive-sharing-intent'
import { Platform } from 'react-native'
import RNFS from 'react-native-fs'

import { EventEmitter } from 'events'

import {
OsReceiveFile,
OsReceiveFileStatus
Expand All @@ -12,6 +13,8 @@ import {
ReceivedFile,
OS_RECEIVE_PROTOCOL_NAME
} from '/app/domain/osReceive/models/ReceivedFile'
import { Media } from '/app/domain/backup/models'
import { getMimeType } from '/app/domain/backup/services/getMedias'

const getDeduplicationKey = (file: ReceivedFile): string | null => {
return file.filePath
Expand Down Expand Up @@ -42,13 +45,19 @@ const mapFilesToUploadToArray = (
filesMap: Map<string, ReceivedFile>
): OsReceiveFile[] => {
return Array.from(filesMap.values()).map(file => ({
name: file.fileName,
name: decodeURI(file.fileName),
file: {
...file,
fromFlagship: true
fromFlagship: true,
filePath: decodeURI(file.filePath),
fileName: decodeURI(file.fileName),
mimeType: getMimeType({
name: decodeURI(file.fileName),
mimeType: Platform.OS === 'android' ? file.mimeType : undefined // mimeType is not reliable on iOS
} as Media)
},
status: OsReceiveFileStatus.toUpload,
type: file.mimeType
type: 'application/pdf'
}))
}

Expand All @@ -60,6 +69,8 @@ const onReceiveFiles = (files: ReceivedFile[]): OsReceiveFile[] => {
return filesArray
}

export const _onReceiveFiles = onReceiveFiles // for testing

class FileReceiver extends EventEmitter {
private activated = false

Expand Down

0 comments on commit d942558

Please sign in to comment.