Commit cceab326 authored by Pierre Dittgen's avatar Pierre Dittgen
Browse files

Fix validation of uploaded file

parent 2a674882
......@@ -7,6 +7,8 @@ from io import BytesIO
from pathlib import Path
from urllib.parse import urlparse
from frictionless.helpers import detect_encoding
log = logging.getLogger(__name__)
......@@ -45,22 +47,26 @@ class UploadedFileValidataResource(ValidataResource):
self.filename = filename
self.content = bytes_content
def build_reader(self):
return BytesIO(self.content)
def __detect_format_from_file_extension(self):
ext = Path(self.filename).suffix
if ext in (".csv", ".tsv", ".ods", ".xls", ".xlsx"):
return ext[1:]
return None
def build_stream_args(self):
"""Uploaded file implementation"""
options = {
"scheme": "stream",
"format": self.__detect_format_from_file_extension(),
}
return (self.build_reader(), options)
def detect_format_from_file_extension(filename: str):
ext = Path(filename).suffix
if ext in (".csv", ".tsv", ".ods", ".xls", ".xlsx"):
return ext[1:]
return None
format = detect_format_from_file_extension(self.filename)
if format in {'csv', 'tsv'}:
scheme = 'text'
encoding = detect_encoding(self.content)
source = self.content.decode(encoding)
else:
scheme = "filelike"
source = BytesIO(self.content)
return (source, { "scheme": scheme, "format": format})
def strip_accents(s):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment