def body_hook(self, item, html): """Copy content to body_html if img are found in the content, they are uploaded. First image is used as feature media, then there are embeds """ if "img" in html: content = sd_etree.parse_html(html, 'html') for img in content.xpath('//img'): src = img.get('src') try: key, media_data = self._add_image(item, src) except Exception as e: logger.error(e) img.getparent().remove(img) continue _id = media_data['_id'] url = url_for_media(_id) img.set("src", url) if key == 'featuremedia': # no need to embed the image for featuremedia continue embed_start = etree.Comment(embed_TPL.format('START', key)) embed_end = etree.Comment(embed_TPL.format('END', key)) img.addprevious(embed_start) img.addnext(embed_end) html = etree.tostring(content, encoding="unicode") item['body_html'] = html
def import_rendition(guid, rendition_name, href, extract_metadata, trigger_events): archive = superdesk.apps["archive"].find_one(req=None, guid=guid) if not archive: msg = "No document found in the media archive with this ID: %s" % guid raise superdesk.SuperdeskError(payload=msg) if rendition_name not in archive["renditions"]: payload = "Invalid rendition name %s" % rendition_name raise superdesk.SuperdeskError(payload=payload) updates = {} metadata = None content, filename, content_type = download_file_from_url(href) if extract_metadata: file_type, ext = content_type.split("/") metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) # perform partial update updates["renditions." + rendition_name + ".href"] = url_for_media(file_guid) updates["renditions." + rendition_name + ".media"] = file_guid result = superdesk.apps["archive"].update(id=guid, updates=updates, trigger_events=trigger_events) return result
def attachments_hook(self, item, attachments): """Attachment are parsed at the end if it's the first image found, it's used as feature media else it's used as embed and put at the end of body_html """ for url in attachments: try: key, media_data = self._add_image(item, url) except Exception as e: logger.error(e) continue if key == 'featuremedia': # no need to embed the image for featuremedia continue embed_start = "<!--" + embed_TPL.format('START', key) + "-->" embed_end = "<!--" + embed_TPL.format('END', key) + "-->" _id = media_data['_id'] new_url = url_for_media(_id) img = '<img src={src} height="{height}" width="{width}">'.format( src=quoteattr(new_url), height=media_data['renditions']['original']['height'], width=media_data['renditions']['original']['width']) item[ 'body_html'] += '<div>' + embed_start + img + embed_end + '</div>'
def import_rendition(guid, rendition_name, href, extract_metadata, trigger_events): archive = superdesk.apps['archive'].find_one(req=None, guid=guid) if not archive: msg = 'No document found in the media archive with this ID: %s' % guid raise superdesk.SuperdeskError(payload=msg) if rendition_name not in archive['renditions']: payload = 'Invalid rendition name %s' % rendition_name raise superdesk.SuperdeskError(payload=payload) updates = {} metadata = None content, filename, content_type = download_file_from_url(href) if extract_metadata: file_type, ext = content_type.split('/') metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) # perform partial update updates['renditions.' + rendition_name + '.href'] = url_for_media(file_guid) updates['renditions.' + rendition_name + '.media'] = file_guid result = superdesk.apps['archive'].update(id=guid, updates=updates, trigger_events=trigger_events) return result
def _save_cropped_image(self, file_stream, original, doc): """ Saves the cropped image and returns the crop dictionary :param file_stream: cropped image stream :param original: original rendition :param doc: crop data :return dict: Crop values :raises SuperdeskApiError.internalError """ crop = {} try: file_name, content_type, metadata = process_file_from_stream( file_stream, content_type=original.get('mimetype')) file_stream.seek(0) file_id = superdesk.app.media.put(file_stream, filename=file_name, content_type=content_type, resource='upload', metadata=metadata) crop['media'] = file_id crop['mimetype'] = content_type crop['href'] = url_for_media(file_id, content_type) crop['CropTop'] = doc.get('CropTop', None) crop['CropLeft'] = doc.get('CropLeft', None) crop['CropRight'] = doc.get('CropRight', None) crop['CropBottom'] = doc.get('CropBottom', None) return crop except Exception as ex: try: superdesk.app.media.delete(file_id) except: pass raise SuperdeskApiError.internalError( 'Generating crop failed: {}'.format(str(ex)))
def _save_cropped_image(self, file_stream, original, doc): """Saves the cropped image and returns the crop dictionary :param file_stream: cropped image stream :param original: original rendition :param doc: crop data :return dict: Crop values :raises SuperdeskApiError.internalError """ crop = {} try: file_name, content_type, metadata = process_file_from_stream( file_stream, content_type=original.get("mimetype") ) file_stream.seek(0) file_id = app.media.put( file_stream, filename=file_name, content_type=content_type, resource="upload", metadata=metadata ) crop["media"] = file_id crop["mimetype"] = content_type crop["href"] = url_for_media(file_id, content_type) crop["CropTop"] = doc.get("CropTop", None) crop["CropLeft"] = doc.get("CropLeft", None) crop["CropRight"] = doc.get("CropRight", None) crop["CropBottom"] = doc.get("CropBottom", None) return crop except Exception as ex: try: app.media.delete(file_id) except Exception: pass raise SuperdeskApiError.internalError("Generating crop failed: {}".format(str(ex)), exception=ex)
def _save_cropped_image(self, file_stream, original, doc): """ Saves the cropped image and returns the crop dictionary :param file_stream: cropped image stream :param original: original rendition :param doc: crop data :return dict: Crop values :raises SuperdeskApiError.internalError """ crop = {} try: file_name, content_type, metadata = process_file_from_stream(file_stream, content_type=original.get('mimetype')) file_stream.seek(0) file_id = superdesk.app.media.put(file_stream, filename=file_name, content_type=content_type, resource='upload', metadata=metadata) crop['media'] = file_id crop['mimetype'] = content_type crop['href'] = url_for_media(file_id, content_type) crop['CropTop'] = doc.get('CropTop', None) crop['CropLeft'] = doc.get('CropLeft', None) crop['CropRight'] = doc.get('CropRight', None) crop['CropBottom'] = doc.get('CropBottom', None) return crop except Exception as ex: try: superdesk.app.media.delete(file_id) except: pass raise SuperdeskApiError.internalError('Generating crop failed: {}'.format(str(ex)))
def import_rendition(guid, rendition_name, href, extract_metadata): archive = superdesk.get_resource_service(ARCHIVE).find_one(req=None, guid=guid) if not archive: msg = 'No document found in the media archive with this ID: %s' % guid raise superdesk.SuperdeskError(payload=msg) if rendition_name not in archive['renditions']: payload = 'Invalid rendition name %s' % rendition_name raise superdesk.SuperdeskError(payload=payload) updates = {} metadata = None content, filename, content_type = download_file_from_url(href) if extract_metadata: file_type, ext = content_type.split('/') metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) # perform partial update updates['renditions.' + rendition_name + '.href'] = url_for_media(file_guid) updates['renditions.' + rendition_name + '.media'] = file_guid result = superdesk.get_resource_service(ARCHIVE).patch(guid, updates=updates) return result
def zip_items(result_id, items_ids): result_id = ObjectId(result_id) archive_service = get_resource_service('archive') vppzip_service = get_resource_service('verifiedpixel_zip') results_service = get_resource_service('verification_results') item = vppzip_service.find_one(_id=result_id, req=None) vppzip_service.system_update(result_id, {'status': "processing"}, item) items = list(archive_service.get_from_mongo( req=ParsedRequest(), lookup={'_id': {'$in': items_ids}})) verification_ids = [item['verification']['results'] for item in items] verification_results = { result['_id']: result for result in list(results_service.get_from_mongo( req=ParsedRequest(), lookup={'_id': {'$in': verification_ids}}) ) } verification_data_object = StringIO() verification_data = {} zip_file_object = BytesIO() zip_file = zipfile.ZipFile(zip_file_object, mode='w') for item in items: item_id = item['_id'] image = get_original_image(item, 'archive')[1] zip_file.writestr(item_id, image) item['verification']['results'] = verification_results[ item['verification']['results'] ] for field in ['_id', '_etag', '_created', '_updated']: del item['verification']['results'][field] verification_data[item_id] = item['verification'] json.dump(verification_data, verification_data_object) zip_file.writestr('verification.json', verification_data_object.getvalue()) zip_file.close() uploaded_zip_id = app.media.put( zip_file_object.getvalue(), filename="{name}_{date}.zip".format( name=items[0].get('slugline', None) or 'image' if len(items) == 1 else 'images', date=datetime.now().isoformat() ), content_type='application/zip', resource=vppzip_service.datasource, metadata={} ) uploaded_zip_url = url_for_media(uploaded_zip_id) item = vppzip_service.find_one(_id=result_id, req=None) vppzip_service.system_update(result_id, { "status": "done", "result": uploaded_zip_url, "result_id": uploaded_zip_id }, item) push_notification( 'verifiedpixel_zip:ready', id=str(result_id), url=uploaded_zip_url )
def zip_items(result_id, items_ids): archive_service = get_resource_service('archive') vppzip_service = get_resource_service('verifiedpixel_zip') results_service = get_resource_service('verification_results') vppzip_service.patch( result_id, {'status': "processing"}, ) items = list(archive_service.get_from_mongo( req=ParsedRequest(), lookup={'_id': {'$in': items_ids}})) verification_ids = [item['verification']['results'] for item in items] verification_results = { result['_id']: result for result in list(results_service.get_from_mongo( req=ParsedRequest(), lookup={'_id': {'$in': verification_ids}}) ) } verification_data_object = StringIO() verification_data = {} zip_file_object = BytesIO() zip_file = zipfile.ZipFile(zip_file_object, mode='w') for item in items: item_id = item['_id'] image = get_original_image(item)[1] zip_file.writestr(item_id, image) item['verification']['results'] = verification_results[ item['verification']['results'] ] for field in ['_id', '_etag', '_created', '_updated']: del item['verification']['results'][field] verification_data[item_id] = item['verification'] json.dump(verification_data, verification_data_object) zip_file.writestr('verification.json', verification_data_object.getvalue()) zip_file.close() uploaded_zip_id = app.media.put( zip_file_object.getvalue(), filename=str(items_ids), content_type='application/zip', resource=vppzip_service.datasource, metadata={} ) uploaded_zip_url = url_for_media(uploaded_zip_id) vppzip_service.patch(result_id, { "status": "done", "result": uploaded_zip_url, "result_id": uploaded_zip_id })
def import_rendition(guid, rendition_name, href, trigger_events): archive = superdesk.apps['archive'].find_one(req=None, guid=guid) if not archive: msg = 'No document found in the media archive with this ID: %s' % guid raise superdesk.SuperdeskError(payload=msg) if rendition_name not in archive['renditions']: payload = 'Invalid rendition name %s' % rendition_name raise superdesk.SuperdeskError(payload=payload) file_guid = download_file_from_url(href) updates = {} # perform partial update updates['renditions.' + rendition_name + '.href'] = url_for_media(file_guid) result = superdesk.apps['archive'].update(id=guid, updates=updates, trigger_events=trigger_events) return result
def prepare_href(self, href, mimetype=None): return url_for_media(href, mimetype)
def prepare_href(self, href): return url_for_media(href)