|
25 | 25 | """Redirector functions and rules.""" |
26 | 26 |
|
27 | 27 | from flask import Blueprint, abort, current_app, redirect |
| 28 | +from invenio_pidstore.errors import PIDDoesNotExistError, ResolverError |
28 | 29 | from sqlalchemy.orm.exc import NoResultFound |
29 | 30 |
|
| 31 | +from cds.modules.records.resolver import record_resolver |
| 32 | + |
30 | 33 | from .resolver import get_pid_by_legacy_recid |
31 | 34 |
|
32 | 35 | HTTP_MOVED_PERMANENTLY = 301 |
@@ -58,3 +61,28 @@ def legacy_record_embed_redirect(legacy_id): |
58 | 61 |
|
59 | 62 | url_path = f"{current_app.config['SITE_URL']}/record/{pid.pid_value}/embed" |
60 | 63 | return redirect(url_path, HTTP_MOVED_PERMANENTLY) |
| 64 | + |
| 65 | + |
| 66 | +@blueprint.route("/record/<legacy_id>/files/<file_name>", strict_slashes=False) |
| 67 | +def legacy_record_file_redirect(legacy_id, file_name): |
| 68 | + """Redirect legacy recid file to new file download link.""" |
| 69 | + try: |
| 70 | + pid = get_pid_by_legacy_recid(legacy_id) |
| 71 | + _, record = record_resolver.resolve(pid_value=pid.pid_value) |
| 72 | + except (NoResultFound, PIDDoesNotExistError, ResolverError, KeyError, TypeError): |
| 73 | + abort(404) |
| 74 | + |
| 75 | + # It's project, try the first video record |
| 76 | + if not record.get("_files", []): |
| 77 | + videos = record.get("videos", []) |
| 78 | + if videos: |
| 79 | + video_pid = videos[0]["$ref"].split("/")[-1] |
| 80 | + _, record = record_resolver.resolve(pid_value=video_pid) |
| 81 | + |
| 82 | + try: |
| 83 | + file_ = next(f for f in record.get("_files", []) if f.get("key") == file_name) |
| 84 | + url = file_["links"]["self"] |
| 85 | + except (StopIteration, KeyError, TypeError): |
| 86 | + abort(404) |
| 87 | + |
| 88 | + return redirect(url, HTTP_MOVED_PERMANENTLY) |
0 commit comments