Beispiel #1
0
    def store_file(self, doc, content, filename, content_type):
        res = process_file_from_stream(content,
                                       filename=filename,
                                       content_type=content_type)
        file_name, content_type, metadata = res

        cropping_data = self.get_cropping_data(doc)
        _, out = crop_image(content, filename, cropping_data)
        metadata['length'] = json.dumps(len(out.getvalue()))

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            id = app.media.put(out,
                               filename=file_name,
                               content_type=content_type,
                               metadata=metadata)
            doc['media'] = id
            doc['mime_type'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            inserted = [doc['media']]
            file_type = content_type.split('/')[0]

            rendition_spec = config.RENDITIONS['avatar']
            renditions = generate_renditions(out, doc['media'], inserted,
                                             file_type, content_type,
                                             rendition_spec, url_for_media)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)
            raise SuperdeskError(message='Generating renditions failed')
Beispiel #2
0
 def crop_and_store_file(self, doc, content, filename, content_type):
     # retrieve file name and metadata from file
     file_name, content_type, metadata = process_file_from_stream(
         content, content_type=content_type)
     # crop the file if needed, can change the image size
     was_cropped, out = crop_image(content, filename, doc)
     # the length in metadata could be updated if it was cropped
     if was_cropped:
         file_name, content_type, metadata_after_cropped = process_file_from_stream(
             out, content_type=content_type)
         # when cropped, metadata are reseted. Then we update the previous metadata variable
         metadata['length'] = metadata_after_cropped['length']
     try:
         logger.debug('Going to save media file with %s ' % file_name)
         out.seek(0)
         file_id = app.media.put(out,
                                 filename=file_name,
                                 content_type=content_type,
                                 resource=self.datasource,
                                 metadata=metadata)
         doc['media'] = file_id
         doc['mimetype'] = content_type
         set_filemeta(doc, decode_metadata(metadata))
         inserted = [doc['media']]
         file_type = content_type.split('/')[0]
         rendition_spec = config.RENDITIONS['avatar']
         renditions = generate_renditions(out, file_id, inserted, file_type,
                                          content_type, rendition_spec,
                                          url_for_media)
         doc['renditions'] = renditions
     except Exception as io:
         for file_id in inserted:
             delete_file_on_error(doc, file_id)
         raise SuperdeskApiError.internalError(
             'Generating renditions failed', exception=io)
Beispiel #3
0
    def store_file(self, doc, content, filename, content_type):
        res = process_file_from_stream(content, filename=filename, content_type=content_type)
        file_name, content_type, metadata = res

        cropping_data = self.get_cropping_data(doc)
        _, out = crop_image(content, filename, cropping_data)
        metadata['length'] = json.dumps(len(out.getvalue()))

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            id = app.media.put(out, filename=file_name, content_type=content_type, metadata=metadata)
            doc['media'] = id
            doc['mime_type'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            inserted = [doc['media']]
            file_type = content_type.split('/')[0]

            rendition_spec = config.RENDITIONS['avatar']
            renditions = generate_renditions(out, doc['media'], inserted, file_type,
                                             content_type, rendition_spec, url_for_media)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)
            raise SuperdeskError(message='Generating renditions failed')
Beispiel #4
0
 def crop_and_store_file(self, doc, content, filename, content_type):
     # retrieve file name and metadata from file
     file_name, content_type, metadata = process_file_from_stream(content, content_type=content_type)
     # crop the file if needed, can change the image size
     was_cropped, out = crop_image(content, filename, doc)
     # the length in metadata could be updated if it was cropped
     if was_cropped:
         file_name, content_type, metadata_after_cropped = process_file_from_stream(out, content_type=content_type)
         # when cropped, metadata are reseted. Then we update the previous metadata variable
         metadata['length'] = metadata_after_cropped['length']
     try:
         logger.debug('Going to save media file with %s ' % file_name)
         out.seek(0)
         file_id = app.media.put(out, filename=file_name, content_type=content_type,
                                 resource=self.datasource, metadata=metadata)
         doc['media'] = file_id
         doc['mimetype'] = content_type
         doc['filemeta'] = decode_metadata(metadata)
         inserted = [doc['media']]
         file_type = content_type.split('/')[0]
         rendition_spec = config.RENDITIONS['avatar']
         renditions = generate_renditions(out, file_id, inserted, file_type,
                                          content_type, rendition_spec, url_for_media)
         doc['renditions'] = renditions
     except Exception as io:
         logger.exception(io)
         for file_id in inserted:
             delete_file_on_error(doc, file_id)
         raise SuperdeskApiError.internalError('Generating renditions failed')
Beispiel #5
0
    def find_one_raw(self, resource, _id):
        # XXX: preview is used here instead of paid download
        #      see SDNTB-15
        data = {}
        url = self._app.config['SCANPIX_SEARCH_URL'] + '/search'
        data['refPtrs'] = [_id]
        r = self._request(url, data)
        doc = r.json()['data'][0]
        self._parse_doc(doc)

        url = doc['renditions']['baseImage']['href']
        # if MIME type can't be guessed, we default to jpeg
        mime_type = mimetypes.guess_type(url)[0] or 'image/jpeg'

        r = self._request(url, data)
        out = BytesIO(r.content)
        file_name, content_type, metadata = process_file_from_stream(
            out, mime_type)

        logger.debug('Going to save media file with %s ' % file_name)
        out.seek(0)
        try:
            file_id = self._app.media.put(out,
                                          filename=file_name,
                                          content_type=content_type,
                                          metadata=None)
        except Exception as e:
            logger.exception(e)
            raise SuperdeskApiError.internalError('Media saving failed')
        else:
            try:
                inserted = [file_id]
                doc['mimetype'] = content_type
                doc['filemeta'] = decode_metadata(metadata)
                # set the version created to now to bring it to the top of the desk, images can be quite old
                doc['versioncreated'] = utcnow()
                file_type = content_type.split('/')[0]
                rendition_spec = get_renditions_spec()
                renditions = generate_renditions(out,
                                                 file_id,
                                                 inserted,
                                                 file_type,
                                                 content_type,
                                                 rendition_spec,
                                                 url_for_media,
                                                 insert_metadata=False)
                doc['renditions'] = renditions
            except (IndexError, KeyError, json.JSONDecodeError) as e:
                logger.exception("Internal error: {}".format(e))
                delete_file_on_error(doc, file_id)

                raise SuperdeskApiError.internalError(
                    'Generating renditions failed')
        return doc
 def get_file_from_document(self, doc):
     file = doc.get('media_fetched')
     if file:
         del doc['media_fetched']
     else:
         content = doc['media']
         res = process_file_from_stream(content, filename=content.filename, content_type=content.mimetype)
         file_name, content_type, metadata = res
         logger.debug('Going to save media file with %s ' % file_name)
         content.seek(0)
         id = app.media.put(content, filename=file_name, content_type=content_type, metadata=metadata)
         doc['media'] = id
         return content, content_type, decode_metadata(metadata)
     return file, file.content_type, file.metadata
 def get_file_from_document(self, doc):
     file = doc.get('media_fetched')
     if file:
         del doc['media_fetched']
     else:
         content = doc['media']
         res = process_file_from_stream(content, filename=content.filename, content_type=content.mimetype)
         file_name, content_type, metadata = res
         logger.debug('Going to save media file with %s ' % file_name)
         content.seek(0)
         id = app.media.put(content, filename=file_name, content_type=content_type, metadata=metadata)
         doc['media'] = id
         return content, content_type, decode_metadata(metadata)
     return file, file.content_type, file.metadata
Beispiel #8
0
    def get_file_from_document(self, doc):
        file = doc.get("media_fetched")
        if file:
            del doc["media_fetched"]
        else:
            content = doc["media"]
            res = process_file_from_stream(content, content_type=content.mimetype)
            file_name, content_type, metadata = res
            logger.debug("Going to save media file with %s " % file_name)
            content.seek(0)
            with timer("media:put.original"):
                doc["media"] = app.media.put(content, filename=file_name, content_type=content_type, metadata=metadata)
            return content, content_type, decode_metadata(metadata)

        return file, file.content_type, file.metadata
 def store_file(self, doc, content, filename, content_type):
     # retrieve file name and metadata from file
     file_name, content_type, metadata = process_file_from_stream(content, content_type=content_type)
     try:
         content.seek(0)
         file_id = doc['media_id']
         existing = app.media.get(doc['media_id'], self.datasource)
         if not existing:
             file_id = app.media.put(content, filename=file_name, content_type=content_type,
                                     resource=self.datasource, metadata=metadata, _id=ObjectId(doc['media_id']))
         doc['media'] = file_id
         doc['mime_type'] = content_type
         doc['filemeta'] = decode_metadata(metadata)
     except Exception as io:
         raise SuperdeskApiError.internalError('Saving file failed', exception=io)
Beispiel #10
0
    def find_one_raw(self, resource, _id):
        url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}'.format(_id)
        r = self._http.request('GET', url, headers=self._headers)
        doc = json.loads(r.data.decode('UTF-8'))
        self._parse_doc(doc)
        if 'fetch_endpoint' in doc:
            del doc['fetch_endpoint']

        # Only if we have credentials can we download the original if the account has that privilege
        if 'AAP_MM_USER' in self._app.config and 'AAP_MM_PASSWORD' in self._app.config \
                and self._app.config['AAP_MM_USER'] is not None:
            url = self._app.config[
                'AAP_MM_SEARCH_URL'] + '/Assets/{}/Original/download'.format(
                    _id)
        else:
            url = doc['renditions']['original']['href']
        r = self._http.request('GET', url, headers=self._headers)

        out = BytesIO(r.data)
        file_name, content_type, metadata = process_file_from_stream(
            out, 'image/jpeg')

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            file_id = self._app.media.put(out,
                                          filename=file_name,
                                          content_type=content_type,
                                          metadata=metadata)
            doc['mimetype'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            # set the version created to now to bring it to the top of the desk, images can be quite old
            doc['versioncreated'] = utcnow()
            inserted = [file_id]
            file_type = content_type.split('/')[0]
            rendition_spec = self._app.config['RENDITIONS']['picture']

            renditions = generate_renditions(out, file_id, inserted, file_type,
                                             content_type, rendition_spec,
                                             self.url_for_media)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)

        return doc
    def find_one_raw(self, resource, _id):
        if self._headers is None:
            self.__set_auth_cookie(self._app)

        url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}'.format(_id)
        r = self._http.request('GET', url, headers=self._headers)
        doc = json.loads(r.data.decode('UTF-8'))
        self._parse_doc(doc)
        if 'fetch_endpoint' in doc:
            del doc['fetch_endpoint']

        # Only if we have credentials can we download the original if the account has that privilege
        if 'AAP_MM_USER' in self._app.config and 'AAP_MM_PASSWORD' in self._app.config \
                and self._app.config['AAP_MM_USER'] is not None:
            url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}/Original/download'.format(_id)
        else:
            url = doc['renditions']['original']['href']
        r = self._http.request('GET', url, headers=self._headers)

        out = BytesIO(r.data)
        file_name, content_type, metadata = process_file_from_stream(out, 'image/jpeg')

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            file_id = self._app.media.put(out, filename=file_name, content_type=content_type, metadata=metadata)
            doc['mimetype'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            # set the version created to now to bring it to the top of the desk, images can be quite old
            doc['versioncreated'] = utcnow()
            inserted = [file_id]
            file_type = content_type.split('/')[0]
            rendition_spec = self._app.config['RENDITIONS']['picture']

            renditions = generate_renditions(out, file_id, inserted, file_type,
                                             content_type, rendition_spec, self.url_for_media)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)

        return doc
Beispiel #12
0
 def store_file(self, doc, content, filename, content_type):
     # retrieve file name and metadata from file
     file_name, content_type, metadata = process_file_from_stream(
         content, content_type=content_type)
     try:
         content.seek(0)
         file_id = doc['media_id']
         existing = app.media.get(doc['media_id'], self.datasource)
         if not existing:
             file_id = app.media.put(content,
                                     filename=file_name,
                                     content_type=content_type,
                                     resource=self.datasource,
                                     metadata=metadata,
                                     _id=ObjectId(doc['media_id']))
         doc['media'] = file_id
         doc['mime_type'] = content_type
         doc['filemeta'] = decode_metadata(metadata)
     except Exception as io:
         raise SuperdeskApiError.internalError('Saving file failed',
                                               exception=io)
    def find_one_raw(self, resource, _id):
        if self._headers is None:
            self.__set_auth_cookie(self._app)

        url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}'.format(_id)
        r = self._http.request('GET', url, headers=self._headers)
        doc = json.loads(r.data.decode('UTF-8'))
        self._parse_doc(doc)
        if 'fetch_endpoint' in doc:
            del doc['fetch_endpoint']

        # Only if we have credentials can we download the original if the account has that privilege
        if self._username is not None and self._password is not None:
            resolutions = self._get_resolutions(_id)
            if doc[ITEM_TYPE] == CONTENT_TYPE.PICTURE:
                if any(i['Name'] == 'Original' for i in resolutions['Image']):
                    url = self._app.config[
                        'AAP_MM_SEARCH_URL'] + '/Assets/{}/Original/download'.format(
                            _id)
                    mime_type = 'image/jpeg'
                else:
                    raise FileNotFoundError
            elif doc[ITEM_TYPE] == CONTENT_TYPE.VIDEO:
                if any(v['Name'] == 'Ipod' for v in resolutions['Video']):
                    url = self._app.config[
                        'AAP_MM_SEARCH_URL'] + '/Assets/{}/Ipod/download'.format(
                            _id)
                    mime_type = doc.get('renditions').get('original').get(
                        'mimetype')
                else:
                    raise FileNotFoundError
            else:
                raise NotImplementedError
        else:
            if doc[ITEM_TYPE] == CONTENT_TYPE.VIDEO:
                mime_type = doc.get('renditions').get('original').get(
                    'mimetype')
            else:
                mime_type = 'image/jpeg'
            url = doc['renditions']['original']['href']

        r = self._http.request('GET', url, headers=self._headers)
        out = BytesIO(r.data)
        file_name, content_type, metadata = process_file_from_stream(
            out, mime_type)

        inserted = []

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            file_id = self._app.media.put(out,
                                          filename=file_name,
                                          content_type=content_type,
                                          metadata=None)
            doc['mimetype'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            # set the version created to now to bring it to the top of the desk, images can be quite old
            doc['versioncreated'] = utcnow()
            inserted = [file_id]
            file_type = content_type.split('/')[0]
            rendition_spec = self._app.config['RENDITIONS']['picture']

            renditions = generate_renditions(out,
                                             file_id,
                                             inserted,
                                             file_type,
                                             content_type,
                                             rendition_spec,
                                             self.url_for_media,
                                             insert_metadata=False)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)

            raise SuperdeskApiError.internalError(
                'Generating renditions failed')

        return doc
Beispiel #14
0
    def find_one_raw(self, resource, _id):
        if self._headers is None:
            self.__set_auth_cookie(self._app)

        url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}'.format(_id)
        r = self._http.request('GET', url, headers=self._headers)
        doc = json.loads(r.data.decode('UTF-8'))
        self._parse_doc(doc)
        if 'fetch_endpoint' in doc:
            del doc['fetch_endpoint']

        # Only if we have credentials can we download the original if the account has that privilege
        if self._username is not None and self._password is not None:
            resolutions = self._get_resolutions(_id)
            if doc[ITEM_TYPE] == CONTENT_TYPE.PICTURE:
                if any(i['Name'] == 'Original' for i in resolutions['Image']):
                    url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}/Original/download'.format(_id)
                    mime_type = 'image/jpeg'
                else:
                    raise FileNotFoundError
            elif doc[ITEM_TYPE] == CONTENT_TYPE.VIDEO:
                if any(v['Name'] == 'Ipod' for v in resolutions['Video']):
                    url = self._app.config['AAP_MM_SEARCH_URL'] + '/Assets/{}/Ipod/download'.format(_id)
                    mime_type = doc.get('renditions').get('original').get('mimetype')
                else:
                    raise FileNotFoundError
            else:
                raise NotImplementedError
        else:
            if doc[ITEM_TYPE] == CONTENT_TYPE.VIDEO:
                mime_type = doc.get('renditions').get('original').get('mimetype')
            else:
                mime_type = 'image/jpeg'
            url = doc['renditions']['original']['href']

        r = self._http.request('GET', url, headers=self._headers)
        out = BytesIO(r.data)
        file_name, content_type, metadata = process_file_from_stream(out, mime_type)

        inserted = []

        try:
            logger.debug('Going to save media file with %s ' % file_name)
            out.seek(0)
            file_id = self._app.media.put(out, filename=file_name, content_type=content_type, metadata=None)
            doc['mimetype'] = content_type
            doc['filemeta'] = decode_metadata(metadata)
            # set the version created to now to bring it to the top of the desk, images can be quite old
            doc['versioncreated'] = utcnow()
            inserted = [file_id]
            file_type = content_type.split('/')[0]
            rendition_spec = self._app.config['RENDITIONS']['picture']

            renditions = generate_renditions(out, file_id, inserted, file_type,
                                             content_type, rendition_spec,
                                             self.url_for_media, insert_metadata=False)
            doc['renditions'] = renditions
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)

            raise SuperdeskApiError.internalError('Generating renditions failed')

        return doc
Beispiel #15
0
    def find_one_raw(self, resource, _id):
        if self._headers is None:
            self.__set_auth_cookie(self._app)

        url = self._app.config["AAP_MM_SEARCH_URL"] + "/Assets/{}".format(_id)
        r = self._http.request("GET", url, headers=self._headers)
        doc = json.loads(r.data.decode("UTF-8"))
        self._parse_doc(doc)
        if "fetch_endpoint" in doc:
            del doc["fetch_endpoint"]

        # Only if we have credentials can we download the original if the account has that privilege
        if self._username is not None and self._password is not None:
            resolutions = self._get_resolutions(_id)
            if doc["type"] == "picture":
                if any(i["Name"] == "Original" for i in resolutions["Image"]):
                    url = self._app.config["AAP_MM_SEARCH_URL"] + "/Assets/{}/Original/download".format(_id)
                    mime_type = "image/jpeg"
                    source_ref = {"href": url, "mimetype": mime_type}
                else:
                    raise FileNotFoundError
            elif doc["type"] == "video":
                if any(v["Name"] == "Ipod" for v in resolutions["Video"]):
                    url = self._app.config["AAP_MM_SEARCH_URL"] + "/Assets/{}/Ipod/download".format(_id)
                    mime_type = doc.get("renditions").get("original").get("mimetype")
                else:
                    raise FileNotFoundError
                if any(v["Name"] == "Video" for v in resolutions["Video"]):
                    source_ref = {
                        "href": self._app.config["AAP_MM_SEARCH_URL"] + "/Assets/{}/Video/download".format(_id),
                        "mimetype": "video/quicktime",
                    }
                else:
                    raise FileNotFoundError
            else:
                raise NotImplementedError
        else:
            if doc["type"] == "video":
                mime_type = doc.get("renditions").get("original").get("mimetype")
            else:
                mime_type = "image/jpeg"
            url = doc["renditions"]["original"]["href"]
            source_ref = {"href": url, "mimetype": mime_type}

        r = self._http.request("GET", url, headers=self._headers)
        out = BytesIO(r.data)
        file_name, content_type, metadata = process_file_from_stream(out, mime_type)

        try:
            logger.debug("Going to save media file with %s " % file_name)
            out.seek(0)
            file_id = self._app.media.put(out, filename=file_name, content_type=content_type, metadata=metadata)
            doc["mimetype"] = content_type
            doc["filemeta"] = decode_metadata(metadata)
            # set the version created to now to bring it to the top of the desk, images can be quite old
            doc["versioncreated"] = utcnow()
            inserted = [file_id]
            file_type = content_type.split("/")[0]
            rendition_spec = self._app.config["RENDITIONS"]["picture"]

            renditions = generate_renditions(
                out, file_id, inserted, file_type, content_type, rendition_spec, self.url_for_media
            )
            doc["renditions"] = renditions
            doc["renditions"]["original_source"] = source_ref
        except Exception as io:
            logger.exception(io)
            for file_id in inserted:
                delete_file_on_error(doc, file_id)

        return doc