Example #1
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(
            bundle.data[u'file'].name)
        file_data = bundle.data[u'file'].read()
        file_sha1 = hashlib.sha1(file_data).hexdigest()
        if file_extension:
            filename = '{}{}'.format(file_sha1, file_extension)
        else:
            filename = file_sha1
        bundle.obj.name = filename

        # add entry to the facesearch.ImageTemplate
        # image_template should be passed the openbr template instad of binary data of the image itself
        tmpl = br.br_load_img(file_data, len(file_data))
        image_template, created = ImageTemplate.objects.get_or_create(
            filename=filename, image_template=file_data)

        if created:
            print 'added new image template'
        else:
            print 'already had same image template'

        fn = helpers.get_filename_absolute(filename)
        with open(helpers.get_filename_absolute(filename),
                  'wb+') as destination_file:
            destination_file.write(file_data)
            destination_file.flush()
            os.fsync(destination_file)

            # make thumbnail on server
            from PIL import ImageFile
            ImageFile.LOAD_TRUNCATED_IMAGES = True
            image = Image.open(destination_file.name)

            # 40% the size of the original image
            # (aspect ratio could still be off, could look into that)
            original_width, original_height = image.size
            THUMB_SIZE = (original_width * 0.4, original_height * 0.4)
            image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
            file_thumbnail_name = helpers.get_fileservice_dir(
            ) + "/" + file_sha1 + "_thumb" + file_extension
            image_type = file_extension[1:]
            if image_type.lower() == 'jpg':
                image_type = 'jpeg'
            image.save(file_thumbnail_name, image_type)

        # index the file in openbr
        if u'index' not in bundle.data or 'false' != bundle.data[u'index']:
            index_face(helpers.get_filename_absolute(filename))

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #2
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name)
        file_data = bundle.data[u'file'].read()
        file_sha1 = hashlib.sha1(file_data).hexdigest()
        if file_extension:
            filename = '{}{}'.format(file_sha1, file_extension)
        else:
            filename = file_sha1
        bundle.obj.name = filename

        # add entry to the facesearch.ImageTemplate
        # image_template should be passed the openbr template instad of binary data of the image itself
        tmpl = br.br_load_img(file_data, len(file_data))
        image_template, created = ImageTemplate.objects.get_or_create(
            filename=filename,
            image_template=file_data)

        if created:
            print 'added new image template'
        else:
            print 'already had same image template'

        fn = helpers.get_filename_absolute(filename)
        with open(helpers.get_filename_absolute(filename), 'wb+') as destination_file:
            destination_file.write(file_data)
            destination_file.flush()
            os.fsync(destination_file)

            # make thumbnail on server
            from PIL import ImageFile
            ImageFile.LOAD_TRUNCATED_IMAGES = True
            image = Image.open(destination_file.name)

            # 40% the size of the original image
            # (aspect ratio could still be off, could look into that)
            original_width, original_height = image.size
            THUMB_SIZE = (original_width * 0.4, original_height * 0.4)
            image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
            file_thumbnail_name = helpers.get_fileservice_dir() + "/" + file_sha1 + "_thumb" + file_extension
            image_type = file_extension[1:]
            if image_type.lower() == 'jpg':
                image_type = 'jpeg'
            image.save(file_thumbnail_name, image_type)

        # index the file in openbr
        if u'index' not in bundle.data or 'false' != bundle.data[u'index']:
            index_face(helpers.get_filename_absolute(filename))

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #3
0
File: api.py Project: WerlingM/vida
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name)
        file_data = bundle.data[u'file'].read()
        file_sha1 = hashlib.sha1(file_data).hexdigest()
        if file_extension:
            filename = '{}{}'.format(file_sha1, file_extension)
        else:
            filename = file_sha1
        bundle.obj.name = filename
        with open(helpers.get_filename_absolute(filename), 'wb+') as destination_file:
            destination_file.write(file_data)

            # make thumbnail on server
            THUMB_SIZE = (256, 256)
            try:
                from PIL import ImageFile
                ImageFile.LOAD_TRUNCATED_IMAGES = True
                image = Image.open(destination_file.name)
            except:
                return False

            image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)

            if file_extension in ['.jpg', '.jpeg']:
                FTYPE = 'JPEG'
            elif file_extension == '.gif':
                FTYPE = 'GIF'
            elif file_extension == '.png':
                FTYPE = 'PNG'
            else:
                return False    # Unrecognized file type

            # Save thumbnail to in-memory file
            file_thumbnail_name = helpers.get_fileservice_dir() + "/" + file_sha1 + "_thumb" + file_extension
            image.save(file_thumbnail_name, FTYPE)

        # index with OpenBR
        if u'index' not in bundle.data or 'false' != bundle.data[u'index']:
            index_face(helpers.get_filename_absolute(filename))

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #4
0
    def download(self, request, **kwargs):
        '''
        example use:
        http://.../fileservice/download/med.mp4/
        or
        http://.../fileservice/med.mp4/download/
        '''
        # method check to avoid bad requests
        self.method_check(request, allowed=['get'])
        # Must be done otherwise endpoint will be wide open
        self.is_authenticated(request)

        response = None
        file_item_name = kwargs.get('name', None)
        if file_item_name:
            filename_absolute = helpers.get_filename_absolute(file_item_name)
            if os.path.isfile(filename_absolute):
                response = serve(request, os.path.basename(filename_absolute),
                                 os.path.dirname(filename_absolute))
                response['Content-Disposition'] = 'attachment; ' \
                                                  'filename="{}"'.format(
                    os.path.basename(filename_absolute))

        if not response:
            response = self.create_response(request=request,
                                            data={},
                                            response_class=HttpNotFound)

        return response
Example #5
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name)

        # -- only allow uploading of files of types specified in FILESERVICE_CONFIG.types_allowed
        types_allowed = helpers.get_fileservice_whitelist()
        if '*' not in types_allowed and file_extension.lower() not in types_allowed:
            raise BadRequest('file type is not whitelisted in FILESERVICE_CONFIG.types_allowed')

        file_data = bundle.data[u'file'].read()
        # TODO: support optional unique name generation from sha1 and uuid.
        #  file_sha1 = hashlib.sha1(file_data).hexdigest() # is file_data only the bytes without filename etc?
        #  if file_extension:
        #    filename_name = '{}{}'.format(file_sha1, file_extension)
        #  else:
        #    filename_name = file_sha1

        # TODO: if the filename uploaded is not a valid sha1, warn that it should at least be unique.
        bundle.obj.name = bundle.data[u'file'].name
        with open(helpers.get_filename_absolute(bundle.data[u'file'].name), 'wb+') as destination_file:
            destination_file.write(file_data)

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #6
0
    def download(self, request, **kwargs):
        '''
        example use:
        http://.../fileservice/download/med.mp4/
        or
        http://.../fileservice/med.mp4/download/
        '''
        # method check to avoid bad requests
        self.method_check(request, allowed=['get'])
        # Must be done otherwise endpoint will be wide open
        self.is_authenticated(request)

        response = None
        file_item_name = kwargs.get('name', None)
        if file_item_name:
            filename_absolute = helpers.get_filename_absolute(file_item_name)
            if os.path.isfile(filename_absolute):
                response = serve(request, os.path.basename(filename_absolute), os.path.dirname(filename_absolute))
                response['Content-Disposition'] = 'attachment; filename="{}"'.format(
                    os.path.basename(filename_absolute))

        if not response:
            response = self.create_response(request, {'status': 'filename not specified'})

        return response
Example #7
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name)

        # -- only allow uploading of files of types specified in FILESERVICE_CONFIG.types_allowed
        types_allowed = helpers.get_fileservice_whitelist()
        if '*' not in types_allowed and file_extension.lower() not in types_allowed:
            raise BadRequest('file type is not whitelisted in FILESERVICE_CONFIG.types_allowed')

        file_data = bundle.data[u'file'].read()
        # TODO: support optional unique name generation from sha1 and uuid.
        #  file_sha1 = hashlib.sha1(file_data).hexdigest() # is file_data only the bytes without filename etc?
        #  if file_extension:
        #    filename_name = '{}{}'.format(file_sha1, file_extension)
        #  else:
        #    filename_name = file_sha1
        bundle.obj.name = bundle.data[u'file'].name
        with open(helpers.get_filename_absolute(bundle.data[u'file'].name), 'wb+') as destination_file:
            destination_file.write(file_data)

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #8
0
File: api.py Project: WerlingM/vida
    def download(self, request, **kwargs):
        # method check to avoid bad requests
        self.method_check(request, allowed=['get'])

        response = None
        file_item = FileItemResource.get_file_item(kwargs)
        if file_item:
            filename_absolute = helpers.get_filename_absolute(file_item.name)
            if os.path.isfile(filename_absolute):
                response = serve(request, os.path.basename(filename_absolute), os.path.dirname(filename_absolute))
                response['Content-Disposition'] = 'attachment; filename="{}"'.format(os.path.basename(filename_absolute))

        if not response:
            response = self.create_response(request, {'status': 'file not found'})

        return response
Example #9
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name)
        file_data = bundle.data[u'file'].read()
        file_sha1 = hashlib.sha1(file_data).hexdigest()
        if file_extension:
            filename = '{}{}'.format(file_sha1, file_extension)
        else:
            filename = file_sha1
        bundle.obj.name = filename
        with open(helpers.get_filename_absolute(filename), 'wb+') as destination_file:
            destination_file.write(file_data)

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle
Example #10
0
    def download(self, request, **kwargs):
        # method check to avoid bad requests
        self.method_check(request, allowed=['get'])

        response = None
        file_item = FileItemResource.get_file_item(kwargs)
        if file_item:
            filename_absolute = helpers.get_filename_absolute(file_item.name)
            if os.path.isfile(filename_absolute):
                response = serve(request, os.path.basename(filename_absolute),
                                 os.path.dirname(filename_absolute))
                response[
                    'Content-Disposition'] = 'attachment; filename="{}"'.format(
                        os.path.basename(filename_absolute))

        if not response:
            response = self.create_response(request,
                                            {'status': 'file not found'})

        return response
Example #11
0
    def obj_create(self, bundle, request=None, **kwargs):
        # create a new File
        bundle.obj = FileItem()
        # full_hydrate does the heavy lifting mapping the
        # POST-ed payload key/values to object attribute/values
        bundle = self.full_hydrate(bundle)
        filename_name, file_extension = os.path.splitext(
            bundle.data[u'file'].name)
        file_data = bundle.data[u'file'].read()
        file_sha1 = hashlib.sha1(file_data).hexdigest()
        if file_extension:
            filename = '{}{}'.format(file_sha1, file_extension)
        else:
            filename = file_sha1
        bundle.obj.name = filename
        with open(helpers.get_filename_absolute(filename),
                  'wb+') as destination_file:
            destination_file.write(file_data)

        # remove the file object passed in so that the response is more concise about what this file will be referred to
        bundle.data.pop(u'file', None)
        return bundle