Commit a071c141 authored by Stephan Kreutzer's avatar Stephan Kreutzer

resource_retriever_1: Support for Dat added via HTTP interop bridge to local NodeJS server.

parent 13e22191
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2017 Stephan Kreutzer
Copyright (C) 2017-2020 Stephan Kreutzer
This file is part of resource_retriever_1 workflow, a submodule of the
digital_publishing_workflow_tools package.
......@@ -21,6 +21,7 @@ along with resource_retriever_1 workflow. If not, see <http://www.gnu.org/licens
<resources>
<resource identifier="https://example.org/resource_1.txt"/>
<resource identifier="file:///home/user/resource_2.txt"/>
<resource identifier="dat://0a9e202b8055721bd2bc93b3c9bbc03efdbda9cfee91f01a123fdeaadeba303e/"/>
</resources>
<output-directory path="./output"/>
</resource-retriever-1-workflow-jobfile>
# Copyright (C) 2017 Stephan Kreutzer
# Copyright (C) 2017-2020 Stephan Kreutzer
#
# This file is part of resource_retriever_1 workflow, a submodule of the
# digital_publishing_workflow_tools package.
......@@ -59,6 +59,7 @@ messageHttpsClient1ResultInfoFileErrorWhileReading = An error occurred while rea
messageHttpClient1ResultInfoFileErrorWhileReading = An error occurred while reading the http_client_1 result information file "{0}".
messageHttpsClient1CallWasntSuccessful = The call of https_client_1 wasn''t successful for resource {0}.
messageHttpClient1CallWasntSuccessful = The call of http_client_1 wasn''t successful for resource {0}.
messageNodejsDatBridgeUrlEscapingFailure = URL escaping for the NodeJS DAT bridge failed.
messageLocalResourceFileCantGetCanonicalPath = Can''t get canonical path of local resource file "{0}".
messageLocalResourceFileDoesntExist = Local resource file "{0}" doesn''t exist.
messageLocalResourcePathIsntAFile = Local resource path "{0}" exists, but isn''t a file.
......
# Copyright (C) 2017-2018 Stephan Kreutzer
# Copyright (C) 2017-2020 Stephan Kreutzer
#
# This file is part of resource_retriever_1 workflow, a submodule of the
# digital_publishing_workflow_tools package.
......@@ -59,6 +59,7 @@ messageHttpsClient1ResultInfoFileErrorWhileReading = Es ist ein Fehler aufgetret
messageHttpClient1ResultInfoFileErrorWhileReading = Es ist ein Fehler aufgetreten beim Auslesen der http_client_1-Ergebnisinformationsdatei "{0}".
messageHttpsClient1CallWasntSuccessful = Der Aufruf von https_client_1 war nicht erfolgreich für Ressource {0}.
messageHttpClient1CallWasntSuccessful = Der Aufruf von http_client_1 war nicht erfolgreich für Ressource {0}.
messageNodejsDatBridgeUrlEscapingFailure = Das URL-Escaping für die NodeJS-DAT-Bridge ist fehlgeschlagen.
messageLocalResourceFileCantGetCanonicalPath = Kann keinen kanonischen Pfad für die lokale Ressourcen-Datei "{0}" ermitteln.
messageLocalResourceFileDoesntExist = Lokale Ressourcen-Datei "{0}" existiert nicht.
messageLocalResourcePathIsntAFile = Lokaler Ressourcen-Pfad "{0}" ist keine Datei.
......
/*
Copyright (C) 2017-2020 Stephan Kreutzer
This file is part of nodejs_local_dat_1, a submodule of the
digital_publishing_workflow_tools package.
nodejs_local_dat_1 is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 or any later version,
as published by the Free Software Foundation.
nodejs_local_dat_1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License 3 for more details.
You should have received a copy of the GNU Affero General Public License 3
along with nodejs_local_dat_1. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let urlParser = require('url');
function handleRequest(request, response)
{
let url = urlParser.parse(request.url, true);
if (url.pathname == "/hyperdrive.readdir")
{
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if (url.query.name == undefined ||
url.query.name == null)
{
response.statusCode = 400;
response.end();
return;
}
const SDK = require('dat-sdk');
const { Hyperdrive } = SDK();
const hyperdrive = Hyperdrive("dat://" + url.query.name);
let callback = function() {
hyperdrive.readdir('/', function(error, list) {
if (error != undefined &&
error != null)
{
response.write(error);
response.statusCode = 500;
response.end();
return;
}
// TODO: JSON-LD. Paths are relative.
response.write(JSON.stringify(list));
/*
// TODO: XML namespace.
response.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ul>");
for (let i = 0, max = list.length; i < max; i++)
{
response.write("<li><a href=\"dat://");
response.write(url.query.name);
// TODO: Slash at the end of the URL or not?
response.write(list[i]);
response.write("\">");
response.write(list[i]);
response.write("</a></li>");
}
response.write("</ul>");
*/
response.statusCode = 200;
response.end();
});
};
if (hyperdrive.metadata.peers.length > 0)
{
hyperdrive.metadata.update({ ifAvailable: true }, callback);
}
else
{
hyperdrive.metadata.once('peer-add', () => {
hyperdrive.metadata.update({ ifAvailable: true }, callback);
});
}
return;
}
else
{
response.statusCode = 404;
response.end();
return;
}
}
console.log("\n" +
"nodejs_local_dat_1 Copyright (C) 2017-2020 Stephan Kreutzer\n" +
"This program comes with ABSOLUTELY NO WARRANTY.\n" +
"This is free software, and you are welcome to redistribute it\n" +
"under certain conditions. See the GNU Affero General Public License 3\n" +
"or any later version for details. Also, see the source code repository\n" +
"https://gitlab.com/publishing-systems/digital_publishing_workflow_tools/ and\n" +
"the project website https://publishing-systems.org.\n\n");
let http = require('http');
let server = http.createServer(handleRequest);
server.listen(8080, function() { });
#!/bin/sh
# Copyright (C) 2017-2020 Stephan Kreutzer
#
# This file is part of nodejs_local_dat_1, a submodule of the
# digital_publishing_workflow_tools package.
#
# nodejs_local_dat_1 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3 or any later version,
# as published by the Free Software Foundation.
#
# nodejs_local_dat_1 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License 3 for more details.
#
# You should have received a copy of the GNU Affero General Public License 3
# along with nodejs_local_dat_1. If not, see <http://www.gnu.org/licenses/>.
sudo apt-get install nodejs
npm install --save dat-sdk
#!/bin/sh
# Copyright (C) 2017-2020 Stephan Kreutzer
#
# This file is part of nodejs_local_dat_1, a submodule of the
# digital_publishing_workflow_tools package.
#
# nodejs_local_dat_1 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3 or any later version,
# as published by the Free Software Foundation.
#
# nodejs_local_dat_1 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License 3 for more details.
#
# You should have received a copy of the GNU Affero General Public License 3
# along with nodejs_local_dat_1. If not, see <http://www.gnu.org/licenses/>.
# Error messages from the second call on are likely caused by the server
# already running, so no second server can be instantiated listening on
# the same port. In the future, the shell script could check if there's
# already a nodejs process.
nodejs nodejs_local_dat_1.js &
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment