-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadFile.vue
More file actions
138 lines (122 loc) · 3.61 KB
/
UploadFile.vue
File metadata and controls
138 lines (122 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { S3FileFieldResultState } from 'django-s3-file-field';
import { getS3ffClient } from '../../plugins/S3FileField';
export default defineComponent({
emits: ['upload', 'close'],
setup(_, { emit }) {
const s3ffClient = getS3ffClient();
const successDialog = ref(false);
const uploadLoading = ref(false);
const uploadProgress = ref<number>(0);
const uploadFile = ref<File | null>(null);
const uploadError = ref<unknown>();
const acceptTypes = ref(
'.geojson, .json, .tif, .tiff, .zip, .gpkg, .nc, .mpg, .mp4, .avi, .mov, .mkv, .webm, .jpg, .jpeg, .png, .gif',
);
const fileInput = ref<HTMLInputElement | null>(null);
function triggerFileInput() {
if (fileInput.value) fileInput.value.click();
}
async function upload() {
try {
uploadLoading.value = true;
uploadError.value = undefined;
uploadProgress.value = 0;
const file = uploadFile.value;
if (!file) return;
const uploadResult = await s3ffClient.uploadFile(
file,
'core.FileItem.file',
(progress) => {
if (progress.state === 1 && progress.uploaded !== undefined && progress.total !== undefined) {
uploadProgress.value = (progress.uploaded / progress.total) * 100;
}
},
);
if (uploadResult.state !== S3FileFieldResultState.Successful) {
const status = ['was aborted', '', 'errored'][uploadResult.state];
throw new Error(`File upload ${status}`);
}
successDialog.value = true;
emit('upload', { result: uploadResult.value, name: file.name });
} catch (err) {
uploadError.value = err;
} finally {
uploadLoading.value = false;
}
}
async function handleFileSelected(event: Event) {
const input = event.target as HTMLInputElement;
if (!input.files || input.files.length === 0) return;
// eslint-disable-next-line prefer-destructuring
uploadFile.value = input.files[0];
await upload();
}
function closeDialogs() {
successDialog.value = false;
emit('close');
}
return {
successDialog,
uploadLoading,
uploadProgress,
uploadError,
acceptTypes,
fileInput,
triggerFileInput,
handleFileSelected,
closeDialogs,
};
},
});
</script>
<template>
<div>
<v-btn
:disabled="uploadLoading"
color="primary"
variant="outlined"
@click="triggerFileInput"
>
<template v-if="!uploadLoading">
Upload File
</template>
<template v-else>
Uploading...
<v-progress-circular
:size="24"
:width="2"
:model-value="uploadProgress"
color="primary"
/>
</template>
</v-btn>
<input
ref="fileInput"
aria-label="fileinput"
type="file"
:accept="acceptTypes"
style="display: none"
@change="handleFileSelected"
/>
<v-alert v-if="uploadError" color="error" dismissible>
{{ String(uploadError) }}
</v-alert>
<v-dialog v-model="successDialog" persistent width="25%">
<v-card>
<v-card-title>Upload Successful</v-card-title>
<v-card-text>
The file has been successfully uploaded.
Processing will begin to convert the file into map layers
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn @click="closeDialogs">
OK
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</template>