views.py 1.36 KB
Newer Older
denis's avatar
denis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
"""
visual_culture.views
"""

import os
import errno

from django.http import HttpResponse, HttpResponseBadRequest, Http404
from visual_culture.readers import Reader
from git_info.utils import find_mime

def ensure_dir(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise exception


import settings

from git_info import git

if settings.PREFIX:
    git_collection = git.GitCollection(settings.PREFIX)
else:
    git_collection = git.GitCollection()


def transduce(request, repo_name, oid, key, value, filename, extension):
    try:
        repo = git_collection[repo_name]
    except KeyError:
        raise Http404
    try:
        obj = repo[oid]
    except KeyError:
        raise Http404
    
    if obj.type != git.pygit2.GIT_OBJ_BLOB:
        return HttpResponseBadRequest('Requested object is not a BLOB')
    
    mime = find_mime(obj, filename)
    r = Reader()
    options = {}
    if key and value:
        options = {key: value}
    result = r.read_blob({'mime': mime, 'repo_name': repo_name, 'blob_hex': oid}, options)
    cpath = os.path.join(settings.MEDIA_ROOT, repo_name, oid, '%s..%s' % (key, value))
    ensure_dir(cpath)
    with open(os.path.join(cpath, '%s%s' % (filename, extension)), 'wb') as f:
        f.write(result['data'])
    return HttpResponse(result['data'], result['mime'])