|
1 | 1 | <template> |
2 | | - |
| 2 | + <div> |
| 3 | + <div v-if="loading"> |
| 4 | + <div class="spinner spinner-xl blue"></div> |
| 5 | + </div> |
| 6 | + <div class="filemanager" v-else> |
| 7 | + <div class="header"> |
| 8 | + <div class="flex-none w-8"></div> |
| 9 | + <div class="flex-1">Name</div> |
| 10 | + <div class="flex-1 text-right">Size</div> |
| 11 | + <div class="flex-1 text-right">Modified</div> |
| 12 | + <div class="flex-none w-1/6">Actions</div> |
| 13 | + </div> |
| 14 | + <div class="row" v-for="folder in folders"> |
| 15 | + <div class="flex-none icon"><folder-icon/></div> |
| 16 | + <div class="flex-1">{{folder.name}}</div> |
| 17 | + <div class="flex-1 text-right text-grey-dark"></div> |
| 18 | + <div class="flex-1 text-right text-grey-dark">{{formatDate(folder.modified)}}</div> |
| 19 | + <div class="flex-none w-1/6"></div> |
| 20 | + </div> |
| 21 | + <div class="row" v-for="file in files"> |
| 22 | + <div class="flex-none icon"> |
| 23 | + <file-text-icon v-if="!file.symlink"/> |
| 24 | + <link2-icon v-else/> |
| 25 | + </div> |
| 26 | + <div class="flex-1">{{file.name}}</div> |
| 27 | + <div class="flex-1 text-right text-grey-dark">{{readableSize(file.size)}}</div> |
| 28 | + <div class="flex-1 text-right text-grey-dark">{{formatDate(file.modified)}}</div> |
| 29 | + <div class="flex-none w-1/6"></div> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | + </div> |
3 | 33 | </template> |
4 | 34 |
|
5 | 35 | <script> |
| 36 | + import filter from 'lodash/filter'; |
| 37 | + import format from 'date-fns/format'; |
| 38 | + import { mapState } from 'vuex'; |
| 39 | + import { FileTextIcon, FolderIcon, Link2Icon } from 'vue-feather-icons'; |
| 40 | +
|
6 | 41 | export default { |
7 | | - name: 'file-manager-page' |
8 | | - }; |
9 | | -</script> |
| 42 | + name: 'file-manager-page', |
| 43 | + components: { FileTextIcon, FolderIcon, Link2Icon }, |
| 44 | +
|
| 45 | + computed: { |
| 46 | + ...mapState('server', ['server', 'credentials']), |
| 47 | + }, |
| 48 | +
|
| 49 | + data: function () { |
| 50 | + return { |
| 51 | + directory: '/', |
| 52 | + loading: true, |
| 53 | +
|
| 54 | + files: [], |
| 55 | + folders: [], |
| 56 | + }; |
| 57 | + }, |
| 58 | +
|
| 59 | + mounted: function () { |
| 60 | + this.listDirectory(); |
| 61 | + }, |
10 | 62 |
|
11 | | -<style scoped> |
| 63 | + methods: { |
| 64 | + /** |
| 65 | + * List the contents of a directory. |
| 66 | + */ |
| 67 | + listDirectory: function () { |
| 68 | + window.axios.get(`${this.credentials.node}/v1/server/directory/${this.directory}`, { |
| 69 | + headers: { |
| 70 | + 'X-Access-Server': this.server.uuid, |
| 71 | + 'X-Access-Token': this.credentials.key, |
| 72 | + } |
| 73 | + }) |
| 74 | + .then((response) => { |
| 75 | + this.files = filter(response.data, function (o) { |
| 76 | + return o.file; |
| 77 | + }); |
12 | 78 |
|
13 | | -</style> |
| 79 | + this.folders = filter(response.data, function (o) { |
| 80 | + return o.directory; |
| 81 | + }); |
| 82 | + }) |
| 83 | + .catch(console.error) |
| 84 | + .finally(() => { |
| 85 | + this.loading = false; |
| 86 | + }); |
| 87 | + }, |
| 88 | +
|
| 89 | + /** |
| 90 | + * Return the human readable filesize for a given number of bytes. This |
| 91 | + * uses 1024 as the base, so the response is denoted accordingly. |
| 92 | + * |
| 93 | + * @param {Number} bytes |
| 94 | + * @return {String} |
| 95 | + */ |
| 96 | + readableSize: function (bytes) { |
| 97 | + if (Math.abs(bytes) < 1024) { |
| 98 | + return `${bytes} Bytes`; |
| 99 | + } |
| 100 | +
|
| 101 | + let u = -1; |
| 102 | + const units = ['KiB', 'MiB', 'GiB', 'TiB']; |
| 103 | +
|
| 104 | + do { |
| 105 | + bytes /= 1024; |
| 106 | + u++; |
| 107 | + } while (Math.abs(bytes) >= 1024 && u < units.length - 1); |
| 108 | +
|
| 109 | + return `${bytes.toFixed(1)} ${units[u]}` |
| 110 | + }, |
| 111 | +
|
| 112 | + /** |
| 113 | + * Format the given date as a human readable string. |
| 114 | + * |
| 115 | + * @param {String} date |
| 116 | + * @return {String} |
| 117 | + */ |
| 118 | + formatDate: function (date) { |
| 119 | + return format(date, 'MMM D, YYYY [at] HH:MM'); |
| 120 | + }, |
| 121 | + } |
| 122 | + }; |
| 123 | +</script> |
0 commit comments