def handle_request():
    server = os.environ["SERVER_NAME"].split(":")[0]
    path = os.environ["REQUEST_URI"]

    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(path).hexdigest()
    if os.path.isfile(lock):
        doRedirect(path)
        return 
    
    with lock_file(lock):
        if DEBUG:
            import cgitb
            cgitb.enable()

        try:
            url_parts = process_url(path, server)
        except Http404, e:
            do404(e.message, DEBUG)
            return

        new_file = Transmogrify(url_parts['original_file'], url_parts['actions'])
        new_file.save()

        doRedirect(path)
Beispiel #2
0
 def testBorder(self):
     transmog = Transmogrify(self.square_img, [('b', '3-f00'),])
     transmog.save()
     transmog = Transmogrify(self.vert_img, [('b', '3-f00'),])
     transmog.save()
     transmog = Transmogrify(self.horiz_img, [('b', '3-f00'),])
     transmog.save()
Beispiel #3
0
 def testLetterbox(self):
     transmog = Transmogrify(self.square_img, [('l', '300x300-888'),])
     transmog.save()
     transmog = Transmogrify(self.vert_img, [('l', '300x300-888'),])
     transmog.save()
     transmog = Transmogrify(self.horiz_img, [('l', '300x300-888'),])
     transmog.save()
Beispiel #4
0
def transmogrify_serve(request, path, document_root=None, show_indexes=False):
    if HEXDIGEST_RE.match(request.META['QUERY_STRING']):
        try:
            request_uri = "%s?%s" % (path, request.META['QUERY_STRING'])
            server = request.META["SERVER_NAME"].split(":")[0]
            url_parts = utils.process_url(request_uri, server, document_root)
            if not os.path.exists(url_parts['requested_file']):
                new_file = Transmogrify(url_parts['original_file'], url_parts['actions'])
                new_file.save()
        except utils.Http404, e:
            raise Http404(e)
Beispiel #5
0
def app(environ, start_response):
    from settings import DEBUG

    cropname = None
    server = environ['SERVER_NAME']
    quality = 80

    request_uri = get_path(environ)
    path_and_query = request_uri.lstrip('/')
    if path_and_query is "":
        return do_404(environ, start_response, "Not Found", DEBUG)

    if environ.get('REQUEST_METHOD', 'GET') == 'PURGE':
        return handle_purge(environ, start_response)

    # Acquire lockfile
    lock = '/tmp/%s' % sha1(path_and_query).hexdigest()
    if os.path.isfile(lock):
        return do_redirect(environ, start_response, request_uri)

    with lock_file(lock):
        try:
            url_parts = process_url(path_and_query, server)
            output_path, _ = os.path.split(url_parts['requested_file'])
            makedirs(output_path)
            if not os.path.exists(url_parts['original_file']):
                raise Http404
            if not os.path.isfile(url_parts['original_file']):
                raise Http404
        except Http404 as e:
            return do_404(environ, start_response, e.message, DEBUG)

        new_file = Transmogrify(
            url_parts['original_file'],
            url_parts['actions'],
            quality=quality,
            output_path=output_path
        )
        new_file.cropname = cropname
        new_file.save()

        if cropname:
            # Rewrite the request_uri to use the new file with the
            # cropname
            urlbits = list(urlparse.urlsplit(request_uri))
            output_filename = new_file.get_processed_filename()
            filename = os.path.basename(output_filename)
            requested_dir = os.path.dirname(urlbits[2])
            urlbits[2] = os.path.join(requested_dir, filename)
            request_uri = urlparse.urlunsplit(urlbits)

        return do_redirect(environ, start_response, request_uri)
Beispiel #6
0
 def test_thumbnail(self):
     expected_square = (300, 300)
     expected_vert = (168, 300)
     expected_horiz = (300, 208)
     expected_animated = (300, 214)
     transmog = Transmogrify(self.square_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_vert, img.size)
     transmog = Transmogrify(self.horiz_img, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_horiz, img.size)
     transmog = Transmogrify(self.animated, [("r", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_animated, img.size)
Beispiel #7
0
 def test_border(self):
     transmog = Transmogrify(self.square_img, [("b", "3-f00")])
     transmog.save()
     transmog = Transmogrify(self.vert_img, [("b", "3-f00")])
     transmog.save()
     transmog = Transmogrify(self.horiz_img, [("b", "3-f00")])
     transmog.save()
     transmog = Transmogrify(self.animated, [("b", "3-f00")])
     transmog.save()
Beispiel #8
0
 def test_letterbox(self):
     transmog = Transmogrify(self.square_img, [("l", "300x300-888")])
     transmog.save()
     transmog = Transmogrify(self.vert_img, [("l", "300x300-888")])
     transmog.save()
     transmog = Transmogrify(self.horiz_img, [("l", "300x300-888")])
     transmog.save()
     transmog = Transmogrify(self.animated, [("l", "300x300-888")])
     transmog.save()
Beispiel #9
0
    def test_cropname(self):
        transmog = Transmogrify(self.horiz_img, [("c", "0-510-300-810")])
        transmog.cropname = "cropped"

        self.assertEqual(transmog.get_processed_filename(), self.cropname_img)
Beispiel #10
0
    def test_crop_bbox(self):
        expected_square = (300, 300)
        transmog = Transmogrify(self.square_img, [("c", "100-100-400-400")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.vert_img, [("c", "0-100-300-400")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.horiz_img, [("c", "0-410-300-710")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        # 810 is larger than the image, PIL adds black to the extra space.
        # who knows if this is desirable but at least it doesn't raise
        # an exception.
        transmog = Transmogrify(self.horiz_img, [("c", "0-510-300-810")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)

        transmog = Transmogrify(self.animated, [("c", "0-410-300-710")])
        transmog.save()
        img = Image.open(transmog.get_processed_filename())
        self.assertEqual(expected_square, img.size)
Beispiel #11
0
 def test_crop(self):
     expected_square = (300, 300)
     transmog = Transmogrify(self.square_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.horiz_img, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.animated, [("c", "300x300")])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
        print "Location: %s" % os.environ["REQUEST_URI"]
        print
        return
    open(lock, 'w')

    if DEBUG:
        import cgitb
        cgitb.enable()

    try:
        server = os.environ["SERVER_NAME"].split(":")[0]
        url_parts = process_url(os.environ['REQUEST_URI'], server)
    except Http404, e:
        do404(e.message, DEBUG)

    new_file = Transmogrify(url_parts['original_file'], url_parts['actions'])
    new_file.save()

    print "Location: /%s" % os.environ['REQUEST_URI']
    print

    # Remove lockfile
    os.remove(lock)

def do404(why, debug):
    if debug:
        message = "<h2>%s</h2>" % why
    else:
        message = "File not found"
    print "Status: 404"
    print "Content-type: text/html"
import sys
from settings import DEBUG
from utils import process_url, Http404
from transmogrify import Transmogrify

def handle_request():
    if DEBUG:
        import cgitb
        cgitb.enable()
    try:
        server = os.environ["SERVER_NAME"].split(":")[0]
        url_parts = process_url(os.environ['REQUEST_URI'], server)
    except Http404, e:
        do404(e.message, DEBUG)
    
    new_file = Transmogrify(url_parts['original_file'], url_parts['actions'])
    new_file.save()
    
    print "Location: /%s" % os.environ['REQUEST_URI']
    print


def do404(why, debug):
    if debug:
        message = "<h2>%s</h2>" % why
    else:
        message = "File not found"
    print "Status: 404"
    print "Content-type: text/html"
    print 
    print ERROR_404 % message
Beispiel #14
0
 def testCrop(self):
     expected_square = (300,300)
     transmog = Transmogrify(self.square_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.horiz_img, [('c', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
Beispiel #15
0
 def testResize(self):
     expected_square = (300,300)
     expected_vert = (168,300)
     expected_horiz = (300,208)
     transmog = Transmogrify(self.square_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_square, img.size)
     transmog = Transmogrify(self.vert_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_vert, img.size)
     transmog = Transmogrify(self.horiz_img, [('r', '300x300'),])
     transmog.save()
     img = Image.open(transmog.get_processed_filename())
     self.assertEqual(expected_horiz, img.size)
Beispiel #16
0
    def testCrapname(self):
        transmog = Transmogrify(self.horiz_img, [('c', '0-510-300-810'),])
        transmog.cropname = "cropped"

        self.assertEqual(transmog.get_processed_filename(),
                         self.cropname_img)
def app(environ, start_response):
    cropname = None
    server = environ['SERVER_NAME']
    quality = 80

    if "path=" in environ.get("QUERY_STRING", ""):
        # I should probably require a POST for this, but meh, let's not
        # rock the boat.

        # transmogrify is being used directly and not as a 404 handler
        query_dict = urlparse.parse_qs(environ['QUERY_STRING'])

        path = query_dict.get("path", [""])[0]
        key = query_dict.get("key", [""])[0]

        # validate the query params
        if not (path and key):
            # The required parameters were not given
            start_response("400 Bad Response",
                           [("Content-Type", "text/plain")])
            return ["path and key are required query parameters"]

        cropname = query_dict.get("cropname", [None])[0]
        quality = 100

        # rewrite the environ to look like a 404 handler
        environ['REQUEST_URI'] = path + "?" + key


    request_uri = environ['REQUEST_URI']
    path_and_query = request_uri.lstrip("/")
    requested_path = urlparse.urlparse(path_and_query).path

    if path_and_query is "":
        return do404(environ, start_response, "Not Found", DEBUG)

    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(path_and_query).hexdigest()

    if os.path.isfile(lock):
        return doRedirect(environ, start_response, request_uri)

    with lock_file(lock):

        if FALLBACK_SERVERS:
            result = do_fallback(FALLBACK_SERVERS, BASE_PATH, requested_path)
            if result == (False, "bad path"):
                start_response("403 Forbidden", [])
                return []

        try:
            url_parts = process_url(path_and_query, server)
        except Http404, e:
            return do404(environ, start_response, e.message, DEBUG)

        new_file = Transmogrify(
            url_parts['original_file'],
            url_parts['actions'],
            quality=quality
        )
        new_file.cropname = cropname
        new_file.save()

        if cropname:
            # Rewrite the request_uri to use the new file with the
            # cropname
            urlbits = list(urlparse.urlsplit(request_uri))
            output_filename = new_file.get_processed_filename()
            filename = os.path.basename(output_filename)
            requested_dir = os.path.dirname(urlbits[2])
            urlbits[2] = os.path.join(requested_dir, filename)
            request_uri = urlparse.urlunsplit(urlbits)

        return doRedirect(environ, start_response, request_uri)
import os
from transmogrify import Transmogrify

square_img = os.path.abspath(
    os.path.join(os.path.dirname(__file__), 'testdata', 'square_img.jpg'))
vert_img = os.path.abspath(
    os.path.join(os.path.dirname(__file__), 'testdata', 'vert_img.jpg'))
horiz_img = os.path.abspath(
    os.path.join(os.path.dirname(__file__), 'testdata', 'horiz_img.jpg'))

####
#### AutoCrop
####
Transmogrify(square_img, [
    ('a', '100x100'),
]).save()
Transmogrify(vert_img, [
    ('a', '100x100'),
]).save()
Transmogrify(horiz_img, [
    ('a', '100x100'),
]).save()

####
#### Thumbnail
####
Transmogrify(square_img, [
    ('t', '200'),
]).save()
Transmogrify(vert_img, [
    ('t', '200'),