From f3046f9af6aa3e352ef3deac3b5b81198011f4d2 Mon Sep 17 00:00:00 2001
From: Ron Lucke <lucke@elan-ev.de>
Date: Tue, 23 Jul 2024 09:29:13 +0000
Subject: [PATCH] evaluate image type of structural element on import and
 export

Closes #3743

Merge request studip/studip!3175
---
 resources/vue/mixins/courseware/export.js | 40 +++++++++++++----------
 resources/vue/mixins/courseware/import.js | 38 ++++++++++++---------
 2 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/resources/vue/mixins/courseware/export.js b/resources/vue/mixins/courseware/export.js
index e5c92ad276a..3d9e33ab4e8 100644
--- a/resources/vue/mixins/courseware/export.js
+++ b/resources/vue/mixins/courseware/export.js
@@ -134,7 +134,7 @@ export default {
                     root_element.children = children;
                 }
             }
-            root_element.imageId = await this.exportStructuralElementImage(root_element);
+            [root_element.imageId, root_element.imageType ] = await this.exportStructuralElementImage(root_element);
 
             delete root_element.relationships;
             delete root_element.links;
@@ -236,7 +236,7 @@ export default {
                     }
 
                     // export file data (if any)
-                    content.imageId = await this.exportStructuralElementImage(element);
+                    [content.imageId, content.imageType ] = await this.exportStructuralElementImage(element);
 
                     delete content.relationships;
                     content.children = new_childs;
@@ -249,25 +249,29 @@ export default {
         },
 
         async exportStructuralElementImage(element) {
-            let fileId = element.relationships.image?.data?.id;
+            const fileId = element.relationships.image?.data?.id;
+            const fileType = element.relationships.image?.data?.type;
+
             if (fileId) {
-                await this.loadFileRefsById({id: fileId});
-                let fileRef = this.fileRefsById({id: fileId});
-                
-                let fileRefData = {};
-                fileRefData.id = fileRef.id;
-                fileRefData.attributes = fileRef.attributes;
-                fileRefData.related_element_id = element.id;
-                fileRefData.folder = null;
-
-                this.exportFiles.json.push(fileRefData);
-                this.exportFiles.download[fileRef.id] = {
-                    folder: null,
-                    url: fileRef.meta['download-url']
-                };
+                if (fileType === 'file-refs') {
+                    await this.loadFileRefsById({id: fileId});
+                    let fileRef = this.fileRefsById({id: fileId});
+                    
+                    let fileRefData = {};
+                    fileRefData.id = fileRef.id;
+                    fileRefData.attributes = fileRef.attributes;
+                    fileRefData.related_element_id = element.id;
+                    fileRefData.folder = null;
+    
+                    this.exportFiles.json.push(fileRefData);
+                    this.exportFiles.download[fileRef.id] = {
+                        folder: null,
+                        url: fileRef.meta['download-url']
+                    };
+                }
             }
 
-            return fileId;
+            return [fileId, fileType];
         },
 
         async exportContainer(container_ref) {
diff --git a/resources/vue/mixins/courseware/import.js b/resources/vue/mixins/courseware/import.js
index 547280f2b35..649a09809f8 100644
--- a/resources/vue/mixins/courseware/import.js
+++ b/resources/vue/mixins/courseware/import.js
@@ -152,7 +152,7 @@ export default {
             }
             // compare image
             if (element.imageId && root.relationships.image.data === null) {
-                await this.setStructuralElementImage(root, element.imageId, files);
+                await this.setStructuralElementImage(root, element, files);
             }
 
             // add children
@@ -187,7 +187,7 @@ export default {
                     
 
                     if (element[i].imageId) {
-                        await this.setStructuralElementImage(new_element, element[i].imageId, files);
+                        await this.setStructuralElementImage(new_element, element[i], files);
                     }
 
 
@@ -205,19 +205,27 @@ export default {
             }
         },
 
-        async setStructuralElementImage(new_element, imageId, files) {
-            let imageFile = files.find((file) => { return file.id === imageId});
-            let zip_filedata = await this.zip.file(imageFile.id).async('blob');
-            // create new blob with correct type
-            let filedata = zip_filedata.slice(0, zip_filedata.size, imageFile.attributes['mime-type']);
-            this.setImportStructuresState(this.$gettext('Lade Vorschaubild hoch'));
-            this.uploadImageForStructuralElement({
-                structuralElement: new_element,
-                file: filedata,
-            }).catch((error) => {
-                console.error(error);
-                this.currentImportErrors.push(this.$gettext('Fehler beim Hochladen des Vorschaubildes.'));
-            });
+        async setStructuralElementImage(new_element, element, files) {
+            const imageId = element.imageId;
+            const imageType = element.imageType ?? 'file-refs';
+            if (imageType === 'file-refs') {
+                const imageFile = files.find((file) => { return file.id === imageId});
+                if (!(imageFile)) {
+                    this.currentImportErrors.push(this.$gettext('Fehler beim Laden des Vorschaubildes.'));
+                    return;
+                }
+                let zip_filedata = await this.zip.file(imageFile.id).async('blob');
+                // create new blob with correct type
+                const filedata = zip_filedata.slice(0, zip_filedata.size, imageFile.attributes['mime-type']);
+                this.setImportStructuresState(this.$gettext('Lade Vorschaubild hoch'));
+                this.uploadImageForStructuralElement({
+                    structuralElement: new_element,
+                    file: filedata,
+                }).catch((error) => {
+                    console.error(error);
+                    this.currentImportErrors.push(this.$gettext('Fehler beim Hochladen des Vorschaubildes.'));
+                });
+            }
         },
 
         async importContainer(container, structuralElement, files) {
-- 
GitLab