def dispatch(cls, collection_id, meta): local_path = archive.load_file(meta) try: best_cls = cls.auction_file(meta, local_path) log.debug("Dispatching %r to %r", meta.file_name, best_cls) best_cls(collection_id).ingest(meta, local_path) db.session.commit() except Exception as exc: cls.handle_exception(meta, collection_id, exc) finally: archive.cleanup_file(meta)
def write_document(export_dir, zf, collection, entity): content_hash = entity.first("contentHash", quiet=True) if content_hash is None: return file_name = entity_filename(entity) arcname = "{0}-{1}".format(entity.id, file_name) arcname = os.path.join(collection.get("label"), arcname) try: local_path = archive.load_file(content_hash, temp_path=export_dir) if local_path is not None and os.path.exists(local_path): zf.write(local_path, arcname=arcname) finally: archive.cleanup_file(content_hash, temp_path=export_dir)
def retrieve(): """Downloads a binary blob from the blob storage archive. --- get: summary: Download a blob from the archive parameters: - description: Authorization token for an archive blob in: query name: claim schema: type: string description: A signed JWT with the object hash. responses: '200': description: OK content: '*/*': {} '404': description: Object does not exist. tags: - Archive """ token = request.args.get("token") token = jwt.decode(token, key=settings.SECRET_KEY, verify=True) content_hash = token.get("c") file_name = token.get("f") mime_type = token.get("m") expire = datetime.utcfromtimestamp(token["exp"]) tag_request(content_hash=content_hash, file_name=file_name) url = archive.generate_url( content_hash, file_name=file_name, mime_type=mime_type, expire=expire, ) if url is not None: return redirect(url) try: local_path = archive.load_file(content_hash) if local_path is None: return Response(status=404) return send_file( str(local_path), as_attachment=True, conditional=True, attachment_filename=file_name, mimetype=mime_type, ) finally: archive.cleanup_file(content_hash)
def retrieve(): """Downloads a binary blob from the blob storage archive. --- get: summary: Download a blob from the archive parameters: - description: Authorization token for an archive blob in: query name: claim schema: type: string description: A signed JWT with the object hash. responses: '200': description: OK content: '*/*': {} '404': description: Object does not exist. tags: - Archive """ claim = request.args.get("claim") role_id, content_hash, file_name, mime_type = archive_claim(claim) require(request.authz.id == role_id) tag_request(content_hash=content_hash, file_name=file_name) url = archive.generate_url(content_hash, file_name=file_name, mime_type=mime_type) if url is not None: return redirect(url) try: local_path = archive.load_file(content_hash) if local_path is None: return Response(status=404) return send_file( str(local_path), as_attachment=True, conditional=True, attachment_filename=file_name, mimetype=mime_type, ) finally: archive.cleanup_file(content_hash)
def _serve_archive(content_hash, file_name, mime_type): """Serve a file from the archive or by generating an external URL.""" url = archive.generate_url(content_hash, file_name=file_name, mime_type=mime_type) if url is not None: return redirect(url) try: local_path = archive.load_file(content_hash, file_name=file_name) if local_path is None: return Response(status=404) return send_file(local_path, as_attachment=True, conditional=True, attachment_filename=file_name, mimetype=mime_type) finally: archive.cleanup_file(content_hash)
def _serve_archive(content_hash, file_name, mime_type): """Serve a file from the archive or by generating an external URL.""" url = archive.generate_url(content_hash, file_name=file_name, mime_type=mime_type) if url is not None: return redirect(url) enable_cache() try: local_path = archive.load_file(content_hash, file_name=file_name) if local_path is None: raise NotFound("File does not exist.") return send_file(open(local_path, 'rb'), as_attachment=True, attachment_filename=file_name, mimetype=mime_type) finally: archive.cleanup_file(content_hash)
def dispatch(cls, source_id, meta): best_score, best_cls = 0, None local_path = archive.load_file(meta) try: for cls in get_ingestors().values(): score = cls.match(meta, local_path) if score > best_score: best_score = score best_cls = cls if best_cls is None: log.debug("No ingestor found for: %r", meta.file_name) return log.debug("Dispatching %r to %r", meta.file_name, best_cls.__name__) best_cls(source_id).ingest(meta, local_path) except Exception as ex: log.exception(ex) db.session.rollback() finally: archive.cleanup_file(meta)
def retrieve(): claim = request.args.get('claim') role_id, content_hash, file_name, mime_type = archive_claim(claim) require(request.authz.id == role_id) tag_request(content_hash=content_hash, file_name=file_name) url = archive.generate_url(content_hash, file_name=file_name, mime_type=mime_type) if url is not None: return redirect(url) try: local_path = archive.load_file(content_hash) if local_path is None: return Response(status=404) return send_file(str(local_path), as_attachment=True, conditional=True, attachment_filename=file_name, mimetype=mime_type) finally: archive.cleanup_file(content_hash)
def retrieve(): claim = request.args.get('claim') role_id, content_hash, file_name, mime_type = archive_claim(claim) require(request.authz.id == role_id) record_audit(Audit.ACT_ARCHIVE, content_hash=content_hash) tag_request(content_hash=content_hash, file_name=file_name) url = archive.generate_url(content_hash, file_name=file_name, mime_type=mime_type) if url is not None: return redirect(url) try: local_path = archive.load_file(content_hash) if local_path is None: return Response(status=404) return send_file(local_path, as_attachment=True, conditional=True, attachment_filename=file_name, mimetype=mime_type) finally: archive.cleanup_file(content_hash)