-
Notifications
You must be signed in to change notification settings - Fork 1
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
Backup: Lots of fixes, refactoring and documentation #887
Merged
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d0597f6
fix(backup): Upload could crash when response body undefined
zatteo 826d3c5
fix(backup): Stop backup only if quota exceeded and not if file too l…
zatteo a17d491
refactor(backup): Create an upload service distinct from Media model
zatteo fe5822f
feat(backup): Create backups in /Photos instead of /
zatteo d332981
fix(backup): Set backup as to do on new start after killing app
zatteo b5d3084
feat(backup): Disable album management on Android
zatteo ce52f19
fix(backup): Stop considering trashed files as backuped when restoring
zatteo 82ba33b
fix(backup): Fix restoring when media at backup root path
zatteo 2a1d096
fix(backup): Try to fix "stream was reset: CANCEL" error
zatteo 84c256b
fix(backup): Allow users to rename is backup folder
zatteo a187a28
feat(backup): Send modification date when uploading
zatteo 1d9348e
feat(backup): Duplicate file and append a number when upload conflict
zatteo 9b50fba
docs(backup): Add schema about backup
zatteo 321c68d
Revert "fix(backup): Try to fix "stream was reset: CANCEL" error"
Crash-- d7ba638
fix: Add Size to upload URL
Crash-- c4aa505
chore: Add a task to generate the dependancies graph
Crash-- d0cfea6
fix: Use okhttp5.0.0-alpha.1 for react-native-background-upload
Crash-- f7cf0f8
fix: Better check for Upload error type in `uploadMedias()`
Ldoppea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,15 @@ import { IOCozyFile } from 'cozy-client/types/types' | |
const DOCTYPE_APPS = 'io.cozy.apps' | ||
const DOCTYPE_FILES = 'io.cozy.files' | ||
const BACKUP_REF = `io.cozy.apps/photos/mobile` | ||
const PHOTOS_REF = `io.cozy.apps/photos` | ||
|
||
interface Folder { | ||
_id: string | ||
attributes: { | ||
name: string | ||
path: string | ||
} | ||
} | ||
|
||
interface BackupFolder { | ||
_id: string | ||
|
@@ -119,17 +128,65 @@ const createRemoteBackupFolderWithConflictStrategy = async ( | |
} | ||
} | ||
|
||
export const getOrCreatePhotosMagicFolder = async ( | ||
Crash-- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client: CozyClient | ||
): Promise<Folder> => { | ||
const { included } = await client.collection(DOCTYPE_FILES).findReferencedBy({ | ||
_type: DOCTYPE_APPS, | ||
_id: PHOTOS_REF | ||
}) | ||
|
||
let photosMagicFolders = included as Folder[] | ||
|
||
photosMagicFolders = photosMagicFolders.filter( | ||
folder => !isInTrash(folder.attributes.path) | ||
) | ||
|
||
if (photosMagicFolders.length > 0) { | ||
return photosMagicFolders[0] | ||
} | ||
|
||
const { data: newPhotosMagicFolder } = await client | ||
.collection(DOCTYPE_FILES) | ||
.createDirectoryByPath('/Photos') | ||
Crash-- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await client.collection(DOCTYPE_FILES).addReferencesTo( | ||
{ | ||
_id: PHOTOS_REF, | ||
_type: DOCTYPE_APPS | ||
}, | ||
[ | ||
{ | ||
_id: newPhotosMagicFolder._id | ||
} | ||
] | ||
) | ||
|
||
return { | ||
_id: newPhotosMagicFolder._id, | ||
attributes: { | ||
name: newPhotosMagicFolder.name, | ||
path: newPhotosMagicFolder.path | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi ne pas retourner tout |
||
} | ||
|
||
export const createRemoteBackupFolder = async ( | ||
client: CozyClient | ||
): Promise<RemoteBackupConfig> => { | ||
const deviceName = await getDeviceName() | ||
const deviceId = await getDeviceId() | ||
|
||
const photosMagicFolder = await getOrCreatePhotosMagicFolder(client) | ||
|
||
const { | ||
data: { _id: backupRootId } | ||
} = await client | ||
.collection(DOCTYPE_FILES) | ||
.createDirectoryByPath(t('services.backup.backupRootPath')) | ||
.getDirectoryOrCreate( | ||
t('services.backup.backupRootName'), | ||
photosMagicFolder | ||
) | ||
|
||
const backupFolderAttributes = { | ||
type: 'directory', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,46 @@ | ||
/* eslint-disable promise/always-return */ | ||
import RNBackgroundUpload, { | ||
UploadOptions | ||
} from 'react-native-background-upload' | ||
|
||
import { getMimeType } from '/app/domain/backup/services/getMedias' | ||
import { Media, UploadMediaResult } from '/app/domain/backup/models/Media' | ||
import { Media } from '/app/domain/backup/models/Media' | ||
import { t } from '/locales/i18n' | ||
|
||
import CozyClient, { StackErrors, IOCozyFile } from 'cozy-client' | ||
|
||
let currentUploadId: string | undefined | ||
import CozyClient from 'cozy-client' | ||
|
||
export const getCurrentUploadId = (): string | undefined => { | ||
return currentUploadId | ||
} | ||
|
||
const setCurrentUploadId = (value: string): void => { | ||
currentUploadId = value | ||
} | ||
import { uploadFile } from '/app/domain/upload/services/upload' | ||
import { UploadResult } from '/app/domain/upload/models' | ||
|
||
export const uploadMedia = async ( | ||
client: CozyClient, | ||
uploadUrl: string, | ||
media: Media | ||
): Promise<UploadMediaResult> => { | ||
): Promise<UploadResult> => { | ||
const filepath = media.path.replace('file://', '') | ||
|
||
return new Promise((resolve, reject) => { | ||
const options = { | ||
url: uploadUrl, | ||
path: filepath, | ||
method: 'POST', | ||
type: 'raw', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': getMimeType(media), | ||
Authorization: `Bearer ${ | ||
// @ts-expect-error Type issue which will be fixed in another PR | ||
client.getStackClient().token.accessToken as string | ||
}` | ||
}, | ||
notification: { | ||
enabled: true, | ||
autoClear: true, | ||
onProgressTitle: t('services.backup.notifications.onProgressTitle'), | ||
onProgressMessage: t( | ||
'services.backup.notifications.onProgressMessage', | ||
{ | ||
filename: media.name | ||
} | ||
), | ||
onCompleteTitle: t('services.backup.notifications.onCompleteTitle'), | ||
onCompleteMessage: t( | ||
'services.backup.notifications.onCompleteMessage', | ||
{ | ||
filename: media.name | ||
} | ||
), | ||
onErrorTitle: t('services.backup.notifications.onErrorTitle'), | ||
onErrorMessage: t('services.backup.notifications.onErrorMessage', { | ||
return uploadFile({ | ||
url: uploadUrl, | ||
// @ts-expect-error Type issue which will be fixed in another PR | ||
token: client.getStackClient().token.accessToken as string, | ||
filename: media.name, | ||
filepath, | ||
mimetype: getMimeType(media), | ||
notification: { | ||
onProgressTitle: t('services.backup.notifications.onProgressTitle'), | ||
onProgressMessage: t('services.backup.notifications.onProgressMessage', { | ||
filename: media.name | ||
}), | ||
onCompleteTitle: t('services.backup.notifications.onCompleteTitle'), | ||
onCompleteMessage: t('services.backup.notifications.onCompleteMessage', { | ||
filename: media.name | ||
}), | ||
onErrorTitle: t('services.backup.notifications.onErrorTitle'), | ||
onErrorMessage: t('services.backup.notifications.onErrorMessage', { | ||
filename: media.name | ||
}), | ||
onCancelledTitle: t('services.backup.notifications.onCancelledTitle'), | ||
onCancelledMessage: t( | ||
'services.backup.notifications.onCancelledMessage', | ||
{ | ||
filename: media.name | ||
}), | ||
onCancelledTitle: t('services.backup.notifications.onCancelledTitle'), | ||
onCancelledMessage: t( | ||
'services.backup.notifications.onCancelledMessage', | ||
{ | ||
filename: media.name | ||
} | ||
) | ||
} | ||
} as UploadOptions | ||
|
||
RNBackgroundUpload.startUpload(options) | ||
.then(uploadId => { | ||
setCurrentUploadId(uploadId) | ||
|
||
RNBackgroundUpload.addListener('error', uploadId, error => { | ||
const { errors } = JSON.parse(error.responseBody) as StackErrors | ||
reject({ | ||
statusCode: error.responseCode, | ||
errors | ||
}) | ||
}) | ||
RNBackgroundUpload.addListener('cancelled', uploadId, data => { | ||
reject({ success: false, data }) | ||
}) | ||
RNBackgroundUpload.addListener('completed', uploadId, response => { | ||
const { data } = JSON.parse(response.responseBody) as { | ||
data: IOCozyFile | ||
} | ||
|
||
if (response.responseCode === 201) { | ||
resolve({ | ||
statusCode: response.responseCode, | ||
data | ||
}) | ||
} else { | ||
reject({ | ||
statusCode: response.responseCode, | ||
data | ||
}) | ||
} | ||
}) | ||
}) | ||
.catch(e => { | ||
reject(e) | ||
}) | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
|
||
export const cancelUpload = (uploadId: string): Promise<boolean> => { | ||
return RNBackgroundUpload.cancelUpload(uploadId) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error.errors content should be checked first here to prevent undefined access error if
error[0]
does not exist