Skip to content

Commit b33bab5

Browse files
authored
Merge pull request #54 from SourceCodeOER/fix_fetch_exercise
fix(file): download file link
2 parents c97e1f5 + 82c13fb commit b33bab5

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

components/Gestion/ExerciseForm.vue

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@
133133
<Icon type="archive" theme="theme--white"/>
134134
{{labelFileText}}</label>
135135
<span class="error-message">{{errors[0]}}</span>
136-
<a v-if="exercise && exercise.file && filename" :href="`${cdnLink}/${exercise.file}`" target="_blank"
136+
<span v-if="exercise && exercise.file && filename" @click="downloadFile"
137137
class="message message--primary-color"
138-
style="text-decoration: underline; cursor: pointer;">Télécharger le fichier</a>
138+
style="text-decoration: underline; cursor: pointer;">Télécharger le fichier</span>
139139
<span class="message message--red" v-if="filename"
140140
style="text-decoration: underline; cursor: pointer;"
141141
@click="deleteFile">Supprimer le fichier</span>
@@ -209,6 +209,9 @@
209209
import TagColorLegend from "~/components/Tag/TagColorLegend.vue";
210210
211211
const debounce = require('lodash.debounce');
212+
213+
const download = require('downloadjs');
214+
212215
@Component({
213216
components: {TagColorLegend, CustomSelect, ValidationObserver, ValidationProvider, RichTextEditor, Tag, Icon}
214217
})
@@ -366,6 +369,34 @@
366369
}
367370
}
368371
372+
async downloadFile() {
373+
if(this.exercise) {
374+
try {
375+
376+
const result:Blob = await this.$axios.$get(`/files/${this.exercise.file}`, {responseType: 'blob'});
377+
378+
download(result, "archive.zip", result.type);
379+
380+
this.$displaySuccess("Téléchargement éffectué.");
381+
382+
} catch (e) {
383+
const error = e as AxiosError;
384+
385+
if (error.response) {
386+
const status: number = error.response.status;
387+
388+
if (status === 404) {
389+
this.$displayError(`Ce fichier est introuvable`);
390+
} else if (status === 500) {
391+
this.$displayError(`Une erreur est survenue lors du téléchargement.`);
392+
}
393+
} else {
394+
this.$displayError(`Une erreur est survenue lors du téléchargement.`);
395+
}
396+
}
397+
}
398+
}
399+
369400
mounted() {
370401
if (process.client && this.exercise !== undefined) {
371402
this.form.title = this.exercise.title;

components/Panel/Item/DetailsPanel.vue

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
</button>
2323
</a>
2424

25-
<a v-if="!!exercise.file" :href="`${cdnLink}/${exercise.file}`" target="_blank" class="button-wrapper">
26-
<button class=" button--ternary-color-reverse">
25+
<div v-if="!!exercise.file" @click="downloadFile" class="button-wrapper">
26+
<button class="button--ternary-color-reverse">
2727
Télécharger l'exercice
2828
</button>
29-
</a>
29+
</div>
3030

3131
</div>
3232
</div>
@@ -41,6 +41,8 @@
4141
import {BusEvent} from "~/components/Event/BusEvent";
4242
import {AxiosError} from "axios";
4343
44+
const download = require('downloadjs');
45+
4446
@Component({
4547
components: {Rating, Icon}
4648
})
@@ -91,6 +93,32 @@
9193
return arrayOfTagByCategories
9294
}
9395
96+
async downloadFile() {
97+
try {
98+
99+
const result:Blob = await this.$axios.$get(`/files/${this.exercise.file}`, {responseType: 'blob'});
100+
101+
download(result, "archive.zip", result.type);
102+
103+
this.$displaySuccess("Téléchargement éffectué.");
104+
105+
} catch (e) {
106+
const error = e as AxiosError;
107+
108+
if (error.response) {
109+
const status: number = error.response.status;
110+
111+
if (status === 404) {
112+
this.$displayError(`Ce fichier est introuvable`);
113+
} else if (status === 500) {
114+
this.$displayError(`Une erreur est survenue lors du téléchargement.`);
115+
}
116+
} else {
117+
this.$displayError(`Une erreur est survenue lors du téléchargement.`);
118+
}
119+
}
120+
}
121+
94122
created() {
95123
const link: string | undefined = this.$accessor.sharedEnv.CDN_SERVER;
96124
this.cdnLink = link ? link : ''

nuxt.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
},
3232
proxy: {
3333
'/api/': {target: undefined},
34+
'/files/': {target: undefined},
3435
'/auth/': {target: undefined}
3536
},
3637
auth: {

server/utils/extendedNuxtConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const nuxtConfig = {
1414

1515
// Do final object manipulation for things where
1616
// extending objects is not appropriate
17-
if (nuxtConfig.proxy && nuxtConfig.proxy['/api/'] && nuxtConfig.proxy['/auth/']) {
17+
if (nuxtConfig.proxy && nuxtConfig.proxy['/files/'] && nuxtConfig.proxy['/api/'] && nuxtConfig.proxy['/auth/']) {
1818
nuxtConfig.proxy['/api/'].target = config.get('API_SERVER');
1919
nuxtConfig.proxy['/auth/'].target = config.get('API_SERVER');
20+
nuxtConfig.proxy['/files/'].target = config.get('API_SERVER');
2021
}
2122

2223
module.exports = nuxtConfig;

0 commit comments

Comments
 (0)