forked from Tevemadar/WebAlign
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstartpage.php
359 lines (350 loc) · 17.8 KB
/
startpage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
ob_start();
$token_params = http_build_query(array(
"grant_type" => "authorization_code",
"code" => filter_input(INPUT_GET, "code"),
"redirect_uri" => getenv("ebrains_redirect_wa"),
"client_id" => getenv("ebrains_id_wa"),
"client_secret" => getenv("ebrains_secret_wa")
));
$token_ch = curl_init(getenv("ebrains_token"));
curl_setopt_array($token_ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $token_params
));
$token_res = curl_exec($token_ch);
curl_close($token_ch);
$token_obj = json_decode($token_res, true);
$token = $token_obj["access_token"];
$json= json_decode(urldecode(filter_input(INPUT_GET, "state")), true);
$json["token"]=$token;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="dppick.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Tevemadar/NetUnzip/inflater.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Tevemadar/NetUnzip/netunzip.min.js"></script>
<style>
.error{
color: red;
font-weight: bold;
}
</style>
<script>
const state=<?php echo json_encode($json);?>;
async function dpjson(params) {
const response = await fetch(
`https://data-proxy.ebrains.eu/api/v1/buckets/${params}`,{
headers:{
accept:"application/json",
authorization:`Bearer ${state.token}`
}
});
return response.json();
}
async function startup(){
if(state.hasOwnProperty("filename")){
state.embedded=true;
location.href="webalign.html?"+encodeURIComponent(JSON.stringify(state));
return;
}
const choice=await dppick({
bucket:state["clb-collab-id"],
token:state.token,
title:"Select WebAlign descriptor",
extensions:[".waln","wwrp"],
create:".waln",
createplaceholder:"Create new WebAlign series",
createnowarn:true,
nocancel:true
});
if(choice.pick){
state.filename=choice.pick;
location.href="webalign.html?"+encodeURIComponent(JSON.stringify(state));
}else{
state.filename=choice.create;
newseries();
}
}
function newseries(){
document.getElementById("newseries").hidden=false;
document.getElementById("filename").innerText="New series "+state.filename;
document.getElementById("collab").value=state["clb-collab-id"];
document.getElementById("atlas").selectedIndex=-1;
trycollect();
}
function cancel(){
delete state.filename;
document.getElementById("newseries").hidden=true;
startup();
}
let collection;
let ctime;
function tryshow(){
// document.getElementById("filebody").innerHTML=
// collection.map(item=>`<tr><td>${item.filename.split("/").slice(-1)[0]}</td><td>${item.format}</td>
// <td>${item.width}</td><td>${item.height}</td><td>${item.tilesize}</td><td>${item.overlap}</td></tr>`).join("");
const copy=collection;
collection=[];
let error=false;
document.getElementById("filebody").innerHTML=
copy.map(item=>{
const filename=item.filename.split("/").slice(-1)[0];
const fail=!filename.match(/_s\d+/);
if(fail)
error=true;
else
collection.push(item);
return `<tr><td ${fail?"class='error'":""}>${filename}</td><td>${item.format}</td>`+
`<td>${item.width}</td><td>${item.height}</td><td>${item.tilesize}</td><td>${item.overlap}</td></tr>`;
}).join("");
if(error)
document.getElementById("log").innerText="Sections marked in red do not follow the expected numbering convention and will not be part of the series.";
else if(collection.length)
document.getElementById("log").innerText="Ready.";
document.getElementById("filetable").hidden=copy.length===0;
document.getElementById("create").disabled=collection.length===0 || document.getElementById("atlas").selectedIndex===-1;
}
let dziproot;
function clear(){
collection=[];
dziproot=false;
oldisv=false;
tryshow();
}
function dzisection(dzi,filename){
return {
filename,
width:parseInt(dzi.match(/Width="(\d+)"/m)[1]),
height:parseInt(dzi.match(/Height="(\d+)"/m)[1]),
tilesize:parseInt(dzi.match(/TileSize="(\d+)"/m)[1]),
overlap:parseInt(dzi.match(/Overlap="(\d+)"/m)[1]),
format:dzi.match(/Format="([^"]+)"/m)[1]
};
}
let dzipbundles;
let bucket;
const wfprefix=".nesysWorkflowFiles/zippedPyramids/";
async function trycollect(){
clear();
const current=ctime=Date.now();
const button=document.getElementById("create");
button.disabled=true;
const prg=document.getElementById("log");
document.getElementById("filetable").hidden=true;
const btns=document.getElementById("dzipbuttons");
btns.hidden=true;
const dzip=document.getElementById("dzip").value;
if(dzip){
bucket=document.getElementById("collab").value.replaceAll(/[^-\w().!]/g, "");
const slash=dzip.lastIndexOf("/");
dziproot=slash===-1?"":dzip.substring(0,slash+1);
const dzips=await dpjson(`${bucket}${dziproot.length?`?prefix=${dziproot}&`:"?"}delimiter=/&limit=10000`)
.then(obj=>obj.objects
.filter(item=>item.hasOwnProperty("name")&&item.name.endsWith(".dzip"))
.map(item=>item.name.substring(slash+1)));
// alert(JSON.stringify(dzips));
for(const dzip of dzips){
if(current!==ctime)return;
prg.innerText="Fetching DZI "+(collection.length+1)+"/"+dzips.length;
const zipdir=await netunzip(
()=>dpjson(`${bucket}/${dziproot}${dzip}?redirect=false`).then(json=>json.url));
for(const [_,entry] of zipdir.entries) {
if(entry.name.endsWith(".dzi")) {
const data=await zipdir.get(entry);
if(current!==ctime)return;
const dzi=new TextDecoder().decode(data);
collection.push(dzisection(dzi,dzip));
break;
}
}
}
tryshow();
return;
}
dzipbundles=new Map();
try{
bucket=document.getElementById("collab").value.replaceAll(/[^-\w().!]/g, "");
const dzips = await dpjson(`${bucket}?prefix=${wfprefix}&limit=10000`);
if(dzips.objects.length<2){
const result = await dpjson(`${bucket}?delimiter=/&limit=10000`);
if(result.hasOwnProperty("objects")){
const images=result.objects.filter(item=>item.hasOwnProperty("subdir")&&item.subdir.includes("."));
for(const image of images){
prg.innerText="Fetching DZI "+(collection.length+1)+"/"+images.length;
const subdir=image.subdir;
const pos=subdir.lastIndexOf(".");
const name=subdir.substring(0,pos);
const urljson=await dpjson(`${bucket}/${subdir+name}.dzi?redirect=false`);
const dzi=await fetch(urljson.url).then(response=>response.text());
if(current!==ctime)
return;
collection.push(dzisection(dzi,subdir.substring(0,subdir.length-1)));
}
}
} else {
for(const item of dzips.objects) {
const parts=item.name.substring(wfprefix.length).split("/");
if(parts.length===2 && parts[1].endsWith(".dzip")) {
if(!dzipbundles.has(parts[0]))
dzipbundles.set(parts[0],[]);
dzipbundles.get(parts[0]).push(parts[1]);
}
}
dzipbundles.forEach((v,k)=>btns.innerHTML+=`<button onclick="dzicollect('${k}')">${k} (${v.length})</button> `);
btns.hidden=dzipbundles.size===0;
}
}catch(ex){console.log(ex);}
tryshow();
}
async function dzicollect(dzipbundle){
clear();
const current=ctime=Date.now();
const prg=document.getElementById("log");
const dzips=dzipbundles.get(dzipbundle);
for(let i=0;i<dzips.length;i++) {
if(current!==ctime)return;
prg.innerText="Fetching DZI "+(collection.length+1)+"/"+dzips.length;
const dzip=dzips[i];
const zipdir=await netunzip(
()=>dpjson(`${bucket}/${wfprefix}${dzipbundle}/${dzip}?redirect=false`).then(json=>json.url));
for(const [_,entry] of zipdir.entries) {
if(entry.name.endsWith(".dzi")) {
const data=await zipdir.get(entry);
if(current!==ctime)return;
const dzi=new TextDecoder().decode(data);
collection.push(dzisection(dzi,dzipbundle+"/"+dzip));
break;
}
}
}
tryshow();
}
// https://object.cscs.ch/v1/AUTH_08c08f9f119744cbbf77e216988da3eb/imgsvc-be74b890-2c14-4404-b187-678ab8cacc9e/ext-d000018_mouse3_calb_s193.tif/ext-d000018_mouse3_calb_s193.dzi
// https://localizoom.apps.hbp.eu/filmstripzoom.html?atlas=ABA_Mouse_CCFv3_2017_25um&series=https://object.cscs.ch/v1/AUTH_4791e0a3b3de43e2840fe46d9dc2b334/ext-d000018_CalbindinDistr-NormalMouse_pub/Mouse3/mouse3_nonlinear_lz.json&pyramids=imgsvc-be74b890-2c14-4404-b187-678ab8cacc9e&tools&nl
let oldisv;
async function import_link(event) {
clear();
const link=event.target.value;
if(!link.startsWith("https://localizoom.apps.hbp.eu/filmstripzoom.html?"))
return;
const params=link.split("?")[1].split("&").reduce((acc,item)=>{
const pair=item.split("=");
acc.set(pair[0],pair.length===1?true:pair[1]);
return acc;
},new Map());
if(params.get("pyramids").startsWith("buckets/")){
bucket=document.getElementById("collab").value=params.get("pyramids").substring("buckets/".length);
}else{
bucket=false;
document.getElementById("collab").value="---";
oldisv=params.get("pyramids");
}
const select=document.getElementById("atlas");
atlas.selectedIndex=-1;
for(let i=0;i<atlas.options.length;i++)
if(atlas.options[i].value===params.get("atlas"))
atlas.selectedIndex=i;
// bucket=document.getElementById("collab").value=params.get("pyramids").substring("buckets/".length);
const series=await fetch(params.get("series")).then(response=>response.json());
collection=await Promise.all(series.slices.map(async slice=>{
const filename=slice.filename;
const name=filename.substring(0,filename.lastIndexOf("."))
let dzi,section;
try {
dzi=await fetch((bucket?"https://data-proxy.ebrains.eu/api/v1/buckets/":"https://object.cscs.ch/v1/AUTH_08c08f9f119744cbbf77e216988da3eb/")+
`${bucket?bucket:oldisv}/${filename}/${name}.dzi`)
.then(response=>response.text());
section=dzisection(dzi,filename);
} catch(ex) {
dzi=await fetch((bucket?"https://data-proxy.ebrains.eu/api/v1/buckets/":"https://object.cscs.ch/v1/AUTH_08c08f9f119744cbbf77e216988da3eb/")+
`${bucket?bucket:oldisv}/${name}.tif/${name}.dzi`)
.then(response=>response.text());
section=dzisection(dzi,name+".tif");
}
if(slice.hasOwnProperty("anchoring"))
section.ouv=slice.anchoring;
if(slice.hasOwnProperty("markers"))
section.markers=slice.markers.map(marker=>({
x:marker[0]*section.width/slice.width,
y:marker[1]*section.height/slice.height,
nx:marker[2]*section.width/slice.width,
ny:marker[3]*section.height/slice.height
}));
return section;
}));
tryshow();
}
async function dzip(){
const choice=await dppick({
bucket:document.getElementById("collab").value,
token:state.token,
title:"Pick a DZIP file",
extensions:[".dzip"]
});
if(choice.pick){
document.getElementById("dzip").value=choice.pick;
trycollect();
}
}
async function create(){
document.getElementById("create").disabled=true;
const series={
atlas:document.getElementById("atlas").value,
sections:collection
};
if(bucket)series.bucket=bucket;
if(oldisv)series.oldisv=oldisv;
if(dziproot!==false)series.dziproot=dziproot;
const upload=await fetch(
`https://data-proxy.ebrains.eu/api/v1/buckets/${state["clb-collab-id"]}/${state.filename}`,{
method: "PUT",
headers:{
accept:"application/json",
authorization:`Bearer ${state.token}`
}
}
).then(response=>response.json());
if (!upload.hasOwnProperty("url")) {
document.getElementById("log").innerHTML=("Possible error happened:<br>" + JSON.stringify(upload));
return;
}
await fetch(upload.url, {
method: "PUT",
headers: {
'Content-Type': 'application/x.webalign'
},
body: JSON.stringify(series)
});
location.href="webalign.html?"+encodeURIComponent(JSON.stringify(state));
}
</script>
</head>
<body onload="startup()">
<div id="newseries" hidden>
<div id="filename"></div>
<input oninput="import_link(event)" placeholder="Import LocaliZoom link"><br><br>
Enter name of image-chunk collab: <input id="collab" oninput="trycollect()"><br>
DZIP: <input id="dzip" oninput="trycollect()"> <button onclick="dzip()">Pick...</button><br>
Target atlas:
<select id="atlas" onchange="tryshow()">
<option value="WHS_SD_Rat_v4_39um">WHS SD Rat v4 39um</option>
<option value="WHS_SD_Rat_v3_39um">WHS SD Rat v3 39um</option>
<option value="ABA_Mouse_CCFv3_2017_25um">ABA Mouse CCFv3 2017 25um</option>
</select><br>
<button id="create" onclick="create()" disabled>Create</button><button onclick="cancel()">Cancel</button>
<pre id="log"></pre>
<div id="dzipbuttons"></div>
<table id="filetable">
<thead>
<th>name</th><th>format</th><th>width</th><th>height</th><th>tilesize</th><th>overlap</th>
</thead>
<tbody id="filebody"></tbody>
</table>
</div>
</body>
</html>