diff --git a/cli.js b/cli.js index 913aa44..ea09833 100755 --- a/cli.js +++ b/cli.js @@ -83,12 +83,31 @@ const handleImportDirCommand = async args => { } const handleGenerateFilesCommand = async args => { - const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args + const { + dirId = 'io.cozy.files.root-dir', + filesCount = 10, + url, + token, + qualify, + mime = 'text/plain' + } = args + + if (qualify && !token) { + log.warn( + 'To view files in MyPapers, an app/kconnector token is required to create qualified files.\nThis token allows you to have the `cozyMetadata.createdByApp` prop on files.' + ) + } + const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [ 'io.cozy.files' ]) await ach.connect() - await ach.createFiles(parseInt(filesCount), dirId) + await ach.createFiles({ + filesCount: parseInt(filesCount), + dirId, + qualify, + mime + }) } const handleDropCommand = async args => { @@ -322,14 +341,17 @@ program program .command('generateFiles [filesCount] [dirId]') + .option('-q, --qualify', 'Add qualification to the files') + .option('-m, --mime ', 'Create files with this mime type') .description('Generates a given number of small files.') .action( - handleErrors(async function(filesCount, dirId) { + handleErrors(async function(filesCount, dirId, options) { await handleGenerateFilesCommand({ url: program.url, token: program.token, + filesCount, dirId, - filesCount + ...options }) }) ) diff --git a/data/qualificationsOptions/data.json b/data/qualificationsOptions/data.json new file mode 100644 index 0000000..1d0ba1b --- /dev/null +++ b/data/qualificationsOptions/data.json @@ -0,0 +1,21 @@ +[ + { + "qualificationLabel": "passport", + "metadata": { + "country": "France", + "number": 123456789012, + "expirationDate": "2025-01-01T12:00:00.000Z", + "noticePeriod": 90 + }, + "contact": { + "multiple": false + } + }, + { + "qualificationLabel": "family_record_book", + "metadata": {}, + "contact": { + "multiple": true + } + } +] diff --git a/libs/createFiles.js b/libs/createFiles.js index c82f224..c4a05f5 100644 --- a/libs/createFiles.js +++ b/libs/createFiles.js @@ -1,14 +1,28 @@ +const crypto = require('crypto') const ProgressBar = require('progress') const faker = require('faker') + const { uploadFileWithConflictStrategy } = require('cozy-client/dist/models/file') +const { Qualification } = require('cozy-client/dist/models/document') + +const data = require('../data/qualificationsOptions/data.json') + +const addMetadata = () => { + const randomData = data[crypto.randomInt(0, data.length)] + const { qualificationLabel, metadata } = randomData + const qualification = Qualification.getByLabel(qualificationLabel) + + return { + qualification, + ...metadata, + datetime: metadata.expirationDate ?? new Date().toISOString(), + datetimeLabel: metadata.expirationDate ?? 'datetime' + } +} -module.exports = async ( - client, - filesCount, - dirId = 'io.cozy.files.root-dir' -) => { +module.exports = async (client, { filesCount, dirId, qualify, mime }) => { const bar = new ProgressBar(':bar', { total: filesCount }) for (let i = 0; i < filesCount; i++) { @@ -19,7 +33,8 @@ module.exports = async ( await uploadFileWithConflictStrategy(client, buffer.toString(), { name, dirId, - contentType: 'text/plain', + contentType: mime, + ...(qualify ? { metadata: addMetadata() } : {}), conflictStrategy: 'rename' }) bar.tick()