From f0405b97446b04101f2fcfc315bbdb368a6a4183 Mon Sep 17 00:00:00 2001
From: Ron Lucke <lucke@elan-ev.de>
Date: Wed, 3 Apr 2024 13:17:59 +0000
Subject: [PATCH] fix #2565

Closes #2565

Merge request studip/studip!2463
---
 ...oursewareStructuralElementDialogImport.vue |  4 ++-
 resources/vue/mixins/courseware/import.js     | 32 +++++++++++++------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/resources/vue/components/courseware/structural-element/CoursewareStructuralElementDialogImport.vue b/resources/vue/components/courseware/structural-element/CoursewareStructuralElementDialogImport.vue
index 60b404ef461..019bbf2b98e 100644
--- a/resources/vue/components/courseware/structural-element/CoursewareStructuralElementDialogImport.vue
+++ b/resources/vue/components/courseware/structural-element/CoursewareStructuralElementDialogImport.vue
@@ -130,7 +130,6 @@ export default {
             this.importZipFile = event.target.files[0];
             this.setImportFilesProgress(0);
             this.setImportStructuresProgress(0);
-            this.setImportErrors([]);
         },
         async importCoursewareArchiv() {
             this.importAborted = false;
@@ -199,6 +198,9 @@ export default {
 
             await this.importCourseware(courseware, this.currentElement, files, this.importBehavior, null);
         }
+    },
+    mounted() {
+        this.setImportErrors([]);
     }
 }
 </script>
diff --git a/resources/vue/mixins/courseware/import.js b/resources/vue/mixins/courseware/import.js
index a7c94201dff..547280f2b35 100644
--- a/resources/vue/mixins/courseware/import.js
+++ b/resources/vue/mixins/courseware/import.js
@@ -53,7 +53,6 @@ export default {
             this.elementCounter = await this.countImportElements([element]);
             this.setImportStructuresState('');
             this.importElementCounter = 0;
-            this.setImportErrors([]);
 
             if (importBehavior === 'default') {
                 await this.importStructuralElement([element], rootId, files);
@@ -286,8 +285,14 @@ export default {
                     let new_file = this.file_mapping[files[i].id].new;
                     let payload = JSON.stringify(block.attributes.payload);
 
-                    payload = payload.replaceAll(old_file.id, new_file.id);
-                    payload = payload.replaceAll(old_file.folder.id, new_file.relationships.parent.data.id);
+                    if (new_file) {
+                        payload = payload.replaceAll(old_file.id, new_file.id);
+                        payload = payload.replaceAll(old_file.folder.id, new_file.relationships.parent.data.id);
+                    } else {
+                        payload = payload.replaceAll(old_file.id, '');
+                        payload = payload.replaceAll(old_file.folder.id, '');
+                    }
+                    
 
                     block.attributes.payload = JSON.parse(payload);
                 }
@@ -395,13 +400,20 @@ export default {
 
                         // create new blob with correct type
                         let filedata = zip_filedata.slice(0, zip_filedata.size, files[i].attributes['mime-type']);
-
-                        let file = await this.createFile({
-                            file: files[i],
-                            filedata: filedata,
-                            folder: folders[files[i].folder.id]
-                        });
-                        this.setImportFilesState(this.$gettext('Erzeuge Datei') + ': ' + files[i].attributes.name);
+                        let file = null;
+                        try {
+                            file = await this.createFile({
+                                file: files[i],
+                                filedata: filedata,
+                                folder: folders[files[i].folder.id]
+                            });
+                        } catch (error) {
+                            this.currentImportErrors.push(this.$gettext('Import einer Datei fehlgeschlagen.'));
+                            this.setImportFilesState(this.$gettext('Fehler beim Anlegen der Datei'));
+                        }
+                        if (file !== null) {
+                            this.setImportFilesState(this.$gettext('Erzeuge Datei') + ': ' + files[i].attributes.name);
+                        }
                         this.setImportFilesProgress(parseInt(i / files.length * 100));
 
                         //file mapping
-- 
GitLab