Skip to content

Commit 4a7a2a7

Browse files
committed
download_links and archived_image pages load download_urls.tsv and can checkLinks
1 parent d8b2ecc commit 4a7a2a7

File tree

3 files changed

+162
-65
lines changed

3 files changed

+162
-65
lines changed
Lines changed: 140 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,148 @@
1-
2-
{% extends "idr_gallery/base.html" %}
3-
4-
{% block content %}
1+
{% extends "idr_gallery/base.html" %} {% block content %}
52

63
<style>
7-
article {
8-
max-width: 800px;
9-
margin: 0 auto;
10-
padding: 20px;
11-
}
4+
article {
5+
max-width: 800px;
6+
margin: 0 auto;
7+
padding: 20px;
8+
}
9+
ul {
10+
list-style: none;
11+
background: white;
12+
margin: 0;
13+
padding: 15px;
14+
margin-bottom: 20px;
15+
border-radius: 5px;
16+
}
1217
</style>
1318

1419
<article>
15-
<h1>Image is not viewable</h1>
16-
<h2>Study: {{ idr_study }}</h2>
17-
<h2>Image Name: {{ image.name }}</h2>
18-
<h3>ID: {{ image.id }}</h3>
19-
<p>
20-
Due to infrastructure limitations, this image is not viewable.
21-
The IDR only supports the viewing of OME-Zarr images.
22-
</p>
23-
24-
{% if fileset %}
25-
<h3>Image Files</h3>
26-
<p>The following files are associated with this image and can be downloaded for offline viewing or analysis:</p>
27-
<ul>
28-
{% for file in fileset.file_urls %}
29-
<li>
30-
{% if file.url %}
31-
<a href="{{ file.url }}">{{ file.path }}</a>
32-
{% else %}
33-
{{ file.path }}
34-
{% endif %}
35-
</li>
36-
{% endfor %}
37-
</ul>
38-
{% else %}
39-
<p>No associated fileset.</p>
40-
{% endif %}
41-
42-
<ul style="background: white;">
43-
<li>Image Path: {{ img_path }}</li>
44-
<li>Data Location: {{ data_location }}</li>
45-
<li>Is Zarr?: {{ is_zarr }}</li>
46-
<li>Download URL: <a href="{{ download_url }}">{{ download_url }}</a></li>
47-
<li>BIA NGFF ID: {{ bia_ngff_id }}</li>
48-
</ul>
20+
<h1>This Image is not viewable</h1>
21+
<h2>Image Name: {{ image.name }}</h2>
22+
<strong>ID: {{ image.id }}</strong><br />
23+
<strong>Study: {{ idr_study }}</strong>
24+
<p>
25+
Due to infrastructure limitations, this image is not viewable. The IDR only
26+
supports the viewing of OME-Zarr images.
27+
</p>
28+
29+
{% if fileset %}
30+
<h3>Image Files</h3>
31+
{% if fileset.file_urls|length == 1 %}
32+
<p>There is 1 file associated with this image:</p>
33+
{% else %}
34+
<p>
35+
There are {{ fileset.file_urls|length }} files associated with this image
36+
which can be downloaded from the links below. For more download options,
37+
including bulk download using Globus or FTP clients, see the
38+
<a href="/about/download.html">IDR Data download page</a>.
39+
</p>
40+
{% endif %}
41+
<ul>
42+
{% for file in fileset.file_urls %}
43+
<li>
44+
{% if file.url %}
45+
<a class="file_download" href="{{ file.url }}">{{ file.path }}</a>
46+
{% else %} {{ file.path }} {% endif %}
47+
</li>
48+
{% endfor %}
49+
</ul>
50+
{% else %}
51+
<p>No associated fileset.</p>
52+
{% endif %} Extra info (debug)
53+
<ul style="background: white">
54+
<li>Image Path: {{ img_path }}</li>
55+
<li>Data Location: {{ data_location }}</li>
56+
<li>Is Zarr?: {{ is_zarr }}</li>
57+
<li>Download URL: <a href="{{ download_url }}">{{ download_url }}</a></li>
58+
<li>BIA NGFF ID: {{ bia_ngff_id }}</li>
59+
</ul>
4960
</article>
5061

62+
<script>
63+
const IDR_ID_NAME = "{{ idr_study }}";
64+
let IDR_ID = IDR_ID_NAME.split("-")[0];
65+
let DOWNLOAD_URL = "{{ download_url }}";
66+
67+
function checkLinks() {
68+
let links = Array.from(document.querySelectorAll("a"));
69+
// links = links.slice(0,50)
70+
links.forEach((link) => {
71+
// ignore image viewer links
72+
if (!link.href || link.href.includes("img_detail")) return;
73+
// no cors-origin check for now, just check if the link is valid
74+
fetch(link.href, { method: "HEAD" })
75+
.then((response) => {
76+
link.style.color = response.ok ? "green" : "orange";
77+
link.title = response.ok ? "Link is valid" : "Link is broken";
78+
})
79+
.catch((error) => {
80+
// show "pink" and re-try with server-side check, to avoid CORS issues
81+
console.error("Error checking link:", error);
82+
link.style.color = "pink";
83+
link.title = "Error checking link";
84+
85+
fetch(`/link_check/?url=${encodeURIComponent(link.href)}`)
86+
.then((res) => res.json())
87+
.then((data) => {
88+
console.log("Link check data:", data);
89+
if (data.is_valid) {
90+
link.style.color = "green";
91+
link.title = "Link is valid (checked with server)";
92+
} else {
93+
link.style.color = "red";
94+
link.title = `Link is broken: ${data.error}`;
95+
}
96+
});
97+
});
98+
});
99+
}
100+
101+
document.addEventListener("DOMContentLoaded", function () {
102+
// Load download URLs from TSV file in GitHub to fix the download URL for this image
103+
fetch(
104+
"https://raw.githubusercontent.com/will-moore/idr.openmicroscopy.org/refs/heads/download_urls/_data/download_urls.tsv",
105+
)
106+
.then((response) => response.text())
107+
.then((data) => {
108+
const lines = data.split("\n");
109+
110+
// Use first line as header to find column names...
111+
const headers = lines[0].split("\t");
112+
113+
const idrIndex = headers.indexOf("idrid");
114+
115+
// Find the line for this IDR ID
116+
const studyLine = lines.find(
117+
(line) => line.split("\t")[idrIndex] === IDR_ID,
118+
);
119+
console.log(`Found line for ${IDR_ID}:`, studyLine);
120+
if (studyLine) {
121+
const cells = studyLine.split("\t");
122+
const downloadUrlIndex = headers.indexOf("download_url");
123+
// downloadUrl e.g. https://ftp.ebi.ac.uk/pub/databases/IDR/idr0047-neuert-yeastmrna/
124+
const downloadUrl = cells[downloadUrlIndex];
125+
const baseUrl = downloadUrl.split(IDR_ID)[0]; // e.g. https://ftp.ebi.ac.uk/pub/databases/IDR/
126+
console.log(`Download URL for ${IDR_ID}:`, downloadUrl);
127+
if (downloadUrl) {
128+
// update all ".file_download" links to this download URL
129+
const fileDownloadLinks =
130+
document.querySelectorAll(".file_download");
131+
fileDownloadLinks.forEach((link) => {
132+
link.href = link.href.replace(DOWNLOAD_URL, baseUrl);
133+
});
134+
}
135+
} else {
136+
console.warn(`No download URL found for ${IDR_ID} in TSV file`);
137+
}
138+
139+
// After updating links, check them if ?check_links=1 is in the URL
140+
const urlParams = new URLSearchParams(window.location.search);
141+
if (urlParams.get("check_links")) {
142+
checkLinks();
143+
}
144+
});
145+
});
146+
</script>
147+
51148
{% endblock %}

idr_gallery/templates/idr_gallery/download_urls.html

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,16 @@ <h1>Download URLs</h1>
2828
let links = Array.from(document.querySelectorAll('a'));
2929
// links = links.slice(0,50)
3030
links.forEach(link => {
31-
// no corss-origin check for now, just check if the link is valid
31+
// ignore image viewer links
32+
if (!link.href || link.href.includes("img_detail")) return;
33+
// no cors-origin check for now, just check if the link is valid
3234
fetch(link.href, { method: 'HEAD'})
3335
.then(response => {
34-
// console.log("response.ok", response.ok, link.href);
3536
link.style.color = response.ok ? 'green' : 'orange';
3637
link.title = response.ok ? 'Link is valid' : 'Link is broken';
37-
38-
if (!response.ok) {
39-
// Use our custom link check endpoint to get more details on why the link is broken
40-
// fetch(`/link_check/?url=${encodeURIComponent(link.href)}`)
41-
// .then(res => res.json())
42-
// .then(data => {
43-
// console.log('Link check data:', data);
44-
// if (data.is_valid) {
45-
// link.style.color = 'blue';
46-
// link.title = 'Link is valid (checked with server)';
47-
// } else {
48-
// link.style.color = 'yellow';
49-
// link.title = `Link is broken: ${data.error}`;
50-
// }
51-
// });
52-
}
5338
})
5439
.catch(error => {
40+
// show "pink" and re-try with server-side check, to avoid CORS issues
5541
console.error('Error checking link:', error);
5642
link.style.color = 'pink';
5743
link.title = 'Error checking link';
@@ -97,6 +83,7 @@ <h1>Download URLs</h1>
9783
let clientPathIndex = headers.indexOf('client_path');
9884
let downloadUrlIndex = headers.indexOf('download_url');
9985
let idrIdIndex = headers.indexOf('idrid');
86+
let imageIdIndex = headers.indexOf('image_id');
10087

10188
// Create table body - update client_path to URL, based on the download_url column...
10289
const tbody = table.createTBody();
@@ -120,13 +107,22 @@ <h1>Download URLs</h1>
120107
const td = document.createElement('td');
121108
td.style.border = '1px solid #ddd';
122109
td.style.padding = '2px';
110+
// handle links or 'client_path' column...
123111
if (cellText.startsWith('http') || (headers[colIndex] === 'client_path' && clientPathUrl)) {
124112
const link = document.createElement('a');
125113
link.href = cellText.startsWith('http') ? cellText : clientPathUrl;
126114
link.textContent = cellText;
127115
link.target = '_blank';
128116
td.textContent = '';
129117
td.appendChild(link);
118+
} else if (colIndex == imageIdIndex) {
119+
// Add link to image viewer at /webclient/img_detail/1884807/
120+
const link = document.createElement('a');
121+
link.href = `/webclient/img_detail/${cellText}/?check_links=true`;
122+
link.textContent = cellText;
123+
link.target = '_blank';
124+
td.textContent = '';
125+
td.appendChild(link);
130126
} else {
131127
td.textContent = cellText;
132128
}

idr_gallery/views.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def image_viewer(request, iid, conn=None, **kwargs):
555555
if data_location == "IDR" or data_location == "Github":
556556
# then link to Download e.g. https://ftp.ebi.ac.uk/pub/databases/IDR/idr0002-heriche-condensation/
557557
# e.g. idr0002-heriche-condensation
558-
download_url = f"https://ftp.ebi.ac.uk/pub/databases/IDR/{idrid_name}"
558+
download_url = f"https://ftp.ebi.ac.uk/pub/databases/IDR/"
559559

560560
if data_location == "Embassy_S3":
561561
# "mkngff" data is at https://uk1s3.embassy.ebi.ac.uk/bia-integrator-data/pages/idr_ngff_data.html
@@ -577,10 +577,14 @@ def image_viewer(request, iid, conn=None, **kwargs):
577577
if image.fileset is not None:
578578
paths = image.getImportedImageFilePaths()
579579
file_urls = []
580+
idrid = idrid_name.split("-")[0] # e.g. idr0002
580581
for path in paths["client_paths"]:
581-
if idrid_name in path:
582-
file_path = path.split(idrid_name, 1)[-1]
583-
file_urls.append({"url": f"{download_url}{file_path}", "path": file_path})
582+
if idrid in path:
583+
file_path = path.split(idrid, 1)[-1]
584+
# url_prefix = download_url.split(idrid, 1)[0] if download_url else ""
585+
# url encode the file path to handle spaces and special characters
586+
file_path = urllib.parse.quote(file_path)
587+
file_urls.append({"url": f"{download_url}{idrid}{file_path}", "path": file_path})
584588
else:
585589
file_urls.append({"url": None, "path": path})
586590
fileset_id = image.fileset.id.val

0 commit comments

Comments
 (0)