Commit 81c0a67f authored by Alain Takoudjou's avatar Alain Takoudjou

erp5_web_mynij_search: Validate source with a json schema before save it in indexeddb

parent 105d367d
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<title>Mynij Search</title> <title>Mynij Search</title>
<script src="rsvp.js" type="text/javascript"></script> <script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script> <script src="renderjs.js" type="text/javascript"></script>
<script src="tv4.js" type="text/javascript"></script>
<script src="gadget_erp5_page_load_source.js" type="text/javascript"></script> <script src="gadget_erp5_page_load_source.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="mynij-style.css"> <link rel="stylesheet" type="text/css" href="mynij-style.css">
</head> </head>
......
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>994.42668.25925.43639</string> </value> <value> <string>994.43649.32057.29422</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1631176409.26</float> <float>1632415203.4</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/ /*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, document*/ /*global window, RSVP, rJS, document, tv4*/
(function (window, document, rJS, RSVP) { (function (window, document, rJS, RSVP) {
var gadget; var gadget;
rJS(window) rJS(window)
.setState({ .setState({
torrent_gadget : null torrent_gadget : null
...@@ -49,11 +50,9 @@ ...@@ -49,11 +50,9 @@
return undefined; return undefined;
}) })
.push(function (response) { .push(function (response) {
var source_json;
if (!response) if (!response)
return undefined; return undefined;
source_json = JSON.parse(response.target.responseText); return response.target.responseText;
return source_json;
}); });
}) })
...@@ -61,18 +60,36 @@ ...@@ -61,18 +60,36 @@
var gadget = this, var gadget = this,
doc, doc,
promise_list = [], promise_list = [],
schema,
i; i;
return gadget.loadSource(options.url) return gadget.state.model_gadget.getSourceSchema()
.push(function (source_list) { .push(function (s) {
if (!source_list) { schema = s;
return gadget.loadSource(options.url);
})
.push(function (source_string) {
var source_list,
valid = false,
a_json = true,
message = "Failed to download source file from specified url";
try {
source_list = JSON.parse(source_string);
valid = tv4.validate(source_list, schema);
} catch (e) {
a_json = false;
}
if (!source_string || !a_json || !valid) {
if (!a_json || !valid) {
message = "Downloaded file is not a valid JSON source!";
}
return gadget.notifySubmitted({ return gadget.notifySubmitted({
message: "Failed to download source file from specified url...", message: message,
status: "error" status: "error"
}) })
.push(function () { .push(function () {
return gadget.redirect({command: 'display', options: { return gadget.redirect({command: 'display', options: {
page : "ojs_local_controller" page : "mynij_search"
}}); }});
}); });
} else { } else {
...@@ -121,4 +138,4 @@ ...@@ -121,4 +138,4 @@
}); });
}); });
}(window, document, rJS, RSVP)); }(window, document, rJS, RSVP, tv4));
\ No newline at end of file \ No newline at end of file
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>994.61267.64826.39321</string> </value> <value> <string>994.64336.47707.63931</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1632401883.67</float> <float>1632417697.12</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -92,7 +92,6 @@ ...@@ -92,7 +92,6 @@
for (elt in result_list[1]) { for (elt in result_list[1]) {
elt_dict = result_list[1][elt]; elt_dict = result_list[1][elt];
if (!elt_dict.status || elt_dict.status === "not built") { if (!elt_dict.status || elt_dict.status === "not built") {
has_item = true;
option = document.createElement("option"); option = document.createElement("option");
option.value = elt_dict.index_name; option.value = elt_dict.index_name;
option.appendChild( option.appendChild(
......
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>994.63879.43990.45038</string> </value> <value> <string>994.64069.60060.33621</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1632401634.98</float> <float>1632405414.19</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -236,6 +236,41 @@ ...@@ -236,6 +236,41 @@
}); });
}) })
.declareMethod("getSourceSchema", function () {
return {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"links": {
"type": "string"
},
"infohash": {
"type": "string"
},
"namehash": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"title",
"description",
"links"
]
}
]
};
})
.declareMethod("getConfig", function () { .declareMethod("getConfig", function () {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
......
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>994.49367.10232.59852</string> </value> <value> <string>994.64287.12898.50995</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1631519569.02</float> <float>1632415269.6</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -38,6 +38,7 @@ url_list = [ ...@@ -38,6 +38,7 @@ url_list = [
"rss-parser.min.js.map", "rss-parser.min.js.map",
"webtorrent.min.js", "webtorrent.min.js",
"jszip.min.js", "jszip.min.js",
"tv4.js",
"mynij-config.json", "mynij-config.json",
"gadget_erp5_mynij.json" "gadget_erp5_mynij.json"
] ]
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
<script src="renderjs.js" type="text/javascript"></script> <script src="renderjs.js" type="text/javascript"></script>
<script src="flexsearch.js" type="text/javascript"></script> <script src="flexsearch.js" type="text/javascript"></script>
<script src="jszip.min.js" type="text/javascript"></script> <script src="jszip.min.js" type="text/javascript"></script>
<script src="tv4.js" type="text/javascript"></script>
<script src="action_mynij_search_import_export_index.js" type="text/javascript"></script> <script src="action_mynij_search_import_export_index.js" type="text/javascript"></script>
</head> </head>
<body> <body>
......
(function (window, rJS, RSVP, jIO, File, Blob, document, JSZip) { (function (window, rJS, RSVP, jIO, File, Blob, document, JSZip, tv4) {
"use strict"; "use strict";
rJS(window) rJS(window)
.declareAcquiredMethod("notifySubmitting", "notifySubmitting") .declareAcquiredMethod("notifySubmitting", "notifySubmitting")
...@@ -212,19 +212,30 @@ ...@@ -212,19 +212,30 @@
}) })
.declareMethod("importIndexCollection", function (dataurl) { .declareMethod("importIndexCollection", function (dataurl) {
var gadget = this; var gadget = this,
schema;
return new RSVP.Queue() return gadget.state.model_gadget.getSourceSchema()
.push(function () { .push(function (s) {
schema = s;
return jIO.util.dataURItoBlob(dataurl); return jIO.util.dataURItoBlob(dataurl);
}) })
.push(function (file_blob) { .push(function (file_blob) {
return jIO.util.readBlobAsText(file_blob, "application/json"); return jIO.util.readBlobAsText(file_blob, "application/json");
}) })
.push(function (evt) { .push(function (evt) {
var index_list = JSON.parse(evt.target.result), var index_list,
promise_list = [], promise_list = [],
valid = true,
i; i;
try {
index_list = JSON.parse(evt.target.result);
} catch (e) {
valid = false;
}
if (!valid || !tv4.validate(index_list, schema)) {
return false;
}
for (i = 0; i < index_list.length; i += 1) { for (i = 0; i < index_list.length; i += 1) {
promise_list.push(gadget.state.model_gadget.createIndexDoc(index_list[i])); promise_list.push(gadget.state.model_gadget.createIndexDoc(index_list[i]));
} }
...@@ -307,7 +318,16 @@ ...@@ -307,7 +318,16 @@
redirect: redirect redirect: redirect
}; };
return gadget.importIndexCollection(content_dict.index_file.url) return gadget.importIndexCollection(content_dict.index_file.url)
.push(function () { .push(function (result) {
if (!result) {
return {
notify: {
message: "The JSON file is not a valid Mynij Source!",
status: "error"
},
redirect: redirect
};
}
return { return {
notify: { notify: {
message: "Index definition imported.", message: "Index definition imported.",
...@@ -389,4 +409,4 @@ ...@@ -389,4 +409,4 @@
}); });
}); });
}(window, rJS, RSVP, jIO, File, Blob, document, JSZip)); }(window, rJS, RSVP, jIO, File, Blob, document, JSZip, tv4));
\ No newline at end of file \ No newline at end of file
...@@ -11,5 +11,6 @@ web_page_module/mynij-** ...@@ -11,5 +11,6 @@ web_page_module/mynij-**
web_page_module/mynij_** web_page_module/mynij_**
web_page_module/rss-parser.min.js.map web_page_module/rss-parser.min.js.map
web_page_module/webtorrent_min_js web_page_module/webtorrent_min_js
web_page_module/tv4_json_validator_js
web_site_module/mynij_search web_site_module/mynij_search
web_site_module/mynij_search/** web_site_module/mynij_search/**
\ No newline at end of file
...@@ -11,5 +11,6 @@ web_page_module/mynij-** ...@@ -11,5 +11,6 @@ web_page_module/mynij-**
web_page_module/mynij_** web_page_module/mynij_**
web_page_module/rss-parser.min.js.map web_page_module/rss-parser.min.js.map
web_page_module/webtorrent_min_js web_page_module/webtorrent_min_js
web_page_module/tv4_json_validator_js
web_site_module/mynij_search web_site_module/mynij_search
web_site_module/mynij_search/** web_site_module/mynij_search/**
\ No newline at end of file
...@@ -10,6 +10,7 @@ web_page_module/jszip_min_js ...@@ -10,6 +10,7 @@ web_page_module/jszip_min_js
web_page_module/mynij-** web_page_module/mynij-**
web_page_module/mynij_** web_page_module/mynij_**
web_page_module/rss-parser.min.js.map web_page_module/rss-parser.min.js.map
web_page_module/tv4_json_validator_js
web_page_module/webtorrent_min_js web_page_module/webtorrent_min_js
web_site_module/mynij_search web_site_module/mynij_search
web_site_module/mynij_search/** web_site_module/mynij_search/**
\ No newline at end of file
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