Skip to content
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

Add CSV max filesize check (#190) #191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion invenio_previewer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
"""Confidence threshold for character encoding detection by `cchardet`."""

PREVIEWER_MAX_FILE_SIZE_BYTES = 1 * 1024 * 1024
"""Maximum file size in bytes for JSON/XML/CSV files."""
"""Maximum file size in bytes for JSON/XML files."""

PREVIEWER_MAX_IMAGE_SIZE_BYTES = 0.5 * 1024 * 1024
"""Maximum file size in bytes for image files."""

PREVIEWER_TXT_MAX_BYTES = 1 * 1024 * 1024
"""Maximum number of .txt file bytes to preview before truncated."""

PREVIEWER_CSV_MAX_BYTES = 100 * 1024 * 1024
"""Maximum file size in bytes for CSV files."""

PREVIEWER_ZIP_MAX_FILES = 1000
"""Max number of files showed in the ZIP previewer."""

Expand Down
10 changes: 9 additions & 1 deletion invenio_previewer/extensions/csv_papaparsejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@

previewable_extensions = ["csv", "dsv"]

def validate_csv(file):
"""Validate a CSV file."""
max_file_size = current_app.config.get(
"PREVIEWER_CSV_MAX_BYTES", 100 * 1024 * 1024
)
if file.size > max_file_size:
return False
return True

def can_preview(file):
"""Determine if the given file can be previewed."""
return file.is_local() and file.has_extensions(".csv", ".dsv")
return file.is_local() and file.has_extensions(".csv", ".dsv") and validate_csv(file)


def preview(file):
Expand Down