Skip to content

Commit fd38e19

Browse files
committed
Using metadata trough iframe et webdav
1 parent db23dd6 commit fd38e19

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

public/js/common.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,16 @@ async function storeFileInCache(file, filename, cacheStore = 'pdf') {
6767
}
6868

6969
async function loadFileFromUrl(url, pageUrl, local = null) {
70+
71+
let headers = new Headers();
72+
if(isDavPath) {
73+
showLoading('Waiting for credentials informations')
74+
let davToken = await requestDavToken();
75+
headers.set('Authorization', 'Basic ' + btoa(davToken));
76+
}
7077
showLoading('Download')
7178
history.replaceState({}, '', pageUrl);
72-
let response = await fetch(url);
79+
let response = await fetch(url, { headers: headers });
7380
if(response.status != 200) {
7481
return;
7582
}
@@ -100,6 +107,19 @@ async function loadFileFromUrl(url, pageUrl, local = null) {
100107
endLoading()
101108
}
102109

110+
async function requestDavToken(url) {
111+
if(!isIframe) {
112+
return;
113+
}
114+
window.parent.postMessage({action: 'getDavToken'}, '*');
115+
while(!davTokenReceived) {
116+
await new Promise(r => setTimeout(r, 100));
117+
}
118+
let davToken = davTokenReceived;
119+
davTokenReceived = null;
120+
return davToken;
121+
}
122+
103123
function startProcessingMode(btn) {
104124
btn.disabled = true;
105125
//btn.querySelector('.bi').classList.add('position-relative');

public/js/metadata.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ let pdfRenderTasks = [];
33
let pdffile = null
44
let deletedMetadata = [];
55
let isLocalPath = false;
6+
let isDavPath = false;
7+
let isIframe = Boolean(window.parent);
8+
let davTokenReceived = null;
69
let hasModifications = false;
710
let sizeOfText = 0;
11+
let pdf = null;
812

913
function responsiveDisplay() {
1014
let menu = document.getElementById('sidebarTools');
@@ -31,7 +35,7 @@ async function loadPDF(pdfBlob) {
3135
pdffile = pdfBlob
3236

3337
const loadingTask = pdfjsLib.getDocument(url);
34-
const pdf = await loadingTask.promise;
38+
pdf = await loadingTask.promise;
3539

3640
const metadata = await pdf.getMetadata()
3741
const attachments = await pdf.getAttachments();
@@ -226,6 +230,9 @@ window.addEventListener('message', function(event) {
226230
}
227231
setIsChanged(true);
228232
}
233+
if (event.data.action === 'davOpenFile' && event.data.key == "token") {
234+
davTokenReceived = event.data.value
235+
}
229236
});
230237

231238
function updateMetadataInfos() {
@@ -344,6 +351,19 @@ async function save() {
344351
});
345352
return ;
346353
}
354+
355+
if(window.location.hash && window.location.hash.match(/^\#dav/)) {
356+
let headers = new Headers();
357+
let davToken = await requestDavToken();
358+
headers.set('Authorization', 'Basic ' + btoa(davToken));
359+
fetch(window.location.hash.replace('#dav:', ''), {
360+
method: 'PUT',
361+
body: newPDF,
362+
headers: headers
363+
});
364+
return ;
365+
}
366+
347367
download(newPDF, document.getElementById('input_pdf_upload').files[0].name)
348368
}
349369

@@ -472,6 +492,14 @@ function createEventsListener() {
472492
}
473493
followText(e.target, '#container-pages .textLayer span', '#pdf_text span', true);
474494
});
495+
496+
document.getElementById('btn_exit').addEventListener('click', function(e) {
497+
if(isIframe) {
498+
window.parent.postMessage({action: 'exit'}, '*');
499+
e.preventDefault();
500+
return;
501+
}
502+
});
475503
}
476504

477505
function followText(element, selectorFrom, selectorTo, hide) {
@@ -507,14 +535,14 @@ async function pageMetadata(url) {
507535
document.querySelector('body').classList.add('bg-light');
508536
document.getElementById('page-upload').classList.add('d-none');
509537
document.getElementById('page-metadata').classList.remove('d-none');
510-
if(isLocalPath) {
538+
if(isLocalPath || isDavPath) {
511539
document.getElementById('save').classList.add('d-none');
512540
document.getElementById('save_mobile').classList.add('d-none');
513541
document.getElementById('save_local').classList.remove('d-none');
514542
document.getElementById('save_mobile_local').classList.remove('d-none');
515543
}
516544

517-
if(url && url.match(/^cache:\/\//)) {
545+
if(url && url.match(/^cache:\/\//) && !isDavPath) {
518546
await loadFileFromCache(url.replace(/^cache:\/\//, ''));
519547
} else if (url) {
520548
await loadFileFromUrl(url);
@@ -535,9 +563,12 @@ async function pageMetadata(url) {
535563
};
536564

537565

538-
document.addEventListener('DOMContentLoaded', function () {
566+
document.addEventListener('DOMContentLoaded', async function () {
539567
if(window.location.hash && window.location.hash.match(/^\#http/)) {
540568
pageMetadata(window.location.hash.replace(/^\#/, ''));
569+
} else if(window.location.hash && window.location.hash.match(/^\#dav:/)) {
570+
isDavPath = true;
571+
pageMetadata(window.location.hash.replace(/^\#dav:/, ''));
541572
} else if(window.location.hash && window.location.hash.match(/^\#local/)) {
542573
isLocalPath = true;
543574
pageMetadata(window.location.origin + "/api/file/get?path=" + window.location.hash.replace(/^\#local:/, ''), '/metadata', window.location.hash.replace(/^\#/, ''));

templates/metadata.html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
</div>
7575
<div id="div-margin-bottom" style="height: 55px;" class="d-md-none"></div>
7676
<div class="offcanvas offcanvas-end show d-none d-md-block shadow-sm" data-bs-backdrop="false" data-bs-scroll="true" data-bs-keyboard="false" tabindex="-1" id="sidebarTools" aria-labelledby="sidebarToolsLabel">
77-
<a class="btn btn-close btn-sm position-absolute d-none d-sm-none d-md-block" style="right: 16px; top: 16px;" title="<?php echo _("Close this PDF and return to the home page"); ?>" href="/metadata"></a>
77+
<a id="btn_exit" class="btn btn-close btn-sm position-absolute d-none d-sm-none d-md-block" style="right: 16px; top: 16px;" title="<?php echo _("Close this PDF and return to the home page"); ?>" href="/metadata"></a>
7878
<div class="offcanvas-header d-block mb-0 pb-0 border-bottom">
7979
<h5 class="mb-1 d-block w-100" id="sidebarToolsLabel"><i class="bi bi-tags"></i> <?php echo _("Edit metadata"); ?></h5>
8080
<button type="button" class="btn-close text-reset d-md-none" data-bs-dismiss="offcanvas" aria-label="Close"></button>

0 commit comments

Comments
 (0)