|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +import { api } from '@/api' |
| 19 | + |
| 20 | +async function isValidObject (apiName, id, params) { |
| 21 | + try { |
| 22 | + const allParams = { ...params, listAll: true, id } |
| 23 | + const json = await api(apiName, allParams) |
| 24 | + const responseName = Object.keys(json).find(key => key.endsWith('response')) || apiName.toLowerCase() + 'response' |
| 25 | + const response = json?.[responseName] |
| 26 | + if (!response) { |
| 27 | + return false |
| 28 | + } |
| 29 | + const objectName = Object.keys(response).find(key => key !== 'count') |
| 30 | + if (!objectName || !Array.isArray(response[objectName])) { |
| 31 | + return false |
| 32 | + } |
| 33 | + return response[objectName].some(item => item.id === id) |
| 34 | + } catch (e) { |
| 35 | + return false |
| 36 | + } |
| 37 | +} |
| 38 | + |
18 | 39 | export function validateLinks (router, isStatic, resource) { |
19 | 40 | const validLinks = { |
20 | 41 | volume: false, |
@@ -45,3 +66,45 @@ export function validateLinks (router, isStatic, resource) { |
45 | 66 |
|
46 | 67 | return validLinks |
47 | 68 | } |
| 69 | + |
| 70 | +export async function validateLinksAsync (router, isStatic, resource) { |
| 71 | + const validLinks = { |
| 72 | + template: false, |
| 73 | + iso: false |
| 74 | + } |
| 75 | + const pendingChecks = [] |
| 76 | + |
| 77 | + if (isStatic) { |
| 78 | + return validLinks |
| 79 | + } |
| 80 | + |
| 81 | + if (resource.templateid) { |
| 82 | + const templatePath = (resource.templateformat === 'ISO' ? '/iso/' : '/template/') + resource.templateid |
| 83 | + if (router.resolve(templatePath).matched[0].redirect !== '/exception/404') { |
| 84 | + pendingChecks.push( |
| 85 | + isValidObject('listTemplates', resource.templateid, { templatefilter: 'executable' }).then(result => { |
| 86 | + validLinks.template = result |
| 87 | + }) |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (resource.isoid) { |
| 93 | + const isoPath = '/iso/' + resource.isoid |
| 94 | + if (router.resolve(isoPath).matched[0].redirect !== '/exception/404') { |
| 95 | + pendingChecks.push( |
| 96 | + isValidObject('listIsos', resource.isoid, { isofilter: 'executable' }).then(result => { |
| 97 | + validLinks.iso = result |
| 98 | + }) |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (pendingChecks.length) { |
| 104 | + await Promise.all(pendingChecks).catch(error => { |
| 105 | + console.error('Error validating links:', error) |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + return validLinks |
| 110 | +} |
0 commit comments