8000 feat(backup): Improve dedup algorithm to work with old backup by zatteo · Pull Request #1077 · cozy/cozy-flagship-app · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(backup): Improve dedup algorithm to work with old backup #1077

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

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
104 changes: 104 additions & 0 deletions src/app/domain/backup/services/manageRemoteBackupConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import * as manageRemoteBackupConfig from '/app/domain/backup/services/manageRemoteBackupConfig'

import type CozyClient from 'cozy-client'
import flag from 'cozy-flags'

import { Media } from '/app/domain/backup/models'
import { File } from '/app/domain/backup/queries'

jest.mock('cozy-flags')

const mockedFlag = flag as jest.MockedFunction<typeof flag>

describe('fetchRemoteBackupConfigs', () => {
const mockClientWithFindReferencedByResult = (
Expand Down Expand Up @@ -142,3 +150,99 @@ describe('fetchDeviceRemoteBackupConfig', () => {
expect(remoteBackupConfig).toBeUndefined()
})
})

describe('isFileCorrespondingToMedia', () => {
it('true when creationDateFromLibrary exists in file and corresponds to creationDate in media', () => {
const file = {
metadata: {
creationDateFromLibrary: new Date(2021, 0, 0, 10, 0, 0).getTime() // good date
},
created_at: new Date(2021, 0, 0, 9, 0, 0).getTime() // bad date (bad EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 0, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(true)
})

it('false when creationDateFromLibrary exists in file and do not correspond to creationDate in media', () => {
const file = {
metadata: {
creationDateFromLibrary: new Date(2021, 0, 0, 10, 0, 0).getTime() // good date
},
created_at: new Date(2021, 0, 0, 9, 0, 0).getTime() // bad date (bad EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 1, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(false)
})

it('true when only created_at exists in file and corresponds to creationDate in media', () => {
const file = {
created_at: new Date(2021, 0, 0, 10, 0, 0).getTime() // good date (lucky EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 0, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(true)
})

it('false when file only created_at exists and do not correspond to creationDate in media', () => {
const file = {
created_at: new Date(2021, 0, 0, 10, 0, 0).getTime() // good date (lucky EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 1, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(false)
})

it('true when only created_at exists in file and corresponds with day/minute to creationDate in media with dedup mode', () => {
mockedFlag.mockReturnValue(true)

const file = {
created_at: new Date(2021, 0, 0, 9, 0, 0).getTime() // bad date (bad EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 0, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(true)
})

it('false when only created_at exists in file and corresponds with day/minute to creationDate in media without dedup mode', () => {
mockedFlag.mockReturnValue(false)

const file = {
created_at: new Date(2021, 0, 0, 9, 0, 0).getTime() // bad date (bad EXIF)
} as unknown as File

const media = {
creationDate: new Date(2021, 0, 0, 10, 0, 0).getTime()
} as unknown as Media

expect(
manageRemoteBackupConfig.isFileCorrespondingToMedia(file, media)
).toBe(false)
})
})
46 changes: 38 additions & 8 deletions src/app/domain/backup/services/manageRemoteBackupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,45 @@ export const createRemoteBackupFolder = async (
return remoteBackupConfig
}

const isFileCorrespondingToMedia = (file: File, media: Media): boolean => {
const creationDate = new Date(
file?.metadata?.creationDateFromLibrary ?? file.created_at
)
creationDate.setMilliseconds(0)
export const isFileCorrespondingToMedia = (
file: File,
media: Media
): boolean => {
const creationDateFromLibrary = file.metadata?.creationDateFromLibrary

return (
file.name === media.name && creationDate.getTime() === media.creationDate
)
/* File come from the new backup */

if (creationDateFromLibrary) {
const creationDate = new Date(creationDateFromLibrary)
creationDate.setMilliseconds(0)

return (
file.name === media.name && creationDate.getTime() === media.creationDate
)
}

if (flag('flagship.backup.dedup')) {
const creationDate = new Date(file.created_at)
creationDate.setMilliseconds(0)

const mediaCreationDate = new Date(media.creationDate)

return (
file.name === media.name &&
creationDate.getFullYear() === mediaCreationDate.getFullYear() &&
creationDate.getMonth() === mediaCreationDate.getMonth() &&
creationDate.getDate() === mediaCreationDate.getDate() &&
creationDate.getMinutes() === mediaCreationDate.getMinutes() &&
creationDate.getSeconds() === mediaCreationDate.getSeconds()
)
} else {
const creationDate = new Date(file.created_at)
creationDate.setMilliseconds(0)

return (
file.name === media.name && creationDate.getTime() === media.creationDate
)
}
}

const formatBackupedMedia = (
Expand Down
0