Restrict specific file type while uploading

How to restrict specific image type while uploading?For example- restrict .docx, .xlsx files while uploading and allow only .pdf file types.

1 Like

@mohitchechani Are you uploading files using an Attach field type? If yes, then I believe that you can put a JSON in the options of the field like:

{"restrictions": {"allowed_file_types": [".jpg", ".png"]}}
1 Like

Thanks for reply!!

But, unfortunately its not working.

@mohitchechani Based on the source code it should work but since it didn’t, I will check if the code converts JSON to object and I will get back to you.

@mohitchechani Since it was impossible to set the allowed file types for the Attach field, I created a plugin that allows several customizations for both Attach and Attach Image fields.

Using it you will be able to set the allowed file types by inserting a JSON in the options field.

{"allowed_file_types": [".jpg", ".png"]}

Check it out, I hope it helps you.

2 Likes

Hi I am getting the below error while running bench get-app

 error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [89 lines of output]
      configuration error: `project.license` must be valid exactly by one definition (2 matches found):
      
          - keys:
              'file': {type: string}
            required: ['file']
          - keys:
              'text': {type: string}
            required: ['text']
      
      DESCRIPTION:
          `Project license <https://www.python.org/dev/peps/pep-0621/#license>`_.
      
      GIVEN VALUE:
          "MIT"
      
      OFFENDING RULE: 'oneOf'
      
      DEFINITION:
          {
              "oneOf": [
                  {
                      "properties": {
                          "file": {
                              "type": "string",
                              "$$description": [
                                  "Relative path to the file (UTF-8) which contains the license for the",
                                  "project."
                              ]
                          }
                      },
                      "required": [
                          "file"
                      ]
                  },
                  {
                      "properties": {
                          "text": {
                              "type": "string",
                              "$$description": [
                                  "The license of the project whose meaning is that of the",
                                  "`License field from the core metadata",
                                  "<https://packaging.python.org/specifications/core-metadata/#license>`_."
                              ]
                          }
                      },
                      "required": [
                          "text"
                      ]
                  }
              ]
          }
      Traceback (most recent call last):
        File "/home/frappe/frappe-bench/env/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
          main()
        File "/home/frappe/frappe-bench/env/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/home/frappe/frappe-bench/env/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 130, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 177, in get_requires_for_build_wheel
          return self._get_build_requires(
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 159, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 281, in run_setup
          super(_BuildMetaLegacyBackend,
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 174, in run_setup
          exec(code, locals())
        File "<string>", line 8, in <module>
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/_distutils/core.py", line 159, in setup
          dist.parse_config_files()
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/dist.py", line 868, in parse_config_files
          pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/config/pyprojecttoml.py", line 62, in apply_configuration
          config = read_configuration(filepath, True, ignore_option_errors, dist)
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/config/pyprojecttoml.py", line 126, in read_configuration
          validate(subset, filepath)
        File "/tmp/pip-build-env-h1xnz2e7/overlay/lib/python3.8/site-packages/setuptools/config/pyprojecttoml.py", line 51, in validate
          raise ValueError(f"{error}\n{summary}") from None
      ValueError: invalid pyproject.toml config: `project.license`.
      configuration error: `project.license` must be valid exactly by one definition (2 matches found):
      
          - keys:
              'file': {type: string}
            required: ['file']
          - keys:
              'text': {type: string}
            required: ['text']
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: 
Traceback (most recent call last):
  File "/home/frappe/.local/bin/bench", line 8, in <module>
    sys.exit(cli())
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/cli.py", line 121, in cli
    raise e
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/cli.py", line 111, in cli
    bench_command()
  File "/usr/lib/python3/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/commands/make.py", line 142, in get_app
    get_app(
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/app.py", line 371, in get_app
    app.install(verbose=verbose, skip_assets=skip_assets, restart_bench=restart_bench)
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/utils/render.py", line 110, in wrapper_fn
    return fn(*args, **kwargs)
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/app.py", line 198, in install
    install_app(
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/app.py", line 422, in install_app
    bench.run(f"{bench.python} -m pip install {quiet_flag} --upgrade -e {app_path} {cache_flag}")
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/bench.py", line 43, in run
    return exec_cmd(cmd, cwd=cwd or self.cwd)
  File "/home/frappe/.local/lib/python3.8/site-packages/bench/utils/__init__.py", line 126, in exec_cmd
    raise CommandFailedError
bench.exceptions.CommandFailedError

@mohitchechani Sorry for that. I fixed it :slightly_smiling_face:.

Hi, its working if the field is in Parent Doctype.

But it’s not working if the attach field is in Child Table.

@mohitchechani I will check the code and get back to you.

@mohitchechani I didn’t find a reason for your problem but I suspected a specific code so I fixed it. Now I’m pretty sure it will work and I hope it does :smiley:.

Will check tomorrow and update

@mohitchechani I will be waiting for the update :grin:.

Now it’s not working on both parent doctype and child table

@mohitchechani It was all because of Attach Image. Sorry for your inconvenience, I fixed everything now :grin:

Hi, thanks for reply!!!

But, unfortunately it still does not work for both parent Doctype & Child Table

@mohitchechani Have you tried to restart bench?
From my end, I will recheck the plugin.

@mohitchechani Although I couldn’t find a reason for the plugin not to work, I made some changes just in case and updated the plugin. Hopefully this time it will work, but please after updating the plugin, restart bench.

Hi, now it’s working perfectly in Parent Doctype.

But not working in child table.

As suggested, I did run bench restart after updation.

@mohitchechani Are you using Attach fieldtype or Attach Image in the child doctype? If you are using Attach Image please check the console because I left a message in case it does extend the old Attach control and not the customized one.

@mohitchechani I think I figured out why it is not working in child doctypes but it is not because of the plugin. Let me check further.