def get(self, arkid, premisid): """ Get the whole record """ from flask import current_app whitelist_opened = open(current_app.config["WHITELIST"], "r") check = False for line in whiteliste_opened: if line.strip() == join(arkid, premisid): check = True break if not check: return abort(404, message="could not find the {}".format(join(arkid, presform.objid))) try: user = "******" if "private" in request.environ.get("REQUEST_URI") else "anonymous" event_category = "anonymous download" if user == "anonymous" else "authorized download" data = get_object_halves(arkid, premisid, current_app.config["LONGTERMSTORAGE_PATH"], current_app.config["LIVEPREMIS_PATH"]) if not data: return abort(404, message="{} cannot be found".format(join(arkid, premisid))) else: attach_filename, mimetype = get_an_attachment_filename(data[1]) record_path = join(current_app.config["LIVEPREMIS_PATH"], str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(premisid)), "arf", "premis.xml") make_download_event(record_path, event_category, datetime.now().isoformat(), "SUCCESS", user, data[1].objid) resp = send_file(data[1].content_loc, as_attachment=True, attachment_filename=attach_filename, mimetype=mimetype) return resp except Exception as e: return jsonify(_EXCEPTION_HANDLER.handle(e).dictify())
def get_data_half_of_object(arkid, premisid, lp_path): arkid_path = str(identifier_to_path(arkid)) premisid_path = str(identifier_to_path(premisid)) path_to_premis = join(lp_path, arkid_path,"arf", "pairtree_root", premisid_path, "arf", "premis.xml") if exists(path_to_premis): return (path_to_premis, extract_identity_data_from_premis_record(path_to_premis)) else: return None
def get_content_half_of_object(arkid, premisid, lts_path): arkid_path = str(identifier_to_path(arkid)) premisid_path = str(identifier_to_path(premisid)) path_to_content = join(lts_path, arkid_path, "arf", "pairtree_root", premisid_path, "arf", "content.file") if exists(path_to_content): return (path_to_content, None) else: return None
def get(self, arkid, premisid, numbered_request): """a method to to retrieve one particular presform related to the identified object __Args__ 1. arkid (str): an accession identifier 2. premisid (str): an object identifier 3. numbered_request (int): a numbered related object that is desired """ from flask import current_app try: user = "******" if "private" in request.environ.get( "REQUEST_URI") else "anonymous" event_category = "anonymous download" if user == "anonymous" else "authorized download" data = get_object_halves(arkid, premisid, current_app.config["LIVEPREMIS_PATH"]) related_objects = data[1].related_objects output = {} tally = 1 for n_object in related_objects: output[str(tally)] = {"loc": join("/", arkid, n_object)} choice = output.get(numbered_request) if choice: presform = get_object_halves( arkid, choice.get("loc").split("/")[2], current_app.config["LONGTERMSTORAGE_PATH"]) if presform: record_path = join( current_app.config["LIVEPREMIS_PATH"], str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(presform[1].objid)), "arf", "premis.xml") make_download_event(record_path, event_category, datetime.now().isoformat(), "SUCCESS", user, presform[1].objid) attach_filename, mimetype = get_an_attachment_filename( data) resp = send_file(content_item, as_attachment=True, attachment_filename=attach_filename, mimetype=mimetype) return jsonify(resp.dictify()) else: return abort(404, message="could not find the {}".format( join(arkid, presform.objid))) else: return abort(404, message="could not find {}".format( join(arkid, premisid))) except Exception as e: return jsonify(_EXCEPTION_HANDLER.handle(e).dictify())
def get_object(self, id): content_path = Path( self.lts_root, identifier_to_path(id), "arf", "content.file" ) if not content_path.is_file(): raise ObjectNotFoundError(str(id)) return open(str(content_path))
def del_object(self, id): content_path = Path( self.lts_root, identifier_to_path(id), "arf", "content.file" ) if not content_path.exists(): return True remove(str(content_path)) return True
def set_object(self, id, content): content_path = Path( self.lts_root, identifier_to_path(id), "arf", "content.file" ) if self.check_object_exists(id): raise ObjectAlreadyExistsError(str(id)) makedirs(str(content_path.parent), exist_ok=True) content.save(str(content_path))
def edit_a_premis_agent(dto): identifier = dto.identifier pairtree_identifier = str(identifier_to_path(identifier)) path_to_agent_record = create_agent_path(dto, identifier) record_to_edit = PremisRecord(frompath=path_to_agent_record) agents_list = record_to_edit.get_agent_list() agent_node = agents_list[0] for n_field in dto.edit_fields: if n_field == "name": agent_node.set_agentName(getattr(dto, n_field)) elif n_field == "type": agent_node.set_agentType(getattr(dto, n_field)) agent_list = [agent_node] record_to_edit = PremisRecord(agents=agent_list) try: write_a_premis_record(record_to_edit, path_to_agent_record) return (True, identifier) except IOError: return (False, None)
def add_event_to_a_premis_agent(dto): """a function to add a PREMIS event to a particular premis record __Args__ 1. premis_record (PremisRecord) an instance of pyremis.lib.PremisRecord 2. an_event (Event): an instance of pypremis.nodes.Event """ path_to_agent_record = join(dto.root, str(identifier_to_path(dto.identifier)), "prf", "agent.xml") record_to_edit = PremisRecord(frompath=path_to_agent_record) agents = record_to_edit.get_agent_list() agent = agents[0] stderr.write(dto.event) new_linked_event = LinkingEventIdentifier("DOI", dto.event) stderr.write(str(new_linked_event)) agent.add_linkingEventIdentifier(new_linked_event) records_to_edit = PremisRecord(agents=[agent]) write_a_premis_record(record_to_edit, path_to_agent_record) return True
def __init__(self, arkid, objid, whitelist=True): """the initialization method for the AnInstanceItem class __Args__ 1. arkid (str): a string nice opaque universal identifier for a particular accession that contains premis record objects 2. objid (str): a string unique universal identifier for a particular premis record object __KWARgs__ 1. whitelist (Boolean): a flag for whether or not to determine of the content is authorized to be distributed publicly """ self.arkid = arkid self.objid = objid self.whitelist = "/disk/0/repositoryCode/apistorage/publicurls/whitelist.txt" self.content_root = "/data/repository/longTermStorage" self.premis_root = "/data/repository/livePremis" self.premisid_path = identifier_to_path(objid) self.arkid_path = identifier_to_path(arkid) self.premis_file_path = join(self.premis_root, str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(objid)), "arf", "premis.xml") self.content_file_path = join(self.content_root, str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(objid)), "arf", "content.file") self.premis_core_data = extract_identity_data_from_premis_record(self.premis_file_path) self.mimetype = self.premis_core_data.mimetype self.extension = self.mimetype.split('/')[1] if whitelist: if self.get_identifier() in open(self.whitelist, "r").readlines(): self.public = True else: self.public = False
def create_agent_path(dto, identifier): path = join(dto.root, str(identifier_to_path(identifier)), "prf", "agent.xml") return path
def check_object_exists(self, id): content_path = Path( self.lts_root, identifier_to_path(id), "arf", "content.file" ) return content_path.is_file()
def get(self, arkid, premisid): """a method to get the last related object if there is one or the content file __Args__ 1. arkid (str): an accession identifier 2. premisid (str): an object identifier """ from flask import current_app try: user = request.headers.get("uid") if request.headers.get( "uid") else "anonymous" event_category = "anononymous download" if user == "anonymous" else "restricted download" data = get_object_halves( arkid, premisid, current_app.config["LONGTERMSTORAGE_PATH"], current_app.config["LIVEPREMIS_PATH"]) related_objects = data[1].related_objects if len(related_objects) > 0: output = {} tally = 1 for n_object in related_objects: output[str(tally)] = {"loc": join("/", arkid, n_object)} last_related_object = list(output.keys())[-1] choice = output.get(last_related_object) if choice: presform = get_object_halves( arkid, choice.get("loc").split("/")[2], current_app.config["LONGTERMSTORAGE_PATH"]) if presform: record_path = join( current_app.config["LIVEPREMIS_PATH"], str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(presform[1].objid)), "arf", "premis.xml") make_download_event(record_path, event_category, datetime.now().isoformat(), "SUCCESS", user, presform[1].objid) attach_filename, mimetype = get_an_attachment_filename( presform[1]) resp = send_file(presform[1].content_loc, as_attachment=True, attachment_filename=attach_filename, mimetype=mimetype) return resp else: return abort(404, message="could not find the {}".format( join(arkid, presform.objid))) else: return abort(404, message="could not find {}".format( join(arkid, premisid))) else: record_path = join(current_app.config["LIVEPREMIS_PATH"], str(identifier_to_path(arkid)), "arf", "pairtree_root", str(identifier_to_path(premisid)), "arf", "premis.xml") make_download_event(record_path, event_category, datetime.now().isoformat(), "SUCCESS", user, data[1].objid) attach_filename, mimetype = get_an_attachment_filename(data[1]) resp = send_file(data[1].content_loc, as_attachment=True, attachment_filename=attach_filename, mimetype=mimetype) return resp except Exception as e: return jsonify(_EXCEPTION_HANDLER.handle(e).dictify())