0

I am writing a management command to upload a file to my server. I am able to upload the file to media properly when the file is in the project folder. But i couldn't manage to upload a file from outside the folder.

Here is the version when the file is in project folder:


def handle(self, *args, **options):
    try:
        log_module = LogModule.objects.get(customer__id=options['customer_id'])
    except LogModule.DoesNotExist:
        return

    storage = log_module.storage_limit
    log_objects = Log.objects.filter(module=log_module)
    for log_object in log_objects:
        storage -= (os.path.getsize('media/' + str(log_object.log_file)) / (1024 * 1024))

    print('Available Storage: ' + str(storage))

    if storage - os.path.getsize(options['file_path']) < 0:
        my_file = open(options['file_path'], 'rb')
        django_file = File(my_file)
        log = Log(module=log_module)
        log.log_file.save(django_file.name, django_file, save=True)
    else:
        print('Not Enough Storage Limit!!!')
        return

And this is what i am trying to achieve:

my_file = open(options['file_path'], 'rb')
django_file = File(my_file)
log = Log(module=log_module)
log.log_file.save(django_file.name, django_file, save=True)

When i try to run this management command, i am getting following error:

Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/hskl/Projects/ramtek_hotspot/hotspot/modules/log_module/management/commands/upload_log.py", line 43, in handle
    log.log_file.save(django_file.name, django_file, save=True)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/db/models/fields/files.py", line 87, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 51, in save
    name = self.get_available_name(name, max_length=max_length)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 75, in get_available_name
    while self.exists(name) or (max_length and len(name) > max_length):
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 310, in exists
    return os.path.exists(self.path(name))
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/core/files/storage.py", line 323, in path
    return safe_join(self.location, name)
  File "/home/hskl/.local/share/virtualenvs/ramtek_hotspot-fTktNGJ3/lib/python3.7/site-packages/django/utils/_os.py", line 46, in safe_join
    'component ({})'.format(final_path, base_path))
django.core.exceptions.SuspiciousFileOperation: The joined path (/home/hskl/Downloads/network02.zip) is located outside of the base path component (/home/hskl/Projects/ramtek_hotspot/media)
``
Hskl
  • 21
  • 2
  • 1
    Does that help you? https://stackoverflow.com/questions/22019371/django-how-to-allow-a-suspicious-file-operation-copy-a-file – RaideR Jul 18 '19 at 11:18
  • I have tried what i think they suggested but it didn't work or i couldn't manage to make it work, i am not sure it is the same error: He is getting "Attempted access to 'D:\path\to\file' denied" but in my case django can access the file. Thank you for the comment. – Hskl Jul 18 '19 at 11:57
  • Possible duplicate of [Django: How to allow a Suspicious File Operation / copy a file](https://stackoverflow.com/questions/22019371/django-how-to-allow-a-suspicious-file-operation-copy-a-file) – Davide Pizzolato Jul 19 '19 at 11:48

0 Answers0