|
| 1 | +<template lang="pug"> |
| 2 | + .download-app |
| 3 | + .btn-group(style='position: absolute; top: 15px; right: 15px;') |
| 4 | + a.btn.btn-sm.btn-info(@click='newSession()', title='New Upload') |
| 5 | + i.fa.fa-fw.fa-cloud-upload |
| 6 | + | new upload |
| 7 | + .alert.alert-danger(v-show="error") |
| 8 | + strong |
| 9 | + i.fa.fa-exclamation-triangle |
| 10 | + | {{ error }} |
| 11 | + .well(v-if='needsPassword') |
| 12 | + h3(style='margin-top: 0') Password |
| 13 | + .form-group |
| 14 | + input.form-control(type='password', v-model='password') |
| 15 | + p.text-danger(v-show='passwordWrong') |
| 16 | + strong Access denied! |
| 17 | + | |
| 18 | + button.btn.btn-primary(:disabled='password.length<1', @click='decrypt()') |
| 19 | + i.fa.fa-key |
| 20 | + | decrypt |
| 21 | + .panel.panel-primary(v-if='!needsPassword') |
| 22 | + .panel-heading Files |
| 23 | + .panel-body |
| 24 | + table.table.table-hover.table-striped(style='margin-bottom: 0') |
| 25 | + tbody |
| 26 | + tr(v-for='file in files', style='cursor: pointer', @click='download(file)') |
| 27 | + td(style='width: 60px') |
| 28 | + file-icon(:file='file') |
| 29 | + td |
| 30 | + p |
| 31 | + clipboard.pull-right(:value='host + file.url', @change='copied(file, $event)', title='Copy to clipboard', style='margin-left: 5px') |
| 32 | + a |
| 33 | + i.fa.fa-fw.fa-copy |
| 34 | + i.fa.fa-check.text-success.pull-right(v-show='file.downloaded') |
| 35 | + | |
| 36 | + strong {{ file.metadata.name }} |
| 37 | + | |
| 38 | + small ({{ humanFileSize(file.size) }}) |
| 39 | + p {{ file.metadata.comment }} |
| 40 | +</template> |
| 41 | + |
| 42 | + |
| 43 | +<script> |
| 44 | + "use strict"; |
| 45 | + import AES from 'crypto-js/aes'; |
| 46 | + import encUtf8 from 'crypto-js/enc-utf8'; |
| 47 | +
|
| 48 | + import FileIcon from './common/FileIcon.vue'; |
| 49 | + import Clipboard from './common/Clipboard.vue'; |
| 50 | +
|
| 51 | + export default { |
| 52 | + name: 'app', |
| 53 | + components: { FileIcon, Clipboard }, |
| 54 | + data () { |
| 55 | + return { |
| 56 | + files: [], |
| 57 | + sid: document.location.pathname.substr(1), |
| 58 | + passwordWrong: false, |
| 59 | + needsPassword: false, |
| 60 | + password: '', |
| 61 | + content: '', |
| 62 | + error: '', |
| 63 | + host: document.location.protocol + '//' + document.location.host |
| 64 | + } |
| 65 | + }, |
| 66 | + methods: { |
| 67 | + download(file) { |
| 68 | + if(file.downloaded && file.metadata.retention === 'one-time') { |
| 69 | + alert('One-Time Download: File is not available anymore.'); |
| 70 | + return; |
| 71 | + } |
| 72 | + document.location.href = file.url; |
| 73 | + file.downloaded = true; |
| 74 | + }, |
| 75 | +
|
| 76 | + copied(file, $event) { |
| 77 | + file.downloaded = $event === 'copied'; |
| 78 | + }, |
| 79 | +
|
| 80 | + decrypt() { |
| 81 | + this.passwordWrong = false; |
| 82 | + this.files = this.files.map(item => { |
| 83 | + if(typeof item === 'object') return item; |
| 84 | + const d = AES.decrypt(item, this.password); |
| 85 | + try { |
| 86 | + return Object.assign( |
| 87 | + JSON.parse(d.toString(encUtf8)), |
| 88 | + {downloaded: false} |
| 89 | + ); |
| 90 | + } catch(e) { |
| 91 | + this.passwordWrong = true; |
| 92 | + return item; |
| 93 | + } |
| 94 | + }); |
| 95 | + if(!this.passwordWrong) { |
| 96 | + this.needsPassword = false; |
| 97 | + this.password = ''; |
| 98 | + } |
| 99 | + }, |
| 100 | +
|
| 101 | + humanFileSize(fileSizeInBytes) { |
| 102 | + let i = -1; |
| 103 | + const byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB']; |
| 104 | + do { |
| 105 | + fileSizeInBytes = fileSizeInBytes / 1024; |
| 106 | + i++; |
| 107 | + } |
| 108 | + while(fileSizeInBytes > 1024); |
| 109 | + return Math.max(fileSizeInBytes, 0.01).toFixed(2) + byteUnits[i]; |
| 110 | + }, |
| 111 | +
|
| 112 | + newSession() { |
| 113 | + document.location.href = '/'; |
| 114 | + } |
| 115 | + }, |
| 116 | +
|
| 117 | + beforeMount() { |
| 118 | + const xhr = new XMLHttpRequest(); |
| 119 | + xhr.open('GET', '/' + this.sid + '.json'); |
| 120 | + xhr.onload = () => { |
| 121 | + if(xhr.status === 200) { |
| 122 | + try { |
| 123 | + this.files = JSON.parse(xhr.responseText).map(f => { |
| 124 | + if(typeof f !== 'object') { |
| 125 | + this.needsPassword = true; |
| 126 | + return f; |
| 127 | + } |
| 128 | + return Object.assign(f, {downloaded: false}); |
| 129 | + }); |
| 130 | + } catch(e) { |
| 131 | + this.error = e.toString(); |
| 132 | + } |
| 133 | + } else { |
| 134 | + this.error = `${xhr.status} ${xhr.statusText}: ${xhr.responseText}`; |
| 135 | + } |
| 136 | + }; |
| 137 | + xhr.send(); |
| 138 | + } |
| 139 | + } |
| 140 | +</script> |
0 commit comments