Commits

Rich Leland committed eb51472

Make SFTP file URLs accessible, refs #147

Comments (0)

Files changed (1)

storages/backends/sftpstorage.py

 # set "~/.ssh/known_hosts" will be used
 
 
+import getpass
 import os
-import stat
 import paramiko
 import posixpath
-import getpass
+import stat
+import urlparse
 from datetime import datetime
+
 from django.conf import settings
+from django.core.files.base import File
 from django.core.files.storage import Storage
-from django.core.files.base import File
+
 try:
     from cStringIO import StringIO
 except ImportError:
-    from StringIO import StringIO
+    from StringIO import StringIO  # noqa
+
 
 class SFTPStorage(Storage):
 
         self._known_host_file = getattr(settings, 'SFTP_KNOWN_HOST_FILE', None)
 
         self._root_path = settings.SFTP_STORAGE_ROOT
+        self._base_url = settings.MEDIA_URL
 
         # for now it's all posix paths.  Maybe someday we'll support figuring
         # out if the remote host is windows.
         return datetime.fromtimestamp(utime)
 
     def url(self, name):
-        remote_path = self._remote_path(name)
-        return 'sftp://%s/%s' % (self._host, remote_path)
+        if self._base_url is None:
+            raise ValueError("This file is not accessible via a URL.")
+        return urlparse.urljoin(self._base_url, name).replace('\\', '/')
+
 
 class SFTPStorageFile(File):
     def __init__(self, name, storage, mode):