|
1 | 1 | const url = require("url"); |
2 | 2 | const path = require("path"); |
| 3 | +const https = require("https"); |
3 | 4 | const CircularJSON = require("circular-json"); |
4 | 5 | const child_process = require('child_process'); |
5 | 6 | const UUID = require("uuidjs"); |
@@ -56,6 +57,19 @@ class Utils { |
56 | 57 |
|
57 | 58 | static metadataDownload(src, dest) { |
58 | 59 | return new Promise((resolve, reject) => { |
| 60 | + |
| 61 | + // check if URL is valid |
| 62 | + if(!this.isValidUrl(src)) { |
| 63 | + return reject("Inserire una URL valida"); |
| 64 | + } |
| 65 | + |
| 66 | + // check if URL exists |
| 67 | + https.get(src, (res) => { |
| 68 | + if(res.statusCode!='200') { |
| 69 | + return reject("Metadata non trovato alla URL indicata"); |
| 70 | + } |
| 71 | + }) |
| 72 | + |
59 | 73 | const file_name = url.parse(src).pathname.split('/').pop(); |
60 | 74 | const file_extention = path.extname(file_name); |
61 | 75 | const cmd = 'wget -O "' + dest + '" "' + src + '" --no-check-certificate --no-cache --no-cookies --user-agent="Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"'; |
@@ -211,6 +225,20 @@ class Utils { |
211 | 225 | }); |
212 | 226 | return fileArray; |
213 | 227 | } |
| 228 | + |
| 229 | + static isValidUrl(str) { |
| 230 | + const pattern = new RegExp( |
| 231 | + '^([a-zA-Z]+:\\/\\/)?' + // protocol |
| 232 | + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name |
| 233 | + '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR IP (v4) address |
| 234 | + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path |
| 235 | + '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string |
| 236 | + '(\\#[-a-z\\d_]*)?$', // fragment locator |
| 237 | + 'i' |
| 238 | + ); |
| 239 | + return pattern.test(str); |
| 240 | + } |
| 241 | + |
214 | 242 | } |
215 | 243 |
|
216 | 244 | module.exports = Utils; |
0 commit comments