-
Notifications
You must be signed in to change notification settings - Fork 73
Export entire folder recursively #711
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
4 commits
Select commit
Hold shift + click to select a range
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
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,62 @@ | ||
| """Service for exporting item folders as streaming ZIP archives.""" | ||
|
|
||
| from django.core.files.storage import default_storage | ||
|
|
||
| from zipstream import ZipStream | ||
|
|
||
| from core import models | ||
|
|
||
| DEFAULT_STORAGE_READ_CHUNK_SIZE = 1024 | ||
|
|
||
|
|
||
| def iter_storage_chunks(file_key, chunk_size=DEFAULT_STORAGE_READ_CHUNK_SIZE): | ||
| """Yield bytes from object storage without buffering the whole file.""" | ||
| with default_storage.open(file_key, "rb") as fh: | ||
| while chunk := fh.read(chunk_size): | ||
| yield chunk | ||
|
|
||
|
|
||
| def export_descendants(folder): | ||
|
lunika marked this conversation as resolved.
|
||
| """ | ||
| Yield (file_key_or_None, archive_path) tuples for a folder's subtree. | ||
|
|
||
| Walks descendants ordered by path, skips descendants whose ancestors are | ||
| soft-deleted, computes the relative archive path for each item, and emits | ||
| a directory entry (`file_key=None`, trailing slash) for folders or a file | ||
| entry for `FILE` items in the `READY` upload state. | ||
| """ | ||
| descendants = folder.descendants().filter(ancestors_deleted_at__isnull=True).order_by("path") | ||
|
|
||
| relative_paths = {str(folder.path): ""} | ||
| for descendant in descendants: | ||
| parent_key = str(descendant.path).rsplit(".", 1)[0] | ||
| parent_relative = relative_paths.get(parent_key) | ||
| if parent_relative is None: | ||
| continue | ||
|
|
||
| name = ( | ||
| descendant.filename | ||
| if descendant.type == models.ItemTypeChoices.FILE | ||
| else descendant.title | ||
| ) | ||
| relative = f"{parent_relative}/{name}" if parent_relative else name | ||
| relative_paths[str(descendant.path)] = relative | ||
|
|
||
| if descendant.type == models.ItemTypeChoices.FOLDER: | ||
| yield None, f"{relative}/" | ||
| elif descendant.upload_state == models.ItemUploadStateChoices.READY: | ||
| yield descendant.file_key, relative | ||
|
|
||
|
|
||
| def build_zip_stream(descendants): | ||
| """Build a ZIP stream that lazily reads exported files from storage.""" | ||
| zip_stream = ZipStream(sized=False) | ||
| for file_key, archive_path in descendants: | ||
| if file_key is None: | ||
| zip_stream.mkdir(archive_path) | ||
| else: | ||
| zip_stream.add( | ||
| data=iter_storage_chunks(file_key), | ||
| arcname=archive_path, | ||
| ) | ||
| return zip_stream | ||
Oops, something went wrong.
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.