Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/base/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ export const openDatabase = () => {
*/
export const saveToIndexedDB = async (
key: string,
file: Blob,
metadata: any
file: any,
metadata?: any | undefined
) => {
const db = await openDatabase();
return new Promise<void>((resolve, reject) => {
Expand All @@ -406,7 +406,13 @@ export const saveToIndexedDB = async (
*/
export const getFromIndexedDB = async (key: string) => {
const db = await openDatabase();
return new Promise<any>((resolve, reject) => {
return new Promise<
| {
file: any;
metadata?: any | undefined;
}
| undefined
>((resolve, reject) => {
const transaction = db.transaction('files', 'readonly');
const store = transaction.objectStore('files');
const request = store.get(key);
Expand All @@ -432,7 +438,7 @@ export const loadGeoTIFFWithCache = async (sourceInfo: {
const cachedData = await getFromIndexedDB(sourceInfo.url);
if (cachedData) {
return {
file: new Blob([cachedData.file]),
file: cachedData.file,
metadata: cachedData.metadata,
sourceUrl: sourceInfo.url
};
Expand Down Expand Up @@ -494,12 +500,18 @@ export const loadFile = async (fileInfo: {
}

case 'ShapefileSource': {
const cached = await getFromIndexedDB(filepath);
if (cached) {
return cached.file;
}

try {
const response = await fetch(
`/jupytergis_core/proxy?url=${filepath}`
);
const arrayBuffer = await response.arrayBuffer();
const geojson = await shp(arrayBuffer);
await saveToIndexedDB(filepath, geojson);
return geojson;
} catch (error) {
console.error('Error loading remote shapefile:', error);
Expand All @@ -508,6 +520,11 @@ export const loadFile = async (fileInfo: {
}

case 'GeoJSONSource': {
const cached = await getFromIndexedDB(filepath);
if (cached) {
return cached.file;
}

try {
const response = await fetch(
`/jupytergis_core/proxy?url=${filepath}`
Expand All @@ -516,6 +533,7 @@ export const loadFile = async (fileInfo: {
throw new Error(`Failed to fetch GeoJSON from URL: ${filepath}`);
}
const geojson = await response.json();
await saveToIndexedDB(filepath, geojson);
return geojson;
} catch (error) {
console.error('Error loading remote GeoJSON:', error);
Expand Down
Loading