Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Jan 26, 2013
1 parent c4e4b20 commit 9887444
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
8 changes: 3 additions & 5 deletions storages/backends/overwrite.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os

from django.conf import settings
from django.core.files.storage import FileSystemStorage


class OverwriteStorage(FileSystemStorage):
"""
Comes from http://www.djangosnippets.org/snippets/976/
(even if it already exists in S3Storage for ages)
See also Django #4339, which might add this functionality to core.
"""

def get_available_name(self, name):
"""
Returns a filename that's free on the target storage system, and
Expand Down
28 changes: 14 additions & 14 deletions storages/backends/symlinkorcopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
from django.core.files.storage import FileSystemStorage

__doc__ = """
I needed to efficiently create a mirror of a directory tree (so that
"origin pull" CDNs can automatically pull files). The trick was that
some files could be modified, and some could be identical to the original.
Of course it doesn't make sense to store the exact same data twice on the
I needed to efficiently create a mirror of a directory tree (so that
"origin pull" CDNs can automatically pull files). The trick was that
some files could be modified, and some could be identical to the original.
Of course it doesn't make sense to store the exact same data twice on the
file system. So I created SymlinkOrCopyStorage.
SymlinkOrCopyStorage allows you to symlink a file when it's identical to
SymlinkOrCopyStorage allows you to symlink a file when it's identical to
the original file and to copy the file if it's modified.
Of course, it's impossible to know if a file is modified just by looking
Of course, it's impossible to know if a file is modified just by looking
at the file, without knowing what the original file was.
That's what the symlinkWithin parameter is for. It accepts one or more paths
(if multiple, they should be concatenated using a colon (:)).
Files that will be saved using SymlinkOrCopyStorage are then checked on their
location: if they are within one of the symlink_within directories,
That's what the symlinkWithin parameter is for. It accepts one or more paths
(if multiple, they should be concatenated using a colon (:)).
Files that will be saved using SymlinkOrCopyStorage are then checked on their
location: if they are within one of the symlink_within directories,
they will be symlinked, otherwise they will be copied.
The rationale is that unmodified files will exist in their original location,
e.g. /htdocs/example.com/image.jpg and modified files will be stored in
The rationale is that unmodified files will exist in their original location,
e.g. /htdocs/example.com/image.jpg and modified files will be stored in
a temporary directory, e.g. /tmp/image.jpg.
"""

class SymlinkOrCopyStorage(FileSystemStorage):
"""Stores symlinks to files instead of actual files whenever possible
When a file that's being saved is currently stored in the symlink_within
directory, then symlink the file. Otherwise, copy the file.
"""
def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL,
def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL,
symlink_within=None):
super(SymlinkOrCopyStorage, self).__init__(location, base_url)
self.symlink_within = symlink_within.split(":")
Expand Down
3 changes: 0 additions & 3 deletions storages/tests/hashpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def test_save_same_file(self):
saves a file twice, the file should only be stored once, because the
content/hash is the same
"""

path_1 = self.storage.save('test', ContentFile('new content'))

path_2 = self.storage.save('test', ContentFile('new content'))

self.assertEqual(path_1, path_2)

0 comments on commit 9887444

Please sign in to comment.