Skip to content

Commit

Permalink
The signature works without the cache and therefore in incognito mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wincelau committed Oct 1, 2024
1 parent f6946ba commit ac8dba4
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 186 deletions.
146 changes: 146 additions & 0 deletions public/js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
function is_mobile() {
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
};

async function canUseCache() {
try {
cache = await caches.open('pdf');
return true;
} catch (e) {
return false;
}
};

async function loadFileFromCache(cacheUrl, pageUrl) {
if(!await canUseCache()) {
document.location = pageUrl;
return false;
}
const cache = await caches.open('pdf');
let responsePdf = await cache.match(cacheUrl);

if(!responsePdf) {
return;
}

let filename = cacheUrl.replace('/pdf/', '');

let pdfBlob = await responsePdf.blob();

let dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
}

async function storeFileInCache() {
let cache = await caches.open('pdf');
let filename = document.getElementById('input_pdf_upload').files[0].name;
let response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
await cache.put('/pdf/'+filename, response);
}

async function loadFileFromUrl(url, pageUrl, local = null) {
history.replaceState({}, '', pageUrl);
let response = await fetch(url);
if(response.status != 200) {
return;
}
let pdfBlob = await response.blob();
let file_id = url.replace(/^.*\//, '');

if(response.headers.has('content-disposition') && response.headers.get('Content-Disposition').match(/attachment; filename="/)) {
file_id = response.headers.get('Content-Disposition').replace(/^[^"]*"/, "").replace(/"[^"]*$/, "").replace(/_signe-[0-9]+\x.pdf/, '.pdf');
}

if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
}
let dataTransfer = new DataTransfer();
if (local) {
file_id = local;
}
dataTransfer.items.add(new File([pdfBlob], file_id, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
}

function storeSymmetricKeyCookie(hash, symmetricKey) {
if (symmetricKey.length != 15) {
console.error("Erreur taille cle symétrique.");
return;
}
document.cookie = hash + "=" + symmetricKey + "; SameSite=Lax; Path=/;";
}

function getSymmetricKey(hash) {
return getCookieValue(hash);
}

function getCookieValue (name) {
return document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '';
}

function generateSymmetricKey() {
const length = 15;
const keySpace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let key = '';

for (let i = 0; i < length; ++i) {
const randomIndex = Math.floor(Math.random() * keySpace.length);
key += keySpace.charAt(randomIndex);
}

return key;
}

function generatePdfHash() {
const length = 20;
const keySpace = '0123456789abcdefghijklmnopqrstuvwxyz';
let key = '';

for (let i = 0; i < length; ++i) {
const randomIndex = Math.floor(Math.random() * keySpace.length);
key += keySpace.charAt(randomIndex);
}

return key;
}

function dataURLtoBlob(dataurl) {
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}

function svgToDataUrl(svg) {

return "data:image/svg+xml;base64," + btoa(svg);
}

function trimSvgWhitespace(svgContent) {
if(!svgContent) {

return null;
}
let svgContainer = document.createElement("div")
svgContainer.classList.add('invisible');
svgContainer.classList.add('position-absolute');
svgContainer.classList.add('top-0');
svgContainer.classList.add('start-0');
svgContainer.style = "z-index: -1;";
svgContainer.innerHTML = svgContent;
document.body.appendChild(svgContainer);
let svg = svgContainer.querySelector('svg');
let box = svg.getBBox();
svg.setAttribute("viewBox", [box.x, box.y, box.width, box.height].join(" "));
svgContent = svgContainer.innerHTML;
document.body.removeChild(svgContainer)

return svgContent = svgContainer.innerHTML;
}
104 changes: 29 additions & 75 deletions public/js/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,102 +208,56 @@ function createEventsListener() {
})
}

async function loadFileFromCache(cacheUrl) {
if(!await canUseCache()) {
document.location = '/metadata';
return false;
}
const cache = await caches.open('pdf');
let responsePdf = await cache.match(cacheUrl);

if(!responsePdf) {
document.location = '/metadata';
return false;
}

let filename = cacheUrl.replace('/pdf/', '');

let pdfBlob = await responsePdf.blob();

let dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));

return true;
}

async function storeFileInCache() {
if(!await canUseCache()) {
return;
}
let cache = await caches.open('pdf');
let filename = document.getElementById('input_pdf_upload').files[0].name;
let response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
await cache.put('/pdf/'+filename, response);
history.pushState({}, '', '/metadata#'+filename);
}

async function loadFileFromUrl(url, local = null) {
history.replaceState({}, '', '/metadata');
var response = await fetch(url);
if(response.status != 200) {
return;
}
var pdfBlob = await response.blob();

if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
}
let dataTransfer = new DataTransfer();
let file_id = url.replace(/^.*\//, '');
if (local) {
file_id = local;
}
dataTransfer.items.add(new File([pdfBlob], file_id, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
}

var pageUpload = async function() {
async function pageUpload() {
document.querySelector('body').classList.remove('bg-light');
document.getElementById('input_pdf_upload').value = '';
document.getElementById('page-upload').classList.remove('d-none');
document.getElementById('page-metadata').classList.add('d-none');
document.getElementById('input_pdf_upload').focus();
window.addEventListener('hashchange', function() {
window.location.reload();
})
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
storeFileInCache();
pageMetadata();
if(await canUseCache()) {
storeFileInCache();
history.pushState({}, '', '/metadata#'+document.getElementById('input_pdf_upload').files[0].name);
}
pageMetadata(null);
});
}

var pageMetadata = async function() {
filename = document.getElementById('input_pdf_upload').files[0].name;
document.title = filename + ' - ' + document.title;
async function pageMetadata(url) {
document.querySelector('body').classList.add('bg-light');
document.getElementById('page-upload').classList.add('d-none');
document.getElementById('page-metadata').classList.remove('d-none');
menu = document.getElementById('sidebarTools');
menuOffcanvas = new bootstrap.Offcanvas(menu);
if(url && url.match(/^cache:\/\//)) {
await loadFileFromCache(url.replace(/^cache:\/\//, ''));
} else if (url) {
await loadFileFromUrl(url);
}

if(!document.getElementById('input_pdf_upload').files.length) {
alert("Chargement du PDF impossible");
document.location = '/metadata';
return;
}

responsiveDisplay();
createEventsListener();
loadPDF(document.getElementById('input_pdf_upload').files[0], filename);
loadPDF(document.getElementById('input_pdf_upload').files[0]);
};

(function () {
pageUpload();
if(window.location.hash && window.location.hash.match(/^\#http/)) {
loadFileFromUrl(window.location.hash.replace(/^\#/, ''));
pageMetadata(window.location.hash.replace(/^\#/, ''));
} else if(window.location.hash && window.location.hash.match(/^\#local/)) {
let hashUrl = window.location.origin + "/api/file/get?path=" + window.location.hash.replace(/^\#local:/, '');
loadFileFromUrl(hashUrl, window.location.hash.replace(/^\#/, ''));
pageMetadata(window.location.origin + "/api/file/get?path=" + window.location.hash.replace(/^\#local:/, ''), '/metadata', window.location.hash.replace(/^\#/, ''));
} else if(window.location.hash) {
loadFileFromCache('/pdf/'+window.location.hash.replace(/^\#/, ''));
pageMetadata('cache:///pdf/'+window.location.hash.replace(/^\#/, ''));
} else {
pageUpload();
}

window.addEventListener('hashchange', function() {
window.location.reload();
})
Expand Down
Loading

0 comments on commit ac8dba4

Please sign in to comment.