-
-
Notifications
You must be signed in to change notification settings - Fork 423
Adds zip downloads for all of a document's attachments #1471
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b3416ee
Makes downloadStream fetch metadata
Spoffy e45b093
Refactors attachment downloads to be streaming, and include metadata
Spoffy 001004b
Adds Zip archive downloads for attachments
Spoffy ca8f3b2
Fixes test types for attachments
Spoffy f7dd1cd
Attempts a fix for content-length causing tests to fail
Spoffy 306261a
Adds TODO
Spoffy 30f9556
Fixes test error in ExternalStorageAttachmentStore
Spoffy dafb2fb
Adds archive tests and fixes
Spoffy dc29771
Changes zip-stream version to 6.0.1
Spoffy 742cac3
Adds permissions check to getAttachmentsArchive
Spoffy 99513f1
Adds DocApi tests
Spoffy 336a678
Fixes bad test expectation for DocApi
Spoffy 6668797
Fixes import order in Archive.ts
Spoffy 72f3e1e
Merge branch 'main' into spoffy/attachment-downloads
Spoffy d95b2d9
Refreshes yarn.lock
Spoffy 3a825f3
Addresses formatting code review feedback
Spoffy b76655f
Changes file naming convention for attachments
Spoffy 6f707bb
Shuts down attachment archiving if document shuts down
Spoffy efe15f7
Adds ActiveDoc archive tests
Spoffy e67c259
Cleans archive assertion in DocAPi
Spoffy 1f845fe
Swaps TODO for doc comment
Spoffy 2038992
Re-adds accidentally removed import
Spoffy c460ab6
Prevents name collisions due to multiple identical attachments
Spoffy a6daa51
Changes attachment file name to match doc name
Spoffy bfc9312
Changes 'no access' to 'insufficient access'
Spoffy d4eaadc
Fixes over-length line
Spoffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,59 @@ | ||
import {ZipArchiveEntry} from 'compress-commons'; | ||
import stream from 'node:stream'; | ||
import ZipStream, {ZipStreamOptions} from 'zip-stream'; | ||
|
||
export interface ArchiveEntry { | ||
name: string; | ||
data: stream.Readable | Buffer; | ||
} | ||
|
||
export interface Archive { | ||
dataStream: stream.Readable; | ||
completed: Promise<void>; | ||
} | ||
|
||
/** | ||
* | ||
* Creates a streamable zip archive, reading files on-demand from the entries iterator. | ||
* Entries are provided as an async iterable, to ensure the archive is constructed | ||
* correctly. A generator can be used for convenience. | ||
* @param {ZipStreamOptions} options - Settings for the zip archive | ||
* @param {AsyncIterable<ArchiveEntry>} entries - Entries to add. | ||
* @returns {Archive} | ||
*/ | ||
export async function create_zip_archive( | ||
options: ZipStreamOptions, entries: AsyncIterable<ArchiveEntry> | ||
): Promise<Archive> { | ||
const archive = new ZipStream(options); | ||
|
||
return { | ||
dataStream: archive, | ||
// Caller is responsible for error handling/awaiting on this promise. | ||
completed: (async () => { | ||
try { | ||
for await (const entry of entries) { | ||
// ZipStream will break if multiple entries try to be added at the same time. | ||
await addEntryToZipArchive(archive, entry); | ||
} | ||
archive.finish(); | ||
} catch (error) { | ||
archive.destroy(error); | ||
} finally { | ||
// If the stream was destroyed with an error, this will re-throw the error we caught above. | ||
// Without this, node will see the stream as having an uncaught error, and complain. | ||
await stream.promises.finished(archive); | ||
} | ||
})() | ||
}; | ||
} | ||
|
||
function addEntryToZipArchive(archive: ZipStream, file: ArchiveEntry): Promise<ZipArchiveEntry | undefined> { | ||
return new Promise((resolve, reject) => { | ||
archive.entry(file.data, { name: file.name }, function(err, entry) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(entry); | ||
}); | ||
}); | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.