2019-02-03 23:39:59 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import {mapState} from "vuex";
|
|
|
|
import { map } from 'lodash';
|
|
|
|
import getDirectoryContents from "@/api/server/getDirectoryContents";
|
|
|
|
import FileRow from "@/components/server/components/filemanager/FileRow";
|
|
|
|
import FolderRow from "@/components/server/components/filemanager/FolderRow";
|
2018-09-23 23:06:23 +00:00
|
|
|
|
2019-02-03 23:39:59 +00:00
|
|
|
type DataStructure = {
|
|
|
|
loading: boolean,
|
|
|
|
errorMessage: string | null,
|
|
|
|
currentDirectory: string,
|
|
|
|
files: Array<any>,
|
|
|
|
directories: Array<any>,
|
|
|
|
editableFiles: Array<string>,
|
|
|
|
}
|
2018-09-23 23:06:23 +00:00
|
|
|
|
2019-02-03 23:39:59 +00:00
|
|
|
export default Vue.component('file-manager', {
|
|
|
|
components: { FileRow, FolderRow },
|
2018-09-23 23:06:23 +00:00
|
|
|
computed: {
|
|
|
|
...mapState('server', ['server', 'credentials']),
|
|
|
|
...mapState('socket', ['connected']),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the breadcrumbs that display on the filemanager based on the directory that the
|
|
|
|
* user is currently in.
|
|
|
|
*/
|
2018-12-16 22:11:56 +00:00
|
|
|
breadcrumbs: function () {
|
|
|
|
const directories = this.currentDirectory.replace(/^\/|\/$/, '').split('/');
|
2018-09-23 23:06:23 +00:00
|
|
|
if (directories.length < 1 || !directories[0]) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-08-14 05:58:58 +00:00
|
|
|
|
2019-02-03 23:39:59 +00:00
|
|
|
return map(directories, function (value: string, key: number) {
|
2018-09-23 23:06:23 +00:00
|
|
|
if (key === directories.length - 1) {
|
|
|
|
return { directoryName: value };
|
2018-08-14 05:58:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 23:06:23 +00:00
|
|
|
return {
|
|
|
|
directoryName: value,
|
|
|
|
path: directories.slice(0, key + 1).join('/'),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
/**
|
|
|
|
* When the route changes reload the directory.
|
|
|
|
*/
|
2018-12-16 22:11:56 +00:00
|
|
|
'$route': function (to) {
|
2018-09-23 23:06:23 +00:00
|
|
|
this.currentDirectory = to.params.path || '/';
|
2018-08-04 05:32:01 +00:00
|
|
|
},
|
|
|
|
|
2018-09-23 23:06:23 +00:00
|
|
|
/**
|
|
|
|
* Watch the current directory setting and when it changes update the file listing.
|
|
|
|
*/
|
2018-12-16 22:11:56 +00:00
|
|
|
currentDirectory: function () {
|
2018-09-23 23:06:23 +00:00
|
|
|
this.listDirectory();
|
|
|
|
},
|
2018-08-14 05:58:58 +00:00
|
|
|
|
2018-09-23 23:06:23 +00:00
|
|
|
/**
|
|
|
|
* When we reconnect to the Daemon make sure we grab a listing of all of the files
|
|
|
|
* so that the error message disappears and we then load in a fresh listing.
|
|
|
|
*/
|
2018-12-16 22:11:56 +00:00
|
|
|
connected: function () {
|
2019-02-03 23:39:59 +00:00
|
|
|
// @ts-ignore
|
2018-09-23 23:06:23 +00:00
|
|
|
if (this.connected) {
|
2018-08-07 06:14:13 +00:00
|
|
|
this.listDirectory();
|
2018-08-14 04:06:11 +00:00
|
|
|
}
|
2018-08-07 06:14:13 +00:00
|
|
|
},
|
2018-09-23 23:06:23 +00:00
|
|
|
},
|
|
|
|
|
2019-02-03 23:39:59 +00:00
|
|
|
data: function (): DataStructure {
|
2018-09-23 23:06:23 +00:00
|
|
|
return {
|
|
|
|
currentDirectory: this.$route.params.path || '/',
|
|
|
|
loading: true,
|
|
|
|
errorMessage: null,
|
|
|
|
directories: [],
|
|
|
|
editableFiles: [],
|
|
|
|
files: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted: function () {
|
|
|
|
this.listDirectory();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
/**
|
|
|
|
* List the contents of a directory.
|
|
|
|
*/
|
2018-12-16 22:11:56 +00:00
|
|
|
listDirectory: function () {
|
2018-09-23 23:06:23 +00:00
|
|
|
this.loading = true;
|
|
|
|
|
2018-12-16 22:11:56 +00:00
|
|
|
const directory = encodeURI(this.currentDirectory.replace(/^\/|\/$/, ''));
|
2018-09-23 23:06:23 +00:00
|
|
|
getDirectoryContents(this.$route.params.id, directory)
|
2018-12-16 22:11:56 +00:00
|
|
|
.then((response) => {
|
2018-09-23 23:06:23 +00:00
|
|
|
this.files = response.files;
|
|
|
|
this.directories = response.directories;
|
|
|
|
this.editableFiles = response.editable;
|
|
|
|
this.errorMessage = null;
|
|
|
|
})
|
2018-12-16 22:11:56 +00:00
|
|
|
.catch((err) => {
|
2019-02-03 23:39:59 +00:00
|
|
|
if (typeof err === 'string') {
|
2018-09-23 23:06:23 +00:00
|
|
|
this.errorMessage = err;
|
|
|
|
return;
|
|
|
|
}
|
2018-08-07 06:14:13 +00:00
|
|
|
|
2018-09-23 23:06:23 +00:00
|
|
|
console.error('An error was encountered while processing this request.', { err });
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.loading = false;
|
|
|
|
});
|
2018-08-04 05:32:01 +00:00
|
|
|
},
|
2018-09-23 23:06:23 +00:00
|
|
|
},
|
2019-02-03 23:39:59 +00:00
|
|
|
|
|
|
|
template: `
|
2019-02-04 01:45:22 +00:00
|
|
|
<div class="animate fadein">
|
2019-02-03 23:39:59 +00:00
|
|
|
<div class="filemanager-breadcrumbs">
|
|
|
|
/<span class="px-1">home</span><!--
|
|
|
|
-->/<router-link :to="{ name: 'server-files' }" class="px-1">container</router-link><!--
|
|
|
|
--><span v-for="crumb in breadcrumbs" class="inline-block">
|
|
|
|
<span v-if="crumb.path">
|
|
|
|
/<router-link :to="{ name: 'server-files', params: { path: crumb.path } }" class="px-1">{{crumb.directoryName}}</router-link>
|
|
|
|
</span>
|
|
|
|
<span v-else>
|
|
|
|
/<span class="px-1 font-semibold">{{crumb.directoryName}}</span>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
2019-02-04 01:45:22 +00:00
|
|
|
<div class="content-box">
|
|
|
|
<div v-if="loading">
|
|
|
|
<div class="spinner spinner-xl blue"></div>
|
2019-02-03 23:39:59 +00:00
|
|
|
</div>
|
2019-02-04 01:45:22 +00:00
|
|
|
<div v-else-if="!loading && errorMessage">
|
|
|
|
<div class="alert error" v-text="errorMessage"></div>
|
|
|
|
</div>
|
|
|
|
<div v-else-if="!directories.length && !files.length">
|
2019-02-03 23:39:59 +00:00
|
|
|
<p class="text-grey text-sm text-center p-6 pb-4">This directory is empty.</p>
|
|
|
|
</div>
|
2019-02-04 01:45:22 +00:00
|
|
|
<div class="filemanager" v-else>
|
|
|
|
<div class="header">
|
|
|
|
<div class="flex-none w-8"></div>
|
|
|
|
<div class="flex-1">Name</div>
|
|
|
|
<div class="flex-1 text-right">Size</div>
|
|
|
|
<div class="flex-1 text-right">Modified</div>
|
|
|
|
<div class="flex-none w-1/6">Actions</div>
|
|
|
|
</div>
|
2019-02-03 23:39:59 +00:00
|
|
|
<div v-for="directory in directories">
|
|
|
|
<folder-row :directory="directory"/>
|
|
|
|
</div>
|
|
|
|
<div v-for="file in files">
|
|
|
|
<file-row :file="file" :editable="editableFiles" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
});
|