Skip to content

Commit 46bf557

Browse files
authored
Merge pull request #15 from Edirom/correcting_image_dimension
Resolving issue #11
2 parents f36bba3 + eac5cf0 commit 46bf557

File tree

2 files changed

+143
-67
lines changed

2 files changed

+143
-67
lines changed

src/store/index.js

+139-64
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default createStore({
4646
loading: false,
4747
logedin: false,
4848
processing: false,
49-
pageDimenssion: [],
49+
pageDimension: [],
5050
mode: allowedModes.selection,
5151
existingMusicMode: false,
5252
selectedZoneId: null,
@@ -796,77 +796,152 @@ export default createStore({
796796
commit('SET_BRANCH', branch)
797797
},
798798
importIIIF({ commit, dispatch, state }, url) {
799-
commit('SET_LOADING', true)
799+
commit('SET_LOADING', true);
800+
801+
// Fetch the IIIF manifest
800802
fetch(url)
801-
.then(res => {
802-
return res.json()
803-
})
803+
.then(res => res.json())
804804
.then(json => {
805-
commit('SET_LOADING', false)
806-
commit('SET_PROCESSING', true)
807-
805+
commit('SET_LOADING', false);
806+
commit('SET_PROCESSING', true);
807+
808808
let canvases = json.sequences[0].canvases;
809-
for (let i = 0; i < canvases.length; i++) {
810-
// Do something with canvas
811-
state.infoJson.push(canvases[i].images[0].resource.service['@id'] + "/info.json")
812-
813-
}
814-
let fetchPromises = [];
815-
816-
for (let i = 0; i < state.infoJson.length; i++) {
817-
// Add each fetch promise to the array
818-
fetchPromises.push(
819-
fetch(state.infoJson[i])
820-
.then(res => res.json()) // Parse the JSON from the response
821-
.then(result => {
822-
// check if this is a proper IIIF Manifest, then convert to MEI
823-
const isManifest = checkIiifManifest(json)
824-
if (!isManifest) {
825-
// do some error handling
826-
return false
827-
}
828-
829-
// Access the width and height from the result
830-
const width = result.width;
831-
const height = result.height;
832-
833-
// Push the dimensions into the state
834-
state.pageDimenssion.push([width, height]);
835-
})
836-
.catch(error => {
837-
console.error("Error:", error);
838-
})
839-
);
840-
}
841-
842-
// Use Promise.all to wait for all fetch requests to complete
843-
Promise.all(fetchPromises)
809+
810+
// Process each canvas one by one, sequentially
811+
const processCanvasesSequentially = async () => {
812+
for (let i = 0; i < canvases.length; i++) {
813+
try {
814+
// Build the info.json URL for the current canvas
815+
const infoUrl = canvases[i].images[0].resource.service['@id'] + "/info.json";
816+
817+
// Push the URL to the state.infoJson array
818+
state.infoJson.push(infoUrl);
819+
820+
// Fetch the info.json
821+
const res = await fetch(infoUrl);
822+
const result = await res.json();
823+
824+
// Check if this is a proper IIIF Manifest, then convert to MEI
825+
const isManifest = checkIiifManifest(json);
826+
if (!isManifest) {
827+
throw new Error("Invalid IIIF manifest");
828+
}
829+
830+
// Extract the width and height from the result
831+
const width = result.width;
832+
const height = result.height;
833+
834+
// Push the dimensions into the state.pageDimension array
835+
state.pageDimension.push([width, height]);
836+
837+
} catch (error) {
838+
console.error(`Error processing canvas ${i}:`, error);
839+
throw error; // This will stop the loop and be caught in the outer catch block
840+
}
841+
}
842+
};
843+
844+
// Call the sequential processing function
845+
processCanvasesSequentially()
844846
.then(() => {
845-
846-
iiifManifest2mei(json, url, parser, state)
847-
848-
.then(mei => {
849-
dispatch('setData', mei)
850-
})
851-
})
852-
.catch(err => {
853-
commit('SET_LOADING', false)
854-
console.log(err)
855-
// add some error message
847+
// After processing all canvases, convert the manifest to MEI
848+
return iiifManifest2mei(json, url, parser, state);
849+
})
850+
.then(mei => {
851+
// Dispatch setData with the generated MEI
852+
dispatch('setData', mei);
853+
})
854+
.catch(err => {
855+
console.error('Error processing IIIF manifest or canvases:', err);
856+
commit('SET_LOADING', false);
857+
// Add any additional error messaging here
858+
})
859+
.finally(() => {
860+
commit('SET_PROCESSING', false); // Ensure processing is set to false after completion
861+
});
862+
})
863+
.catch(error => {
864+
// Handle errors in the initial IIIF manifest fetch
865+
console.error('Error fetching IIIF manifest:', error);
866+
commit('SET_LOADING', false);
867+
});
868+
},
869+
importIIIF({ commit, dispatch, state }, url) {
870+
commit('SET_LOADING', true);
871+
872+
// Fetch the IIIF manifest
873+
fetch(url)
874+
.then(res => res.json())
875+
.then(json => {
876+
commit('SET_LOADING', false);
877+
commit('SET_PROCESSING', true);
878+
879+
let canvases = json.sequences[0].canvases;
880+
881+
// Map all canvas images to their info.json URLs
882+
const infoJsonUrls = canvases.map(canvas => {
883+
return canvas.images[0].resource.service['@id'] + "/info.json";
884+
});
885+
886+
// Store all fetch promises for info.json
887+
const fetchPromises = infoJsonUrls.map((infoUrl) => {
888+
return fetch(infoUrl)
889+
.then(res => res.json())
890+
.then(result => {
891+
// Check if this is a proper IIIF Manifest
892+
const isManifest = checkIiifManifest(json);
893+
if (!isManifest) {
894+
throw new Error("Invalid IIIF manifest");
895+
}
896+
897+
// Extract the width and height from the result
898+
const width = result.width;
899+
const height = result.height;
900+
901+
// Return both the infoUrl and the dimensions
902+
return { infoUrl, dimensions: [width, height] };
856903
})
904+
.catch(error => {
905+
console.error(`Error fetching ${infoUrl}:`, error);
906+
// Return a fallback object in case of error, to keep the promise chain going
907+
return { infoUrl, dimensions: [null, null], error: true };
908+
});
909+
});
910+
911+
// Use Promise.allSettled to fetch all info.json files concurrently and wait for all of them
912+
Promise.allSettled(fetchPromises)
913+
.then((results) => {
914+
// Update state.infoJson and state.pageDimension in batches
915+
results.forEach(result => {
916+
if (result.status === 'fulfilled') {
917+
state.infoJson.push(result.value.infoUrl);
918+
state.pageDimension.push(result.value.dimensions);
919+
}
920+
});
921+
922+
// After processing all canvases, convert the manifest to MEI
923+
return iiifManifest2mei(json, url, parser, state);
857924
})
858-
.catch(error => {
859-
console.error("Error with one of the promises:", error);
925+
.then(mei => {
926+
// Dispatch setData with the generated MEI
927+
dispatch('setData', mei);
928+
})
929+
.catch(err => {
930+
console.error('Error processing IIIF manifest or canvases:', err);
931+
commit('SET_LOADING', false);
932+
// Add any additional error messaging here
933+
})
934+
.finally(() => {
935+
commit('SET_PROCESSING', false); // Ensure processing is set to false after completion
860936
});
861-
862-
863-
864-
865-
866-
867-
868-
937+
})
938+
.catch(error => {
939+
// Handle errors in the initial IIIF manifest fetch
940+
console.error('Error fetching IIIF manifest:', error);
941+
commit('SET_LOADING', false);
942+
});
869943
},
944+
870945
importXML({ commit, dispatch }, mei) {
871946
fetch(mei)
872947
.then(res => {

src/tools/iiif.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { uuid } from '@/tools/uuid.js'
22

3-
function addPage (canvas, dimension, n, file, meiSurfaceTemplate, hasItems) {
3+
function addPage (canvas,canvases, dimension, n, file, meiSurfaceTemplate, hasItems) {
44
// var imgsrc = canvas.images[0].resource.service['@id']+"/info.json"
55
//var width, height = getDimention(imgsrc)
66

77
const label = canvas.label
88
var width
99
var height
10-
if(n < 120){
10+
if(n <= canvases.length){
11+
console.log(" number is", n , "dimenssion ", dimension, "canvas width ", canvas.width , "canvas height ", canvas.height )
1112
height = dimension[1]
1213
width = dimension[0]
1314

@@ -108,7 +109,7 @@ export async function iiifManifest2mei (json, url, parser, state) {
108109
if(json.sequences){
109110
json.sequences[0].canvases.forEach((canvas, i) => {
110111
var hasItems = false
111-
addPage(canvas, state.pageDimenssion[i + 1], i + 1, file, meiSurfaceTemplate, hasItems)
112+
addPage(canvas, json.sequences[0].canvases, state.pageDimension[i], i + 1, file, meiSurfaceTemplate, hasItems)
112113

113114
})
114115
}else{

0 commit comments

Comments
 (0)