aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/processing/__init__.py
diff options
context:
space:
mode:
authorChristopher Allan Webber <cwebber@dustycloud.org>2012-03-26 14:33:12 -0500
committerChristopher Allan Webber <cwebber@dustycloud.org>2012-03-26 14:33:12 -0500
commite3e5980624d70922b84df764d32625d088fb841c (patch)
tree480e5d7c994f31797be4990bc0347d6b866bc5b2 /mediagoblin/processing/__init__.py
parentebb24e4fd18fb53133755333314a23d22fc72707 (diff)
parentc16b8196631e5b1c4cbe618a9af74f5455fe861c (diff)
downloadmediagoblin-e3e5980624d70922b84df764d32625d088fb841c.tar.lz
mediagoblin-e3e5980624d70922b84df764d32625d088fb841c.tar.xz
mediagoblin-e3e5980624d70922b84df764d32625d088fb841c.zip
Merge remote-tracking branch 'remotes/bretts/bug261-resized-filenames'
Diffstat (limited to 'mediagoblin/processing/__init__.py')
-rw-r--r--mediagoblin/processing/__init__.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/mediagoblin/processing/__init__.py b/mediagoblin/processing/__init__.py
index ecdfa8a9..9dee3baa 100644
--- a/mediagoblin/processing/__init__.py
+++ b/mediagoblin/processing/__init__.py
@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
+import os
from mediagoblin.db.util import atomic_update
from mediagoblin import mg_globals as mgg
@@ -33,6 +34,37 @@ def create_pub_filepath(entry, filename):
unicode(entry._id),
filename])
+class FilenameBuilder(object):
+ """Easily slice and dice filenames.
+
+ Initialize this class with an original file path, then use the fill()
+ method to create new filenames based on the original.
+
+ """
+ MAX_FILENAME_LENGTH = 255 # VFAT's maximum filename length
+
+ def __init__(self, path):
+ """Initialize a builder from an original file path."""
+ self.dirpath, self.basename = os.path.split(path)
+ self.basename, self.ext = os.path.splitext(self.basename)
+ self.ext = self.ext.lower()
+
+ def fill(self, fmtstr):
+ """Build a new filename based on the original.
+
+ The fmtstr argument can include the following:
+ {basename} -- the original basename, with the extension removed
+ {ext} -- the original extension, always lowercase
+
+ If necessary, {basename} will be truncated so the filename does not
+ exceed this class' MAX_FILENAME_LENGTH in length.
+
+ """
+ basename_len = (self.MAX_FILENAME_LENGTH -
+ len(fmtstr.format(basename='', ext=self.ext)))
+ return fmtstr.format(basename=self.basename[:basename_len],
+ ext=self.ext)
+
def mark_entry_failed(entry_id, exc):
"""