-
Notifications
You must be signed in to change notification settings - Fork 234
feat: validate file formats in url (#1606) #1669
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
Changes from 1 commit
49fd592
5680e0d
0ce7e54
c1e1528
5591d3e
d4a289f
a69c197
fa0dbb2
d438dc6
d0948fc
5222026
08c8b3b
262190f
c661282
7644d6d
fbe7d7c
4ee48f3
1a69277
02c15c6
32cc3ab
730f21b
9464fb7
ee29f6a
70d4970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: jupyterjazz <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,13 +32,13 @@ class AnyUrl(BaseAnyUrl, AbstractType): | |
|
|
||
| @classmethod | ||
| def mime_type(cls) -> str: | ||
| """Returns the mime type this class deals with.""" | ||
| """Returns the mime type associated with the class.""" | ||
| raise NotImplementedError | ||
|
|
||
| @classmethod | ||
| def extra_extensions(cls) -> List[str]: | ||
| """Returns a list of allowed file extensions for this class which | ||
| falls outside the scope of mimetypes library.""" | ||
| """Returns a list of allowed file extensions for the class | ||
| that are not covered by the mimetypes library.""" | ||
| raise NotImplementedError | ||
|
|
||
| def _to_node_protobuf(self) -> 'NodeProto': | ||
|
|
@@ -55,24 +55,25 @@ def _to_node_protobuf(self) -> 'NodeProto': | |
| @classmethod | ||
| def is_extension_allowed(cls, value: Any) -> bool: | ||
| """ | ||
| Check if the file extension of the url is allowed for that class. | ||
| First read the mime type of the file, if it fails, then check the file extension. | ||
| Check if the file extension of the URL is allowed for this class. | ||
| First, it guesses the mime type of the file. If it fails to detect the | ||
| mime type, it then checks the extra file extension. | ||
|
|
||
| :param value: url to the file | ||
| :param value: The URL or file path. | ||
| :return: True if the extension is allowed, False otherwise | ||
| """ | ||
| if cls == AnyUrl: # no check for AnyUrl class | ||
| if cls is AnyUrl: | ||
| return True | ||
| mimetype, _ = mimetypes.guess_type(value.split("?")[0]) | ||
| print('mimetype for value', mimetype, value, value.split("?")[0]) | ||
|
|
||
| url_parts = value.split("?") | ||
| mimetype, _ = mimetypes.guess_type(url_parts[0]) | ||
| if mimetype and mimetype.startswith(cls.mime_type()): | ||
| return True | ||
| filename = value.split("?")[0].split('.') | ||
| if len(filename) > 1: | ||
| extension = filename[-1] | ||
| return extension in cls.extra_extensions() | ||
|
|
||
| return False | ||
| filename = url_parts[0].split('.') | ||
| extension = filename[-1] if len(filename) > 1 else None | ||
|
||
|
|
||
| return extension in cls.extra_extensions() | ||
|
|
||
| @classmethod | ||
| def validate( | ||
|
|
@@ -98,7 +99,9 @@ def validate( | |
| url = super().validate(abs_path, field, config) # basic url validation | ||
|
|
||
| if not cls.is_extension_allowed(value): | ||
| raise ValueError(f'file {value} is not a valid file format for class {cls}') | ||
| raise ValueError( | ||
| f"The file '{value}' is not in a valid format for class '{cls.__name__}'." | ||
| ) | ||
|
|
||
| return cls(str(value if input_is_relative_path else url), scheme=None) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| MESH_EXTRA_EXTENSIONS = [ | ||
| '3ds', | ||
| '3mf', | ||
| 'ac', | ||
| 'ac3d', | ||
| 'amf', | ||
| 'assimp', | ||
| 'bvh', | ||
| 'cob', | ||
| 'collada', | ||
| 'ctm', | ||
| 'dxf', | ||
| 'e57', | ||
| 'fbx', | ||
| 'gltf', | ||
| 'glb', | ||
| 'ifc', | ||
| 'lwo', | ||
| 'lws', | ||
| 'lxo', | ||
| 'md2', | ||
| 'md3', | ||
| 'md5', | ||
| 'mdc', | ||
| 'm3d', | ||
| 'mdl', | ||
| 'ms3d', | ||
| 'nff', | ||
| 'obj', | ||
| 'off', | ||
| 'pcd', | ||
| 'pod', | ||
| 'pmd', | ||
| 'pmx', | ||
| 'ply', | ||
| 'q3o', | ||
| 'q3s', | ||
| 'raw', | ||
| 'sib', | ||
| 'smd', | ||
| 'stl', | ||
| 'ter', | ||
| 'terragen', | ||
| 'vtk', | ||
| 'vrml', | ||
| 'x3d', | ||
| 'xaml', | ||
| 'xgl', | ||
| 'xml', | ||
| 'xyz', | ||
| 'zgl', | ||
| 'vta', | ||
| ] | ||
|
|
||
| TEXT_EXTRA_EXTENSIONS = ['md', 'log'] | ||
|
|
||
| POINT_CLOUD_EXTRA_EXTENSIONS = [ | ||
| 'ascii', | ||
| 'bin', | ||
| 'b3dm', | ||
| 'bpf', | ||
| 'dp', | ||
| 'dxf', | ||
| 'e57', | ||
| 'fls', | ||
| 'fls', | ||
| 'glb', | ||
| 'ply', | ||
| 'gpf', | ||
| 'las', | ||
| 'obj', | ||
| 'osgb', | ||
| 'pcap', | ||
| 'pcd', | ||
| 'pdal', | ||
| 'pfm', | ||
| 'ply', | ||
| 'ply2', | ||
| 'pod', | ||
| 'pods', | ||
| 'pnts', | ||
| 'ptg', | ||
| 'ptx', | ||
| 'pts', | ||
| 'rcp', | ||
| 'xyz', | ||
| 'zfs', | ||
| ] |
Uh oh!
There was an error while loading. Please reload this page.