diff --git a/cli.js b/cli.js index 913aa44..3a5dad0 100755 --- a/cli.js +++ b/cli.js @@ -83,12 +83,24 @@ const handleImportDirCommand = async args => { } const handleGenerateFilesCommand = async args => { - const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args + const { dirId, filesCount, url, token, qualify, mime } = 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 +334,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/libs/createFiles.js b/libs/createFiles.js index c82f224..bf3e72e 100644 --- a/libs/createFiles.js +++ b/libs/createFiles.js @@ -1,13 +1,30 @@ +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 { + qualifications +} = require('cozy-client/dist/assets/qualifications.json') + +const addQualification = () => { + const { label } = qualifications[crypto.randomInt(0, qualifications.length)] + const qualification = Qualification.getByLabel(label) + + return { qualification } +} module.exports = async ( client, - filesCount, - dirId = 'io.cozy.files.root-dir' + { + filesCount = 10, + dirId = 'io.cozy.files.root-dir', + qualify, + mime = 'text/plain' + } ) => { const bar = new ProgressBar(':bar', { total: filesCount }) @@ -19,7 +36,8 @@ module.exports = async ( await uploadFileWithConflictStrategy(client, buffer.toString(), { name, dirId, - contentType: 'text/plain', + contentType: mime, + ...(qualify ? { metadata: addQualification() } : {}), conflictStrategy: 'rename' }) bar.tick()