Skip to content

Commit

Permalink
shorten-url support for legacy json files (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
segun-codes authored Feb 28, 2023
1 parent e0c3cfe commit cce3976
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions examples/js/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const setupMap = () => {
map.addGoogleMutant();
map.whenReady(() => {
const url = location.href;
if (isLegacyUrlFormat(url) || isWorkingUrlFormat(url)) {

if (isWorkingUrlFormat(url)) {
new bootstrap.Modal(welcomeModal).hide();
mapReconstructionMode = true;
return;
Expand Down Expand Up @@ -265,20 +266,6 @@ function extractJsonFromUrlParams(url) {
return jsonDownloadURL;
}

function isJsonDetected(url) {
if (url.includes('?json=')) {
const startIndex = url.lastIndexOf('.');
const fileExtension = url.slice(startIndex + 1);

if (fileExtension === 'json') {
console.log('JSON found in map shareable link'); // for debugging purposes only
return true;
}
}

return false;
}

// places image on tile
function placeImage(imageURL, options, newImage = false) {
let image;
Expand Down Expand Up @@ -396,41 +383,55 @@ const reconstructMapFromJson = async (jsonDownloadURL = false, savedMapObj) => {
placeImage(imageURL, options, false);
};

// transforms url from format: https://localhost:8080/examples/archive.html?k=texas-barnraising-copy
// to: https://archive.org/details/texas-barnraising-copy
function convertToFetchUrl(url) {
// transforms url from format: https://localhost:8080/examples/archive.html?k=texas-barnraising-copy to: https://archive.org/details/texas-barnraising-copy
// OR from https://localhost:8080/examples/archive.html?kl=--10 to https://archive.org/download/mapknitter/--10.json
function convertToFetchUrl(url, legacyJson = false) {
let fetchUrl;
const queryParam = url.substring(url.lastIndexOf('=') + 1);
return `https://archive.org/details/${queryParam}`;
}

function isWorkingUrlFormat(url) {
return url.includes('?k=');
if (!legacyJson) {
fetchUrl = `https://archive.org/details/${queryParam}`;
} else {
fetchUrl = `https://archive.org/download/mapknitter/${queryParam}.json`;
}

return fetchUrl;
}

function isLegacyUrlFormat(url) {
return isJsonDetected(url);
function isWorkingUrlFormat(url) {
if (url.includes('?k=')) {
return {isWorkingFormat: true, legacyJson: false};
}

if (url.includes('?kl=')) {
return {isWorkingFormat: true, legacyJson: true};
}
}

document.addEventListener('DOMContentLoaded', async (event) => {
if (mapReconstructionMode) {
const url = location.href;

// working url format: https://localhost:8080/examples/archive.html?k=texas-barnraising-copy
if (isWorkingUrlFormat(url)) {
const fetchUrl = convertToFetchUrl(url).replace('details', 'metadata');
const response = await performFetch(fetchUrl);
// working url formats: https://localhost:8080/examples/archive.html?k=texas-barnraising-copy (for mapknitter-lite generated json files)
// OR https://localhost:8080/examples/archive.html?kl=--10 (legacy format - for json files from legacy mapknitter.org)
const assessedUrl = isWorkingUrlFormat(url);

if (response) {
const jsonDownloadURL = findSavedMapsJson(response);
if (assessedUrl.isWorkingFormat) {
let jsonDownloadURL;

if (!assessedUrl.legacyJson) { // checks if url points to mapknitter-lite generated json file(s)
const fetchUrl = convertToFetchUrl(url).replace('details', 'metadata');
const response = await performFetch(fetchUrl);

if (response) {
jsonDownloadURL = findSavedMapsJson(response);
reconstructMapFromJson(jsonDownloadURL);
}
} else {
jsonDownloadURL = convertToFetchUrl(url, assessedUrl.legacyJson); // jsonDownloadURL => https://archive.org/download/mapknitter/--10.json
reconstructMapFromJson(jsonDownloadURL);
}
}

// legacy url format: http://localhost:8081/examples/archive.html?json=https://archive.org/download/mapknitter/--10.json
if (isLegacyUrlFormat(url)) {
const jsonDownloadURL = extractJsonFromUrlParams(url); // returns url with format https://archive.org/download/mapknitter/--10.json
reconstructMapFromJson(jsonDownloadURL);
}
}
}
});

Expand Down Expand Up @@ -714,8 +715,8 @@ function handleDrop(e) {
});
reader.readAsDataURL(files[i]);
}
}
}
}
};

function uploadFiles() {
const dropZone = document.getElementById('dropZone');
Expand Down

0 comments on commit cce3976

Please sign in to comment.