From 6962cf6f9b9b34e5c4f867e8b0f95383ef7bbb28 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Fri, 3 Feb 2023 10:48:55 +0100 Subject: [PATCH] feat: allow to 'zip' a fileCollection instance This also removes fileCollectionItemsZip that was likely not used --- src/FileCollection.ts | 12 ++---- src/__tests__/FileCollection.test.ts | 21 ++++++++-- src/__tests__/fileCollectionItemsZip.test.ts | 20 ---------- src/__tests__/fileCollectionToZip.test.ts | 5 +-- src/fileCollectionItemsZip.ts | 42 -------------------- src/fileCollectionToZip.ts | 4 +- src/index.ts | 1 - 7 files changed, 25 insertions(+), 80 deletions(-) delete mode 100644 src/__tests__/fileCollectionItemsZip.test.ts delete mode 100644 src/fileCollectionItemsZip.ts diff --git a/src/FileCollection.ts b/src/FileCollection.ts index 2867480..af69090 100644 --- a/src/FileCollection.ts +++ b/src/FileCollection.ts @@ -1,8 +1,5 @@ import { FileCollectionItem } from './FileCollectionItem'; -import { - fileCollectionItemsZip, - FileCollectionItemsZipOptions, -} from './fileCollectionItemsZip'; +import { fileCollectionToZip } from './fileCollectionToZip'; export class FileCollection { readonly files: FileCollectionItem[]; @@ -19,11 +16,10 @@ export class FileCollection { * Zip the FileCollection * This method returns a new FileCollection that contains only one FileItem that * is the zipped file (called by default 'file.zip') + * Not sure this is super useful and we should probably remove it and replace it by fileCollectionToZip */ - async zip(options: FileCollectionItemsZipOptions = {}) { - return new FileCollection([ - await fileCollectionItemsZip(this.files, options), - ]); + async zip() { + return fileCollectionToZip(this); } [Symbol.iterator]() { diff --git a/src/__tests__/FileCollection.test.ts b/src/__tests__/FileCollection.test.ts index 717da94..d10cb5c 100644 --- a/src/__tests__/FileCollection.test.ts +++ b/src/__tests__/FileCollection.test.ts @@ -1,7 +1,7 @@ -/* eslint-disable @typescript-eslint/prefer-regexp-exec */ import { join } from 'path'; import { fileCollectionFromPath } from '../fileCollectionFromPath'; +import { fileCollectionFromZip } from '../fileCollectionFromZip'; describe('FileCollection', () => { it('filter', async () => { @@ -18,8 +18,21 @@ describe('FileCollection', () => { ).toStrictEqual(['dir1/dir3/e.txt - e.txt', 'dir1/dir3/f.txt - f.txt']); const zipped = await fileCollection.zip(); - const zippedFile = zipped.files[0]; - expect(zippedFile.relativePath).toBe('file.zip'); - expect((await zippedFile.arrayBuffer()).byteLength).toBe(612); + expect(zipped).toHaveLength(612); + + const unzippedFileCollection = await fileCollectionFromZip(zipped); + + expect( + Array.from( + unzippedFileCollection.files.map( + (a) => `${a.relativePath} - ${a.name}`, + ), + ), + ).toStrictEqual([ + 'dir1/a.txt - a.txt', + 'dir1/b.txt - b.txt', + 'dir1/dir3/e.txt - e.txt', + 'dir1/dir3/f.txt - f.txt', + ]); }); }); diff --git a/src/__tests__/fileCollectionItemsZip.test.ts b/src/__tests__/fileCollectionItemsZip.test.ts deleted file mode 100644 index d729f6c..0000000 --- a/src/__tests__/fileCollectionItemsZip.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { join } from 'path'; - -import { fileCollectionFromPath } from '../fileCollectionFromPath'; -import { fileCollectionItemsZip } from '../fileCollectionItemsZip'; - -describe('fileCollectionItemsZip', () => { - it('simple data', async () => { - const fileCollection = await fileCollectionFromPath( - join(__dirname, 'data'), - ); - - const zippedItem = await fileCollectionItemsZip(fileCollection.files); - - const zip = await zippedItem.arrayBuffer(); - expect(zip.byteLength).toBe(1482); - - // This is the way you can check that the zip is correct - // writeFileSync('test.zip', Buffer.from(await zippedItem.arrayBuffer())); - }); -}); diff --git a/src/__tests__/fileCollectionToZip.test.ts b/src/__tests__/fileCollectionToZip.test.ts index 2fed165..4ef3109 100644 --- a/src/__tests__/fileCollectionToZip.test.ts +++ b/src/__tests__/fileCollectionToZip.test.ts @@ -10,9 +10,8 @@ describe('fileCollectionToZip', () => { join(__dirname, 'data'), ); - const arrayBuffer = await fileCollectionToZip(fileCollection); - - const unzipped = await fileCollectionFromZip(arrayBuffer); + const typedArray = await fileCollectionToZip(fileCollection); + const unzipped = await fileCollectionFromZip(typedArray); const paths = unzipped.files.map((file) => file.relativePath); expect(paths).toStrictEqual([ 'data/dir1/a.txt', diff --git a/src/fileCollectionItemsZip.ts b/src/fileCollectionItemsZip.ts deleted file mode 100644 index 333e01b..0000000 --- a/src/fileCollectionItemsZip.ts +++ /dev/null @@ -1,42 +0,0 @@ -import JSZip from 'jszip'; - -import { FileCollectionItem } from './FileCollectionItem'; - -export type FileCollectionItemsZipOptions = { - /** - * Relative path of the zipped file - * @default 'file.zip' - */ - relativePath?: string; -}; - -/** - * This method is used internally to allow fileCollection.zip() to work - * @param fileCollectionItems - * @param options - * @returns - */ -export async function fileCollectionItemsZip( - fileCollectionItems: FileCollectionItem[], - options: FileCollectionItemsZipOptions = {}, -): Promise { - const zip = new JSZip(); - const { relativePath = 'file.zip' } = options; - for (const file of fileCollectionItems) { - zip.file(file.relativePath, await file.arrayBuffer()); - } - - return { - lastModified: Date.now(), - name: relativePath.replace(/^.*\//, ''), - relativePath, - size: -1, // no idea about the size, we didn't compress it yet ... - arrayBuffer: () => zip.generateAsync({ type: 'arraybuffer' }), - stream: () => { - throw new Error('stream on zip is not implemented'); - }, - text: () => { - throw new Error('text on zip is not implemented'); - }, - }; -} diff --git a/src/fileCollectionToZip.ts b/src/fileCollectionToZip.ts index 2d89c18..8a259a1 100644 --- a/src/fileCollectionToZip.ts +++ b/src/fileCollectionToZip.ts @@ -9,12 +9,12 @@ import { FileCollection } from './FileCollection'; */ export async function fileCollectionToZip( fileCollection: FileCollection, -): Promise { +): Promise { const jsZip = new JSZip(); for (const file of fileCollection) { jsZip.file(file.relativePath, await file.arrayBuffer()); } - return jsZip.generateAsync({ type: 'arraybuffer' }); + return jsZip.generateAsync({ type: 'uint8array' }); } diff --git a/src/index.ts b/src/index.ts index abd2df4..d23e03f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ export * from './fileCollectionFromWebservice'; export * from './fileCollectionFromFileList'; export * from './fileCollectionFromFileArray'; export * from './fileCollectionFromFiles'; -export * from './fileCollectionToZip'; export * from './groupFiles'; export * from './FileCollection'; export * from './FileCollectionItem';