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 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 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 update_renditions(item, href): inserted = [] try: content, filename, content_type = download_file_from_url(href) file_type, ext = content_type.split('/') metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) inserted.append(file_guid) rendition_spec = app.config.get('RENDITIONS', {}).get('picture', {}) renditions = generate_renditions(content, file_guid, inserted, file_type, content_type, rendition_spec, url_for_media) item['renditions'] = renditions item['mimetype'] = content_type item['filemeta'] = metadata except Exception: for file_id in inserted: app.media.delete(file_id) raise
def update_renditions(item, href, old_item): """Update renditions for an item. If the old_item has renditions uploaded in to media then the old rendition details are assigned to the item, this avoids repeatedly downloading the same image and leaving the media entries orphaned. If there is no old_item the original is downloaded and renditions are generated. :param item: parsed item from source :param href: reference to original :param old_item: the item that we have already ingested, if it exists :return: item with renditions """ inserted = [] try: # If there is an existing set of renditions we keep those if old_item: media = old_item.get('renditions', {}).get('original', {}).get('media', {}) if media: item['renditions'] = old_item['renditions'] item['mimetype'] = old_item.get('mimetype') item['filemeta'] = old_item.get('filemeta') item['filemeta_json'] = old_item.get('filemeta_json') return content, filename, content_type = download_file_from_url(href) file_type, ext = content_type.split('/') metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) inserted.append(file_guid) rendition_spec = get_renditions_spec() renditions = generate_renditions(content, file_guid, inserted, file_type, content_type, rendition_spec, url_for_media) item['renditions'] = renditions item['mimetype'] = content_type set_filemeta(item, metadata) except Exception as e: logger.exception(e) for file_id in inserted: app.media.delete(file_id) raise
def update_renditions(item, href, old_item): """ If the old_item has renditions uploaded in to media then the old rendition details are assigned to the item, this avoids repeatedly downloading the same image and leaving the media entries orphaned. If there is no old_item the original is downloaded and renditions are generated. :param item: parsed item from source :param href: reference to original :param old_item: the item that we have already injested, if it exists :return: item with renditions """ inserted = [] try: # If there is an existing set of renditions we keep those if old_item: media = old_item.get('renditions', {}).get('original', {}).get('media', {}) if media: item['renditions'] = old_item['renditions'] item['mimetype'] = old_item.get('mimetype') item['filemeta'] = old_item.get('filemeta') logger.info("Reuters image not updated for GUID:{}".format(item[GUID_FIELD])) return content, filename, content_type = download_file_from_url(href) file_type, ext = content_type.split('/') metadata = process_file(content, file_type) file_guid = app.media.put(content, filename, content_type, metadata) inserted.append(file_guid) rendition_spec = app.config.get('RENDITIONS', {}).get('picture', {}) renditions = generate_renditions(content, file_guid, inserted, file_type, content_type, rendition_spec, url_for_media) item['renditions'] = renditions item['mimetype'] = content_type item['filemeta'] = metadata except Exception: for file_id in inserted: app.media.delete(file_id) raise