Ejemplo n.º 1
0
def thumbnail_create(request, repo_id):

    content_type = 'application/json; charset=utf-8'
    result = {}

    path = request.GET.get('path')
    size = request.GET.get('size', THUMBNAIL_DEFAULT_SIZE)
    obj_id = get_file_id_by_path(repo_id, path)
    raw_path, inner_path, user_perm = get_file_view_path_and_perm(
        request, repo_id, obj_id, path)

    if user_perm is None:
        err_msg = _(u"Permission denied.")
        return HttpResponse(json.dumps({"err_msg": err_msg}),
                            status=403,
                            content_type=content_type)

    repo = get_repo(repo_id)
    if repo.encrypted:
        err_msg = _(
            u"Image thumbnail is not supported in encrypted libraries.")
        return HttpResponse(json.dumps({"err_msg": err_msg}),
                            status=403,
                            content_type=content_type)

    if not ENABLE_THUMBNAIL:
        err_msg = _(u"Thumbnail function is not enabled.")
        return HttpResponse(json.dumps({"err_msg": err_msg}),
                            status=403,
                            content_type=content_type)

    open_file = urllib2.urlopen(raw_path)
    file_size = int(open_file.info()['Content-Length'])
    if file_size > THUMBNAIL_IMAGE_SIZE_LIMIT * 1024**2:
        # if file is bigger than 30MB
        err_msg = _(u"Image file is too large.")
        return HttpResponse(json.dumps({"err_msg": err_msg}),
                            status=520,
                            content_type=content_type)

    thumbnail_dir = os.path.join(THUMBNAIL_ROOT, size)
    if not os.path.exists(thumbnail_dir):
        os.makedirs(thumbnail_dir)

    thumbnail_file = os.path.join(thumbnail_dir, obj_id)
    if not os.path.exists(thumbnail_file):
        try:
            f = StringIO(open_file.read())
            image = Image.open(f)
            image.thumbnail((int(size), int(size)), Image.ANTIALIAS)
            image.save(thumbnail_file, THUMBNAIL_EXTENSION)
        except Exception as e:
            logger.error(e)
            return HttpResponse(json.dumps({'success': False}),
                                status=500,
                                content_type=content_type)

    result['thumbnail_src'] = get_thumbnail_src(repo_id, obj_id, size)
    return HttpResponse(json.dumps(result), content_type=content_type)
Ejemplo n.º 2
0
    def test_can_get(self):
        obj_id = seafile_api.get_file_id_by_path(self.repo.id, self.file)

        rst = get_file_view_path_and_perm(self.request, self.repo.id, obj_id,
                                          self.file)
        assert '8082' in rst[0]
        assert '8082' in rst[1]
        assert rst[2] == 'rw'
    def test_can_get(self):
        obj_id = seafile_api.get_file_id_by_path(self.repo.id, self.file)

        rst = get_file_view_path_and_perm(self.request, self.repo.id, obj_id,
                                          self.file)
        assert '8082' in rst[0]
        assert '8082' in rst[1]
        assert rst[2] == 'rw'
Ejemplo n.º 4
0
def thumbnail_create(request, repo_id):

    content_type = 'application/json; charset=utf-8'
    result = {}

    path = request.GET.get('path')
    size = request.GET.get('size', THUMBNAIL_DEFAULT_SIZE)
    obj_id = get_file_id_by_path(repo_id, path)
    raw_path, inner_path, user_perm = get_file_view_path_and_perm(request,
                                                                  repo_id,
                                                                  obj_id, path)

    if user_perm is None:
        err_msg = _(u"Permission denied.")
        return HttpResponse(json.dumps({"err_msg": err_msg}), status=403,
                            content_type=content_type)

    repo = get_repo(repo_id)
    if repo.encrypted:
        err_msg = _(u"Image thumbnail is not supported in encrypted libraries.")
        return HttpResponse(json.dumps({"err_msg": err_msg}), status=403,
                            content_type=content_type)

    if not ENABLE_THUMBNAIL:
        err_msg = _(u"Thumbnail function is not enabled.")
        return HttpResponse(json.dumps({"err_msg": err_msg}), status=403,
                            content_type=content_type)

    thumbnail_dir = os.path.join(THUMBNAIL_ROOT, size)
    if not os.path.exists(thumbnail_dir):
        os.makedirs(thumbnail_dir)

    thumbnail_file = os.path.join(thumbnail_dir, obj_id)
    if not os.path.exists(thumbnail_file):
        try:
            f = StringIO(urllib2.urlopen(raw_path).read())
            image = Image.open(f)
            image.thumbnail((int(size), int(size)), Image.ANTIALIAS)
            image.save(thumbnail_file, THUMBNAIL_EXTENSION)
        except Exception as e:
            logger.error(e)
            return HttpResponse(json.dumps({'success': False}), status=500, content_type=content_type)

    result['thumbnail_src'] = get_thumbnail_src(repo_id, obj_id, size)
    return HttpResponse(json.dumps(result), content_type=content_type)