Skip to content

Commit

Permalink
Use GZIP magic bytes to detect whether a file is compressed (#360)
Browse files Browse the repository at this point in the history
This should fix #359.
  • Loading branch information
Luthaf authored Aug 30, 2024
1 parent 2e2d352 commit 52a2cc1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
13 changes: 8 additions & 5 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class ChemiscopeApp {
}
}

const dataset = readJSON(this.dataset, buffer);
const dataset = readJSON(buffer);
await this.load(config as Configuration, dataset);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ export class ChemiscopeApp {
const file = loadDataset.files![0];
this.dataset = file.name;
readFile(file, (result) => {
const dataset = readJSON(file.name, result);
const dataset = readJSON(result);
this.load({}, dataset);
// clear the selected file name to make sure 'onchange' is
// called again if the user loads a file a the same path
Expand Down Expand Up @@ -242,7 +242,7 @@ export class ChemiscopeApp {
return;
}

this.visualizer.applySettings(readJSON(file.name, result));
this.visualizer.applySettings(readJSON(result));
// clear the selected file name to make sure 'onchange' is
// called again if the user loads a file a the same path
// multiple time
Expand Down Expand Up @@ -307,9 +307,12 @@ function stopLoading() {
}

/** Read JSON or gzipped JSON and return the parsed object */
function readJSON(path: string, buffer: ArrayBuffer): any {
function readJSON(buffer: ArrayBuffer): any {
const magic = new Uint8Array(buffer.slice(0, 2));

let text;
if (path.endsWith('.gz')) {
// '1f 8b' is the magic constant starting gzip files
if (magic[0] == 0x1f && magic[1] == 0x8b) {
text = inflate(new Uint8Array(buffer), { to: 'string' });
} else {
const decoder = new TextDecoder('utf-8');
Expand Down
40 changes: 23 additions & 17 deletions python/chemiscope/sphinx/static/chemiscope-sphinx.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable */

/**
* Enum of the modes
*/
const VISUALISER_MODE = {
DEFAULT: "default",
STRUCTURE: "structure",
MAP: "map"
DEFAULT: 'default',
STRUCTURE: 'structure',
MAP: 'map',
};

/**
Expand Down Expand Up @@ -35,15 +37,12 @@ async function loadChemiscopeSphinx(divId, filePath, visualizerMode = VISUALISER
// Load widget
const visualiser = getVisualizer(visualizerMode);
await visualiser.load(config, dataset);
}

// Display errors
catch (error) {
} catch (error) {
// Display errors
console.error(error);
displayWarning(divId, error);
}

// Hide loading
finally {
} finally {
// Hide loading
toggleLoadingVisible(divId, false);
}
}
Expand Down Expand Up @@ -74,11 +73,18 @@ async function fetchDataset(filePath) {
if (!response.ok) {
throw new Error(`Failed to fetch ${filePath}: ${response.statusText}`);
}

// Get as json
const buffer = await response.arrayBuffer();
const decompressedData = pako.inflate(new Uint8Array(buffer), { to: 'string' });
return parseJsonWithNaN(decompressedData);
const magic = new Uint8Array(buffer.slice(0, 2));

let text;
// '1f 8b' is the magic constant starting gzip files
if (magic[0] == 0x1f && magic[1] == 0x8b) {
text = pako.inflate(new Uint8Array(buffer), { to: 'string' });
} else {
const decoder = new TextDecoder('utf-8');
text = decoder.decode(buffer);
}
return parseJsonWithNaN(text);
}

/**
Expand Down Expand Up @@ -141,7 +147,7 @@ function generateChemiscopeHTML(config, visualizerMode) {
function toggleLoadingVisible(divId, visible = true) {
const loader = document.getElementById(`${divId}-loading`);
if (loader) {
loader.style.display = visible ? "block" : "none";
loader.style.display = visible ? 'block' : 'none';
}
}

Expand All @@ -151,7 +157,7 @@ function toggleLoadingVisible(divId, visible = true) {
function hideElement(elementId) {
const element = document.getElementById(elementId);
if (element) {
element.style.display = "none";
element.style.display = 'none';
} else {
console.error(`Element ${elementId} is not found`);
}
Expand Down

0 comments on commit 52a2cc1

Please sign in to comment.