Beispiel #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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
Beispiel #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 settings.UPLOAD_LIMIT
Beispiel #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
Beispiel #7
0
def get_image_for_record(record,
                         user=None,
                         width=100000,
                         height=100000,
                         passwords={},
                         crop_to_square=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/')
    if PDF_SUPPORT:
        q = q | Q(mimetype='application/pdf')
    print "Media: " + str(media)
    media = media.select_related('storage').filter(q)

    if not media:
        print "NOT MEDIA"
        return None
    print "IS MEDIA"
    print str(media)
    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:
        width = min(width, restrictions.get('width', width))
        height = min(height, restrictions.get('height', height))

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

        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)
            import ImageFile
            ImageFile.MAXBLOCK = 16 * 1024 * 1024
            from multimedia import get_image
            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)
                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.error('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:
            if not os.path.exists(sp):
                try:
                    os.makedirs(sp)
                except:
                    # check if directory exists now, if so another process may have created it
                    if not os.path.exists(sp):
                        # still does not exist, raise error
                        raise
            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
Beispiel #8
0
def get_image_for_record(recordid, user=None, width=100000, height=100000, passwords={}, crop_to_square=False):
    media = get_media_for_record(recordid, 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/')
    if PDF_SUPPORT:
        q = q | Q(mimetype='application/pdf')
    if DEBUG:
        print "Media: "+str(media)
    media = media.select_related('storage').filter(q)
    print 'i am a filtered media ' +str(media)
    if not media:
        if DEBUG:
            print "NOT MEDIA"
        return None
    if DEBUG:
        print "IS MEDIA"
        print str(media)
    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:
        width = min(width, restrictions.get('width', width))
        height = min(height, restrictions.get('height', height))

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

        def derivative_image(master, width, height):
            if not master.file_exists():
                print('Image derivative failed for media %d, cannot find file "%s"' % (master.id, master.get_absolute_file_path()))
                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
            from multimedia import get_image
            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)
                output = StringIO.StringIO()
                if image.mode != "RGB":
                    image = image.convert("RGB")
                print 'line 154 storage.__init__'
                image.save(output, 'JPEG', quality=85, optimize=True)
                #print 'output get value=' + str(output.getvalue())
                if not image:
                    print 'image is null'
                else: 
                    print 'image.size=' + str(image.size)
                return output.getvalue(), image.size
            except Exception, e:
                logging.error('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 '')
        if DEBUG:
            print 'M is a '+ str(type(m))
        storage_path = m.storage.get_derivative_storage_path()
        if storage_path:
            if not os.path.exists(storage_path):
                try:
                    os.makedirs(storage_path)
                except:
                    # check if directory exists now, if so another process may have created it
                    if not os.path.exists(storage_path):
                        # still does not exist, raise error
                        raise
            path = os.path.join(storage_path, name)
            if DEBUG:
                print 'storage/__init__/180 ~ path ='+path
            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:
                    if DEBUG:
                        print 'No derivative Image present'
                    return None
            return path

        else:
            if DEBUG:
                print 'line 185'
            return None
Beispiel #9
0
def get_image_for_record(record, user=None, width=100000, height=100000, passwords={}, crop_to_square=False):
    print 'getting image from record'
    media = get_media_for_record(record, user, passwords)
    #print "Media: " + media
    q = Q(mimetype__startswith='image/')
    print q
	# TODO: Work out what happened to the audio support between 1.2 -> 1.8
    #if settings.FFMPEG_EXECUTABLE:
    #    # also support video and audio
    #    q = q | Q(mimetype__startswith='video/') | Q(mimetype__startswith='audio/')
    #if PDF_SUPPORT:
    #    q = q | Q(mimetype='application/pdf')

    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:
            log.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():

        def derivative_image(master, width, height):
            if not master.file_exists():
                print('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:
                #print('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:
                    print("Returning None")
                    return None
            return path

        else:
            print("Returning None")
            return None
Beispiel #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'