Skip to content

Commit

Permalink
fix: Correctly handle removal of unimportant cached files
Browse files Browse the repository at this point in the history
Previous implementation was incorrect because `lastOpened` prop was
stored as a string in the device's storage and so it was retrieved as
string

Then the date-fns `differenceInMonths` was failing comparing dates as
it accepts only `Date` objects and no string

This commit fixes this by parsing the persisted date into a `Date` and
by fixing related TS typings
  • Loading branch information
Ldoppea committed Dec 20, 2024
1 parent a015a0b commit ac933a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/app/domain/io.cozy.files/importantFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const cleanOldNonImportantFiles = async (
const lastOpened = offlineFile.lastOpened
if (
!importantFilesIds.includes(offlineFile.id) &&
(differenceInMonths(lastOpened, now) > NB_OF_MONTH_BEFORE_EXPIRATION ||
!lastOpened)
(!lastOpened ||
differenceInMonths(now, lastOpened) > NB_OF_MONTH_BEFORE_EXPIRATION)
) {
log.debug(
`Remove old unimportant file ${
Expand Down
55 changes: 48 additions & 7 deletions src/app/domain/io.cozy.files/offlineFilesConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatISO, parseISO } from 'date-fns'

import { CozyPersistedStorageKeys, getData, storeData } from '/libs/localStore'

export interface OfflineFile {
Expand All @@ -7,16 +9,27 @@ export interface OfflineFile {
lastOpened: Date
}

interface SerializedOfflineFile {
id: string
rev: string
path: string
lastOpened: string
}

export type OfflineFilesConfiguration = Map<string, OfflineFile>
export type SerializedOfflineFilesConfiguration = [string, OfflineFile][]
export type SerializedOfflineFilesConfiguration = [
string,
SerializedOfflineFile
][]

export const getOfflineFilesConfiguration =
async (): Promise<OfflineFilesConfiguration> => {
const filesArray = await getData<OfflineFilesConfiguration>(
CozyPersistedStorageKeys.OfflineFiles
)
const serializedFilesArray =
await getData<SerializedOfflineFilesConfiguration>(
CozyPersistedStorageKeys.OfflineFiles
)

const files = new Map(filesArray)
const files = serializedToConfiguration(serializedFilesArray)

return files
}
Expand All @@ -31,7 +44,7 @@ export const addOfflineFileToConfiguration = async (
lastOpened: new Date()
})

const filesArray = Array.from(files.entries())
const filesArray = configurationToSerialized(files)

return storeData(CozyPersistedStorageKeys.OfflineFiles, filesArray)
}
Expand All @@ -43,7 +56,7 @@ export const removeOfflineFileFromConfiguration = async (

files.delete(fileId)

const filesArray = Array.from(files.entries())
const filesArray = configurationToSerialized(files)

return storeData(CozyPersistedStorageKeys.OfflineFiles, filesArray)
}
Expand Down Expand Up @@ -73,3 +86,31 @@ export const updateLastOpened = async (fileId: string): Promise<void> => {

await addOfflineFileToConfiguration(file)
}

const serializedToConfiguration = (
serializedConfiguration: SerializedOfflineFilesConfiguration | null
): OfflineFilesConfiguration => {
const configurationArray = serializedConfiguration?.map(([key, file]) => {
const parsedFile = {
...file,
lastOpened: file.lastOpened ? parseISO(file.lastOpened) : null
}

return [key, parsedFile] as [string, OfflineFile]
})

return new Map(configurationArray)
}

const configurationToSerialized = (
configuration: OfflineFilesConfiguration
): SerializedOfflineFilesConfiguration => {
return Array.from(configuration.entries()).map(([key, file]) => {
const parsedFile = {
...file,
lastOpened: file.lastOpened ? formatISO(file.lastOpened) : ''
}

return [key, parsedFile] as [string, SerializedOfflineFile]
})
}

0 comments on commit ac933a8

Please sign in to comment.