Example #1
0
def get_allowed_collections_for_personal_records(user, readable_collections):
    allowed = []
    for c in Collection.objects.filter(id__in=readable_collections):
        restrictions = get_effective_permissions_and_restrictions(user, c)[3]
        if not restrictions or \
                restrictions.get('personal', 'yes').lower() != 'no':
            allowed.append(c.id)
    return allowed
Example #2
0
def get_allowed_collections_for_personal_records(user, readable_collections):
    allowed = []
    for c in Collection.objects.filter(id__in=readable_collections):
        restrictions = get_effective_permissions_and_restrictions(user, c)[3]
        if not restrictions or \
                restrictions.get('personal', 'yes').lower() != 'no':
            allowed.append(c.id)
    return allowed
Example #3
0
 def get_upload_limit(self, user):
     if user.is_superuser:
         return 0
     r, w, m, restrictions = get_effective_permissions_and_restrictions(user, self)
     if restrictions:
         try:
             return int(restrictions["uploadlimit"])
         except (ValueError, KeyError):
             pass
     return settings.UPLOAD_LIMIT
Example #4
0
 def is_downloadable_by(self, user, original=True):
     r, w, m, restrictions = get_effective_permissions_and_restrictions(
         user, self.storage)
     # if size or download restrictions exist, no direct download of a
     # media file is allowed
     if (restrictions and ((original and 'width' in restrictions) or
                           (original and 'height' in restrictions)
                           or restrictions.get('download', 'yes') == 'no')):
         return False
     else:
         return r
Example #5
0
 def get_upload_limit(self, user):
     if user.is_superuser:
         return 0
     r, w, m, restrictions = get_effective_permissions_and_restrictions(
         user, self)
     if restrictions:
         try:
             return int(restrictions['uploadlimit'])
         except (ValueError, KeyError):
             pass
     return getattr(settings, 'UPLOAD_LIMIT', 0)
Example #6
0
 def is_downloadable_by(self, user):
     r, w, m, restrictions = get_effective_permissions_and_restrictions(user, self.storage)
     # if size or download restrictions exist, no direct download of a media file is allowed
     if restrictions and (
         restrictions.has_key("width")
         or restrictions.has_key("height")
         or restrictions.get("download", "yes") == "no"
     ):
         return False
     else:
         return r
Example #7
0
 def is_downloadable_by(self, user, original=True):
     r, w, m, restrictions = get_effective_permissions_and_restrictions(
         user, self.storage)
     # if size or download restrictions exist, no direct download of a
     # media file is allowed
     if (restrictions and
             ((original and 'width' in restrictions) or
              (original and 'height' in restrictions) or
              restrictions.get('download', 'yes') == 'no')):
         return False
     else:
         return r
Example #8
0
def _check_playable(user, media):
    restrictions = get_effective_permissions_and_restrictions(
        user, media.storage)[3]
    return not restrictions or restrictions.get('download') != 'only'
Example #9
0
def get_image_for_record(record,
                         user=None,
                         width=100000,
                         height=100000,
                         passwords={},
                         crop_to_square=False,
                         force_reprocess=False,
                         loris_name=False):
    media = get_media_for_record(record, user, passwords)
    q = Q(mimetype__startswith='image/')
    if settings.FFMPEG_EXECUTABLE:
        # also support video and audio
        q = q | Q(mimetype__startswith='video/') | \
            Q(mimetype__startswith='audio/')
    q = q | Q(mimetype='application/pdf') | Q(url__endswith='.pptx')

    media = media.select_related('storage').filter(q)

    if not media:
        return None
    map(lambda m: m.identify(lazy=True), media)
    media = sorted(media, _imgsizecmp, reverse=True)
    # find matching media
    last = None
    for m in media:
        if m.width > width or m.height > height:
            # Image still larger than given dimensions
            last = m
        elif (m.width == width and m.height <= height) or \
                (m.width <= width and m.height == height):
            # exact match
            break
        else:
            # Now we have a smaller image
            m = last or m
            break

    # m is now equal or larger to requested size, or smaller but closest
    # to the requested size

    # check what user size restrictions are
    restrictions = get_effective_permissions_and_restrictions(user,
                                                              m.storage)[3]
    if restrictions:
        try:
            width = min(width, int(restrictions.get('width', width)))
            height = min(height, int(restrictions.get('height', height)))
        except ValueError:
            logging.exception('Invalid height/width restrictions: %s' %
                              repr(restrictions))

    # see if image needs resizing
    if (m.width > width or m.height > height or m.mimetype != 'image/jpeg'
            or not m.is_local() or force_reprocess):

        def derivative_image(master, width, height):
            if not master.file_exists():
                logging.error('Image derivative failed for media %d, '
                              'cannot find file "%s"' %
                              (master.id, master.get_absolute_file_path()))
                return None, (None, None)
            from PIL import ImageFile
            ImageFile.MAXBLOCK = 16 * 1024 * 1024
            # Import here to avoid circular reference
            # TODO: need to move all these functions out of __init__.py
            from multimedia import get_image, overlay_image_with_mimetype_icon
            try:
                file = get_image(master)
                image = Image.open(file)
                if crop_to_square:
                    w, h = image.size
                    if w > h:
                        image = image.crop(
                            ((w - h) / 2, 0, (w - h) / 2 + h, h))
                    elif w < h:
                        image = image.crop(
                            (0, (h - w) / 2, w, (h - w) / 2 + w))
                image.thumbnail((width, height), Image.ANTIALIAS)
                image = overlay_image_with_mimetype_icon(
                    image, master.mimetype)
                output = StringIO.StringIO()
                if image.mode != "RGB":
                    image = image.convert("RGB")
                image.save(output, 'JPEG', quality=85, optimize=True)
                return output.getvalue(), image.size
            except Exception, e:
                logging.exception('Image derivative failed for media %d (%s)' %
                                  (master.id, e))
                return None, (None, None)

        # See if a derivative already exists
        name = '%s-%sx%s%s.jpg' % (m.id, width, height,
                                   'sq' if crop_to_square else '')
        sp = m.storage.get_derivative_storage_path()
        if sp:
            path = os.path.join(sp, name)

            if not os.path.exists(path) or os.path.getsize(path) == 0:
                output, (w, h) = derivative_image(m, width, height)
                if output:
                    with file(path, 'wb') as f:
                        f.write(output)
                else:
                    return None
            return path

        else:
            return None
Example #10
0
def _check_playable(user, media):
    restrictions = get_effective_permissions_and_restrictions(
        user, media.storage)[3]
    return not restrictions or restrictions.get('download') != 'only'