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
    def view(self, request, **kwargs):
        '''
        allow a file to be viewed as opposed to download. This is particularly
        needed when a video file is stored
        in the fileservice and user wants to be able to use a view the
        video as opposed to having to download it
        first. It passes the serving of the file to nginx/apache which will
        return all the proper headers allowing,
        say, html5's video viewer's 'seek' indicator/knob to work. Otherwise
         the video is only played sequentially

        Note that nginx/apache need to be configured accordingly. nginx
        for example:
        location /var/lib/geoserver_data/file-service-store/ {
           # forces requests to be authorized
           internal;
           alias   /var/lib/geoserver_data/file-service-store/;
        }

        for apache, need to install xsendfile module, enable it, set
        the path and then
        XSendFile on
        XSendFilePath /var/lib/geoserver_data/file-service-store

        example use:
        /fileservice/view/med.mp4
        or
        /fileservice/med.mp4/view

        Note that media players tend to require the route to end with the
        filename like /fileservice/view/med.mp4
        '''

        # 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)

        file_item_name = kwargs.get('name', None)
        url = urllib.pathname2url(file_item_name)
        mime = MimeTypes()
        mime_type = mime.guess_type(url)
        response = HttpResponse(content_type=mime_type[0])
        file_with_route = smart_str('{}{}'.format(
            helpers.get_fileservice_dir(), file_item_name))
        # apache header
        response['X-Sendfile'] = file_with_route
        # nginx header
        response['X-Accel-Redirect'] = file_with_route

        return response
Example #4
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 #5
0
    def view(self, request, **kwargs):
        '''
        allow a file to be viewed as opposed to download. This is particularly needed when a video file is stored
        in the fileservice and user wants to be able to use a view the video as opposed to having to download it
        first. It passes the serving of the file to nginx/apache which will return all the proper headers allowing,
        say, html5's video viewer's 'seek' indicator/knob to work. Otherwise the video is only played sequentially

        Note that nginx/apache need to be configured accordingly. nginx for example:
        location /var/lib/geoserver_data/file-service-store/ {
           # forces requests to be authorized
           internal;
           alias   /var/lib/geoserver_data/file-service-store/;
        }

        for apache, need to install xsendfile module, enable it, set the path and then
        XSendFile on
        XSendFilePath /var/lib/geoserver_data/file-service-store

        example use:
        /fileservice/view/med.mp4
        or
        /fileservice/med.mp4/view

        Note that media players tend to require the route to end with the filename like /fileservice/view/med.mp4
        '''
        # 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:
            mime = MimeTypes()
            url = urllib.pathname2url(file_item_name)
            mime_type = mime.guess_type(url)
            response = HttpResponse(content_type=mime_type[0])
            file_with_route = smart_str('{}{}'.format(helpers.get_fileservice_dir(), file_item_name))
            # apache header
            response['X-Sendfile'] = file_with_route
            # nginx header
            response['X-Accel-Redirect'] = file_with_route

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

        return response