Skip to content
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

(ccc-libs)feat: Migrating saveFiles to cozy-client #909

Merged
merged 1 commit into from
Feb 3, 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
2 changes: 1 addition & 1 deletion packages/cozy-ccc-libs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
89 changes: 47 additions & 42 deletions packages/cozy-ccc-libs/src/launcher/saveFiles.js
Original file line number Diff line number Diff line change
@@ -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')
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -194,6 +190,7 @@ async function getFileIfExists(entry, options) {
fileIdAttributes && slug && sourceAccountIdentifier
if (isReadyForFileMetadata) {
const file = await getFileFromMetaData(
client,
entry,
fileIdAttributes,
sourceAccountIdentifier,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -298,27 +300,35 @@ async function createFile(entry, options, method, fileId) {

let fileDocument
if (method === 'create') {
fileDocument = await cozy.files.create(toCreate, createFileOptions)
const clientResponse = await client.save({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ne pourrait pas remplacer tout ça par

const clientResponse = await client.save({
  _type: 'io.cozy.files',
  type: 'file',
  data: toCreate,
  ...createFileOptions,
  ...(file?._id ? {
    _id: file._id,
    _rev: file._rev
  } : {})
})

Et du coup on n'aurait plus besoin de distinguer via "method"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comme précisé sur mattermost, sera traité dans une autre carte et est référencé par #908

_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')
}

if (
options.validateFileContent &&
!(await options.validateFileContent(fileDocument))
) {
await removeFile(fileDocument)
await removeFile(client, fileDocument)
throw new Error('BAD_DOWNLOADED_FILE')
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
})
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down