Skip to content

Commit

Permalink
feat: load zip from url
Browse files Browse the repository at this point in the history
  • Loading branch information
simontaurus authored Oct 12, 2024
1 parent ce79cb0 commit dbab25e
Showing 1 changed file with 54 additions and 36 deletions.
90 changes: 54 additions & 36 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ <h1>3. JSON-LD</h1>
console.log("Error while parsing direct link: " + reason)
}
}

if (param === 'fileUrl') data.fileUrl = value
})
}

Expand All @@ -342,6 +344,8 @@ <h1>3. JSON-LD</h1>
var mergeOptions = function () {
data.options = Object.assign(defaultOptions, data.options)
refreshUI()

if (data.fileUrl) downloadZipFromUrl(data.fileUrl);
}

/* -------------------------------------------------------------- refreshUI */
Expand Down Expand Up @@ -687,6 +691,55 @@ <h1>3. JSON-LD</h1>
}
}

var loadZipFile = function(file) {
console.log(file);
JSZip.loadAsync(file) //zip.file(name, ab);
.then(_zip => {
zip = _zip;
zip.forEach(function (relativePath, zipEntry) { // 2) print entries
console.log(zipEntry.name);
if (zipEntry.name.split('/').pop().split('.')[0] === "ro-crate-metadata") {
zip_root_path = zipEntry.name.replace(zipEntry.name.split('/').pop(), "")
console.log("ROOT:", zip_root_path)

zipEntry.async("text").then(json_str => {
var metadata_json = JSON.parse(json_str);
metadata_json = jsonld.frame(metadata_json, {
"@context": [
"https://w3id.org/ro/crate/1.1/context",
{"id": "@id", "type": "@type"}
],
"@type": "CreativeWork"
}, {base: null, omitGraph: false, compactArrays: true, _compactToRelative: false})
// note: json-editor will transform non-array values to arrays if defined in the schema
//.then(metadata_json => {console.log(metadata_json); return jsonld.compact(metadata_json, metadata_json["@context"])})
.then(metadata_json => {
//console.log(metadata_json);
var jsondata = metadata_json;
for (var node of metadata_json["@graph"]) {
if (node["id"] === "ro-crate-metadata.json") jsondata = node
}
jsoneditor.options.schema["@context"] = metadata_json["@context"];
schemaTextarea.setValue(JSON.stringify(jsoneditor.options.schema, null, 2));
delete metadata_json["@context"];
jsoneditor.setValue(jsondata);
});
});
}
});
})
}

var downloadZipFromUrl = function (url) {
fetch(url) //Fetch the .zip file
.then(response => { //Check if the response headers indicate a zip file
const contentType = response.headers.get('content-type');
return response.blob();
}).then(blob => { //Extract the contents of the zip file
loadZipFile(blob);
}
}

/* ------------------------------------------------------- updateDirectLink */

var updateDirectLink = function () {
Expand Down Expand Up @@ -761,42 +814,7 @@ <h1>3. JSON-LD</h1>

uploadArchive.addEventListener('change', (e) => {
const file = e.target.files[0];
console.log(file);
JSZip.loadAsync(file) //zip.file(name, ab);
.then(_zip => {
zip = _zip;
zip.forEach(function (relativePath, zipEntry) { // 2) print entries
console.log(zipEntry.name);
if (zipEntry.name.split('/').pop().split('.')[0] === "ro-crate-metadata") {
zip_root_path = zipEntry.name.replace(zipEntry.name.split('/').pop(), "")
console.log("ROOT:", zip_root_path)

zipEntry.async("text").then(json_str => {
var metadata_json = JSON.parse(json_str);
metadata_json = jsonld.frame(metadata_json, {
"@context": [
"https://w3id.org/ro/crate/1.1/context",
{"id": "@id", "type": "@type"}
],
"@type": "CreativeWork"
}, {base: null, omitGraph: false, compactArrays: true, _compactToRelative: false})
// note: json-editor will transform non-array values to arrays if defined in the schema
//.then(metadata_json => {console.log(metadata_json); return jsonld.compact(metadata_json, metadata_json["@context"])})
.then(metadata_json => {
//console.log(metadata_json);
var jsondata = metadata_json;
for (var node of metadata_json["@graph"]) {
if (node["id"] === "ro-crate-metadata.json") jsondata = node
}
jsoneditor.options.schema["@context"] = metadata_json["@context"];
schemaTextarea.setValue(JSON.stringify(jsoneditor.options.schema, null, 2));
delete metadata_json["@context"];
jsoneditor.setValue(jsondata);
});
});
}
});
})
loadZipFile(file);
}, false);

downloadArchive.addEventListener('click', function (e) {
Expand Down

0 comments on commit dbab25e

Please sign in to comment.