Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): Add examples of scripts adding and deleting files
- Loading branch information
1 parent
f681dd4
commit b525047
Showing
2 changed files
with
150 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { createClientInteractive } = require('cozy-client') | ||
const { | ||
schema, | ||
createFolderWithFilesBulk, | ||
deleteFile | ||
} = require('./helpers/file') | ||
const { ArgumentParser } = require('argparse') | ||
|
||
global.fetch = require('node-fetch') // in the browser we have native fetch | ||
|
||
/** | ||
* Several utilities for files management: | ||
* - Create a folder with files | ||
* - Delete it | ||
* - Delete all the files without path that are not trashed | ||
* - Get all the files | ||
* | ||
*/ | ||
const main = async () => { | ||
const parser = new ArgumentParser() | ||
parser.addArgument('--url', { defaultValue: 'http://cozy.localhost:8080' }) | ||
const args = parser.parseArgs() | ||
|
||
const client = await createClientInteractive({ | ||
scope: ['io.cozy.files'], | ||
uri: args.url, | ||
schema, | ||
oauth: { | ||
softwareID: 'io.cozy.client.cli' | ||
} | ||
}) | ||
|
||
const count = 1000 | ||
|
||
const folder = await createFolderWithFilesBulk(client, count) | ||
await deleteFile(client, folder) | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
main().catch(e => console.error(e)) |
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const { default: CozyClient, QueryDefinition } = require('cozy-client') | ||
const { FileDocument } = require('cozy-client/dist/types') | ||
|
||
const schema = { | ||
files: { | ||
doctype: 'io.cozy.files' | ||
} | ||
} | ||
|
||
const generateFakeFolder = () => ({ | ||
name: 'folder-name' + Math.random(), | ||
type: 'directory', | ||
dirId: 'io.cozy.files.root-dir' | ||
}) | ||
|
||
const generateFakeFile = (folder, i, file) => ({ | ||
name: `${folder.name}-${i}.txt`, | ||
contentType: 'text/plain', | ||
path: `/${folder.name}/name${i}.txt`, | ||
type: 'file', | ||
dirId: folder._id, | ||
data: file | ||
}) | ||
|
||
const createFolder = async client => { | ||
const { data: folder } = await client.stackClient | ||
.collection('io.cozy.files') | ||
.create(generateFakeFolder()) | ||
return folder | ||
} | ||
|
||
const createFilesInsideFolder = async (client, count, folder) => { | ||
const generatedFile = Buffer.from('generated files') | ||
for (let index = 0; index < count; index++) { | ||
let file = generateFakeFile(folder, index, generatedFile) | ||
await client.stackClient.collection('io.cozy.files').create(file) | ||
} | ||
} | ||
/** | ||
* Create a folder and add files inside | ||
* | ||
* @param {CozyClient} client - Cozy client instance | ||
* @param {number} count - Number of files to create | ||
* @returns {Promise<FileDocument>} Folder created | ||
*/ | ||
const createFolderWithFilesBulk = async (client, count) => { | ||
const folder = await createFolder(client) | ||
await createFilesInsideFolder(client, count, folder) | ||
return folder | ||
} | ||
|
||
/** | ||
* Delete a file | ||
* | ||
* @param {CozyClient} client - Cozy client instance | ||
* @param {FileDocument} file - File to delete | ||
* @param {string} file._id - Id of the file | ||
* @returns {Promise<void>} | ||
*/ | ||
const deleteFile = async (client, file) => { | ||
await client.stackClient | ||
.collection('io.cozy.files') | ||
.destroy({ _id: file._id }) | ||
} | ||
|
||
/** | ||
* Delete all the files inside a Cozy does not have path field | ||
* Useful to remove orphans | ||
* | ||
* @param {CozyClient} client - Cozy client instance | ||
* @returns {Promise<void>} | ||
*/ | ||
const deleteAllFilesWithoutPath = async client => { | ||
const result = await getAllFiles(client) | ||
console.log(result.length, 'files found inside this Drive') | ||
const undefinedPathFiles = result.filter(({ path }) => !path) | ||
console.log( | ||
undefinedPathFiles.length, | ||
'files without path found inside this Drive' | ||
) | ||
|
||
for (let file of undefinedPathFiles) { | ||
if (!file.trashed) { | ||
await deleteFile(client, file) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get all files | ||
* | ||
* @param {CozyClient} client - Cozy client instance | ||
* @returns {Promise<FileDocument>} | ||
*/ | ||
const getAllFiles = async client => { | ||
const query = new QueryDefinition({ | ||
doctype: 'io.cozy.files', | ||
limit: null | ||
}) | ||
const resp = await client.queryAll(query) | ||
return resp.data | ||
} | ||
|
||
module.exports = { | ||
schema, | ||
createFolderWithFilesBulk, | ||
deleteFile, | ||
deleteAllFilesWithoutPath, | ||
getAllFiles | ||
} |