Skip to content
Draft
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
43 changes: 19 additions & 24 deletions storages/backends/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
# In models.py you can write:
# from FTPStorage import FTPStorage
# fs = FTPStorage()
# For a TLS configuration, you must use 'ftps' protocol
# For a TLS configuration, you must use 'ftpes' protocol
# class FTPTest(models.Model):
# file = models.FileField(upload_to='a/b/c/', storage=fs)

import ftplib
import io
import os
import re
import urllib.parse
from urllib.parse import unquote
from urllib.parse import urljoin
from urllib.parse import urlparse

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -58,28 +59,22 @@ def get_default_settings(self):

def _decode_location(self, location):
"""Return splitted configuration data from location."""
splitted_url = re.search(
r"^(?P<scheme>.+)://(?P<user>.+):(?P<passwd>.+)@"
r"(?P<host>.+):(?P<port>\d+)/(?P<path>.*)$",
location,
)

if splitted_url is None:
raise ImproperlyConfigured("Improperly formatted location URL")
if splitted_url["scheme"] not in ("ftp", "aftp", "ftps"):
raise ImproperlyConfigured("Only ftp, aftp, ftps schemes supported")
if splitted_url["host"] == "":
raise ImproperlyConfigured("You must at least provide host!")
splitted_url = urlparse(location)

config = {}
config["active"] = splitted_url["scheme"] == "aftp"
config["secure"] = splitted_url["scheme"] == "ftps"
if splitted_url.scheme not in ("ftp", "aftp", "ftps", "ftpes"):
raise ImproperlyConfigured("Only ftp, aftp, ftps, ftpes schemes supported")
if splitted_url.hostname == "":
raise ImproperlyConfigured("You must at least provide host!")

config["path"] = splitted_url["path"] or "/"
config["host"] = splitted_url["host"]
config["user"] = splitted_url["user"]
config["passwd"] = splitted_url["passwd"]
config["port"] = int(splitted_url["port"])
config = {
"active": splitted_url.scheme == "aftp",
"secure": splitted_url.scheme in ["ftps", "ftpes"],
"path": unquote(splitted_url.path) or "/",
"host": unquote(splitted_url.hostname),
"user": unquote(splitted_url.username),
"passwd": unquote(splitted_url.password),
"port": splitted_url.port,
}

return config

Expand Down Expand Up @@ -240,7 +235,7 @@ def size(self, name):
def url(self, name):
if self.base_url is None:
raise ValueError("This file is not accessible via a URL.")
return urllib.parse.urljoin(self.base_url, name).replace("\\", "/")
return urljoin(self.base_url, name).replace("\\", "/")


class FTPStorageFile(File):
Expand Down
29 changes: 23 additions & 6 deletions tests/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from storages.backends import ftp

USER = "foo"
PASSWORD = "b@r"
PASSWORD = "bar"
HOST = "localhost"
PORT = 2121

Expand Down Expand Up @@ -50,7 +50,7 @@ def test_init_location_from_setting(self, mock_setting):
def test_decode_location(self):
config = self.storage._decode_location(URL)
wanted_config = {
"passwd": "b@r",
"passwd": "bar",
"host": "localhost",
"user": "foo",
"active": False,
Expand All @@ -62,7 +62,7 @@ def test_decode_location(self):
# Test active FTP
config = self.storage._decode_location("a" + URL)
wanted_config = {
"passwd": "b@r",
"passwd": "bar",
"host": "localhost",
"user": "foo",
"active": True,
Expand All @@ -79,7 +79,24 @@ def test_decode_location_error(self):
self.storage._decode_location("http://foo.pt")

def test_decode_location_urlchars_password(self):
self.storage._decode_location(geturl(pwd="b#r"))
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%3Ar"))["passwd"], "b:r"
)
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%2Fr"))["passwd"], "b/r"
)
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%40r"))["passwd"], "b@r"
)
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%23r"))["passwd"], "b#r"
)
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%3Fr"))["passwd"], "b?r"
)
self.assertEqual(
self.storage._decode_location(geturl(pwd="b%25r"))["passwd"], "b%r"
)

@override_settings(FTP_STORAGE_LOCATION=URL)
def test_override_settings(self):
Expand Down Expand Up @@ -269,11 +286,11 @@ def test_close(self, mock_ftp, mock_storage):

class FTPTLSTest(TestCase):
def setUp(self):
self.storage = ftp.FTPStorage(location=geturl(scheme="ftps"))
self.storage = ftp.FTPStorage(location=geturl(scheme="ftpes"))

def test_decode_location(self):
wanted_config = {
"passwd": "b@r",
"passwd": "bar",
"host": "localhost",
"user": "foo",
"active": False,
Expand Down