def validate_url_validity(item, errors): """ Checks url for validity """ url = item.get("url") item_id = item.get("id") if url is None: return for idx, single_url in enumerate(url): if not utils.is_url_valid(single_url, item): errors.add( f"Field url with value [{single_url}] and number #{idx} is wrong" ) if not utils.is_url_self_served(single_url): continue match = utils.SELF_SERVED_URL_REGEXP.match(single_url) if not match: errors.add( f"Self served url [{single_url}] doesn't match SELF_SERVED_URL_REGEXP" ) continue if (match.group("item_id") != item_id): errors.add("Wrong item_id specified in self-served url") continue single_filename, single_filesize = utils.get_file_info_from_url( single_url, item) metadata = utils.extract_metadata_from_file(single_filename) owners = metadata.get("owner").split("+") if not owners: errors.add( f"Owner specification expected for self-served url #{number} [{url}], stored at [{single_filename}]" ) continue for owner in owners: owner_fullname = config.parser.bookkeepers.get(owner) if owner_fullname: note = item.get("note") if note is None: errors.add( f"Owner fullname ({owner_fullname}) should be present in note, but the note is missing" ) elif owner_fullname not in note: errors.add( f"Owner fullname ({owner_fullname}) should be present in note, but it is not" )
def get_book_pdf(book_id, index): """ TODO: I'm a huge method that isn't easy to read Please, refactor me ASAP """ utils_flask.require(index > 0, http.client.NOT_FOUND, "Param index should be positive number") items = item_index["id"].get(book_id, None) if items is None: flask.abort(http.client.NOT_FOUND, f"Book with id {book_id} was not found") item = utils.first(items) request_uri = flask.request.path item_urls = item.get("url") or set() filenames = item.get("filename") is_url_valid = ((request_uri in item_urls) and utils.is_url_local(request_uri) and utils.is_url_self_served(request_uri) and index <= len(filenames)) utils_flask.require( is_url_valid, http.client.NOT_FOUND, f"Book with id {book_id} is not available for download") filename = filenames[index - 1] pdf_full_path = os.path.join(config.www.elibrary_dir, filename) if not os.path.isfile(pdf_full_path): message = f"Item {book_id} metadata is wrong: file for {request_uri} is missing" logging.error(message) flask.abort(http.client.INTERNAL_SERVER_ERROR, message) logging.info(f"Sending pdf file: {pdf_full_path}") if config.unittest_mode: #using send_file in unittest mode causes ResourceWarning due to unclosed file response = flask.make_response("SOME_BINARY_PDF_LIKE_DATA") response.headers["Content-Type"] = "application/pdf" response.headers["Content-Disposition"] = "attachment" return response else: basename = os.path.basename(pdf_full_path) return flask.send_file(pdf_full_path, as_attachment=True, attachment_filename=basename)
def validate_url_validity(item, errors): """ Checks url for validity """ url = item.get("url") item_id = item.get("id") if url is None: return for idx, single_url in enumerate(url): if not utils.is_url_valid(single_url, item): errors.add(f"Field url with value [{single_url}] and number #{idx} is wrong") if not utils.is_url_self_served(single_url): continue match = utils.SELF_SERVED_URL_REGEXP.match(single_url) if not match: errors.add(f"Self served url [{single_url}] doesn't match SELF_SERVED_URL_REGEXP") continue if (match.group("item_id") != item_id): errors.add("Wrong item_id specified in self-served url") continue single_filename, single_filesize = utils.get_file_info_from_url(single_url, item) metadata = utils.extract_metadata_from_file(single_filename) owners = metadata.get("owner").split("+") if not owners: errors.add(f"Owner specification expected for self-served url #{number} [{url}], stored at [{single_filename}]") continue for owner in owners: owner_fullname = config.parser.bookkeepers.get(owner) if owner_fullname: note = item.get("note") if note is None: errors.add(f"Owner fullname ({owner_fullname}) should be present in note, but the note is missing") elif owner_fullname not in note: errors.add(f"Owner fullname ({owner_fullname}) should be present in note, but it is not")
def from_url(single_url, item): if utils.is_url_self_served(single_url): return Availability.AvailableHere else: return Availability.AvailableElsewhere