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