local-server/html/ui/js/Downloads.js

65 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-02-07 20:29:19 +00:00
export default {
props: [ 'kind', 'name', 'token', 'state' ],
data() {
return { createDisabled: false, selectedAssets: {} }
},
computed: {
availableAssets() {
return {
cluster: ['addons'],
host: [
"kernel",
2023-08-20 09:08:42 +00:00
"initrd",
2023-02-07 20:29:19 +00:00
"bootstrap.tar",
2023-08-20 09:08:42 +00:00
"boot.img.lz4",
"boot.iso",
2023-02-07 20:29:19 +00:00
"config",
2023-02-13 16:24:28 +00:00
"bootstrap-config",
2023-02-07 20:29:19 +00:00
"boot.tar",
"boot-efi.tar",
"boot.img.gz",
2023-08-20 09:08:42 +00:00
"boot.img",
2023-02-07 20:29:19 +00:00
"ipxe",
],
}[this.kind]
},
downloads() {
let ret = []
Object.entries(this.state.Downloads)
.filter(e => { let d=e[1]; return d.Kind == this.kind && d.Name == this.name })
.forEach(e => {
let token= e[0], d = e[1]
d.Assets.forEach(asset => {
ret.push({name: asset, url: '/public/downloads/'+token+'/'+asset})
})
})
return ret
},
assets() {
return this.availableAssets.filter(a => this.selectedAssets[a])
},
},
methods: {
createToken() {
event.preventDefault()
this.createDisabled = true
fetch('/authorize-download', {
method: 'POST',
body: JSON.stringify({Kind: this.kind, Name: this.name, Assets: this.assets}),
2023-02-13 16:24:28 +00:00
headers: { 'Authorization': 'Bearer ' + this.token, 'Content-Type': 'application/json' },
2023-02-07 20:29:19 +00:00
}).then((resp) => resp.json())
.then((token) => { this.selectedAssets = {}; this.createDisabled = false })
.catch((e) => { alert('failed to create link'); this.createDisabled = false })
},
},
template: `<div class="downloads">
<div class="options">
<span v-for="asset in availableAssets"><label><input type="checkbox" v-model="selectedAssets[asset]" />&nbsp;{{ asset }}</label></span>
</div>
<button :disabled="createDisabled || assets.length==0" @click="createToken">+</button>
<div><a v-for="d in downloads" target="_blank" :href="d.url">{{ d.name }}</a></div>
</div>`
}