From 3c9ccc7b1c6213d433c327120648a8d4c0e26ea0 Mon Sep 17 00:00:00 2001 From: LucsT Date: Thu, 2 Feb 2023 11:11:57 +0100 Subject: [PATCH] (ccc-libs)feat: Migrating saveFiles to cozy-client --- packages/cozy-ccc-libs/package.json | 2 +- .../cozy-ccc-libs/src/launcher/saveFiles.js | 89 ++++++++++--------- yarn.lock | 18 ++-- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/packages/cozy-ccc-libs/package.json b/packages/cozy-ccc-libs/package.json index c053377d9..09a16c555 100644 --- a/packages/cozy-ccc-libs/package.json +++ b/packages/cozy-ccc-libs/package.json @@ -30,7 +30,7 @@ "dependencies": { "@cozy/minilog": "^1.0.0", "bluebird-retry": "^0.11.0", - "cozy-client": "^34.7.1", + "cozy-client": "^34.11.0", "cozy-client-js": "^0.20.0", "ky": "^0.25.1", "lodash": "^4.17.21", diff --git a/packages/cozy-ccc-libs/src/launcher/saveFiles.js b/packages/cozy-ccc-libs/src/launcher/saveFiles.js index 5154f7e47..f2eab1857 100644 --- a/packages/cozy-ccc-libs/src/launcher/saveFiles.js +++ b/packages/cozy-ccc-libs/src/launcher/saveFiles.js @@ -1,16 +1,12 @@ import Minilog from '@cozy/minilog' import get from 'lodash/get' import omit from 'lodash/omit' -import { Client } from 'cozy-client-js' import { Q } from 'cozy-client' import retry from 'bluebird-retry' -let cozy -let client - const log = Minilog('saveFiles') -const saveFiles = async (entries, folderPath, options = {}) => { +const saveFiles = async (client, entries, folderPath, options = {}) => { if (!entries || entries.length === 0) { log.warn('No file to download') } @@ -20,13 +16,10 @@ const saveFiles = async (entries, folderPath, options = {}) => { if (!options.sourceAccountIdentifier) { log.warn('There is no sourceAccountIdentifier given to saveFiles') } - if (!options.client) { + if (!client) { throw new Error('No cozy-client instance given') } - client = options.client - cozy = initCozyClientJs(client) - const saveOptions = { folderPath, fileIdAttributes: options.fileIdAttributes, @@ -70,7 +63,10 @@ const saveFiles = async (entries, folderPath, options = {}) => { entry, saveOptions ) - entry = await saveEntry(entry, { ...saveOptions, resultFolderPath }) + entry = await saveEntry(client, entry, { + ...saveOptions, + resultFolderPath + }) if (entry && entry._cozy_file_to_create) { savedFiles++ delete entry._cozy_file_to_create @@ -87,8 +83,8 @@ const saveFiles = async (entries, folderPath, options = {}) => { return savedEntries } -const saveEntry = async function (entry, options) { - let file = await getFileIfExists(entry, options) +const saveEntry = async function (client, entry, options) { + let file = await getFileIfExists(client, entry, options) let shouldReplace = false if (file) { try { @@ -121,7 +117,7 @@ const saveEntry = async function (entry, options) { interval: 1000, throw_original: true, max_tries: options.retry, - args: [entry, options, method, file ? file._id : undefined] + args: [client, entry, options, method, file ? file : undefined] }).catch(err => { if (err.message === 'BAD_DOWNLOADED_FILE') { log.warn( @@ -182,7 +178,7 @@ function noMetadataDeduplicationWarning(options) { } } -async function getFileIfExists(entry, options) { +async function getFileIfExists(client, entry, options) { const fileIdAttributes = options.fileIdAttributes const slug = options.manifest.slug const sourceAccountIdentifier = get( @@ -194,6 +190,7 @@ async function getFileIfExists(entry, options) { fileIdAttributes && slug && sourceAccountIdentifier if (isReadyForFileMetadata) { const file = await getFileFromMetaData( + client, entry, fileIdAttributes, sourceAccountIdentifier, @@ -202,16 +199,17 @@ async function getFileIfExists(entry, options) { if (!file) { // no file with correct metadata, maybe the corresponding file already exist in the default // path from a previous version of the connector - return await getFileFromPath(entry, options) + return getFileFromPath(client, entry, options) } else { return file } } else { - return await getFileFromPath(entry, options) + return getFileFromPath(client, entry, options) } } async function getFileFromMetaData( + client, entry, fileIdAttributes, sourceAccountIdentifier, @@ -256,22 +254,26 @@ async function getFileFromMetaData( } } -async function getFileFromPath(entry, options) { +async function getFileFromPath(client, entry, options) { try { log.debug(`Checking existence of ${getFilePath({ entry, options })}`) - const result = await cozy.files.statByPath(getFilePath({ entry, options })) - return result + const result = await client + .collection('io.cozy.files') + .statByPath(getFilePath({ entry, options })) + return result.data } catch (err) { log.debug(err.message) return false } } -async function createFile(entry, options, method, fileId) { - const folder = await cozy.files.statByPath(options.folderPath) +async function createFile(client, entry, options, method, file) { + const folder = await client + .collection('io.cozy.files') + .statByPath(options.folderPath) let createFileOptions = { name: getFileName(entry), - dirID: folder._id + dirId: folder.data._id } if (options.contentType) { createFileOptions.contentType = options.contentType @@ -298,19 +300,27 @@ async function createFile(entry, options, method, fileId) { let fileDocument if (method === 'create') { - fileDocument = await cozy.files.create(toCreate, createFileOptions) + const clientResponse = await client.save({ + _type: 'io.cozy.files', + type: 'file', + data: toCreate, + ...createFileOptions + }) + fileDocument = clientResponse.data } else if (method === 'updateById') { log.debug(`replacing file for ${entry.filename}`) - fileDocument = await cozy.files.updateById( - fileId, - toCreate, - createFileOptions - ) + const clientResponse = client.save({ + _id: file._id, + _rev: file._rev, + _type: 'io.cozy.files', + data: toCreate, + ...createFileOptions + }) + fileDocument = clientResponse.data } - if (options.validateFile) { if ((await options.validateFile(fileDocument)) === false) { - await removeFile(fileDocument) + await removeFile(client, fileDocument) throw new Error('BAD_DOWNLOADED_FILE') } @@ -318,7 +328,7 @@ async function createFile(entry, options, method, fileId) { options.validateFileContent && !(await options.validateFileContent(fileDocument)) ) { - await removeFile(fileDocument) + await removeFile(client, fileDocument) throw new Error('BAD_DOWNLOADED_FILE') } } @@ -385,9 +395,12 @@ const shouldReplaceFile = async function (file, entry, options) { return shouldReplaceFileFn(file, entry, options) } -const removeFile = async function (file) { - await cozy.files.trashById(file._id) - await cozy.files.destroyById(file._id) +const removeFile = async function (client, file) { + if (!client) { + log.error('No client, impossible to delete file') + } else { + await client.collection('io.cozy.files').deleteFilePermanently(file._id) + } } module.exports = saveFiles @@ -509,11 +522,3 @@ async function getOrCreateDestinationPath(entry, saveOptions) { // } return finalPath } - -function initCozyClientJs(cozyClient) { - const { uri } = cozyClient.stackClient - return new Client({ - cozyURL: uri, - token: cozyClient.stackClient.getAccessToken() - }) -} diff --git a/yarn.lock b/yarn.lock index 808fe4e3a..e21b7ca56 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5896,16 +5896,16 @@ cozy-client@^33.2.0: sift "^6.0.0" url-search-params-polyfill "^8.0.0" -cozy-client@^34.7.1: - version "34.7.1" - resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-34.7.1.tgz#d0641b43dbf67ca33a4ccae2a9a62b24784c2e1f" - integrity sha512-3KDrVOCMVnsxLcuCggM/rL13cIh+jY2MKZmHDZVM0cF8HeGMDHOJSH7cGwyL6vEoEzo6K2bxqPafJaIRqgw9Mw== +cozy-client@^34.11.0: + version "34.11.0" + resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-34.11.0.tgz#f6877b9eb9080fd1d509cdbcf59473560f282392" + integrity sha512-EENMenuECpCe1k8InnoSiI4lpJ20hbfcrBNh5KYTRhHnAzBmrxikzzxEGB5VMzX5bq48Rm94pjpqzNeD0heLEQ== dependencies: "@cozy/minilog" "1.0.0" "@types/jest" "^26.0.20" "@types/lodash" "^4.14.170" btoa "^1.2.1" - cozy-stack-client "^34.7.1" + cozy-stack-client "^34.11.0" date-fns "2.29.3" json-stable-stringify "^1.0.1" lodash "^4.17.13" @@ -5980,10 +5980,10 @@ cozy-stack-client@^33.2.0: mime "^2.4.0" qs "^6.7.0" -cozy-stack-client@^34.7.1: - version "34.7.1" - resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-34.7.1.tgz#f1b81cce2c88ba558a1b5995a91674462f3e629c" - integrity sha512-iarCc6y79HCcQKM7ITQX+y3Tvhi1SjtMGonKAkA98MOtB7qBBolU0CJd9CTLyRfzJAVT/VpXHpwBwTLQ5tyo4g== +cozy-stack-client@^34.11.0: + version "34.11.0" + resolved "https://registry.yarnpkg.com/cozy-stack-client/-/cozy-stack-client-34.11.0.tgz#2c6a6f33b0a935c4d491077d7a4f85e8d2a542b2" + integrity sha512-rl6FKFY6r8fhnihBnrsLOS0F0xdvx/2YG6Ok/Yk9OKIsm0GMAn4DIq15xv4XLpHkxSyJLHBX+pZBtdfAFq7wJg== dependencies: detect-node "^2.0.4" mime "^2.4.0"