Skip to content

Commit

Permalink
feat(media): add audio icon SVG and enhance SlideMedia with media typ…
Browse files Browse the repository at this point in the history
…e detection

feat(export): enhance media handling by copying media parts and relationships during slide export

media audio stable

media based on content-type

feat(slide): refactor media icon creation and positioning for improved layout

video stable
  • Loading branch information
ThomasSevagen committed Feb 26, 2025
1 parent 75a97a6 commit 8e8e5fe
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 50 deletions.
8 changes: 7 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand Down Expand Up @@ -130,6 +131,11 @@
<artifactId>poi-ooxml</artifactId>
<version>${apachePoiVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
<version>${apachePoiVersion}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.entcore.common.controller.ControllerHelper;
import org.entcore.common.user.UserUtils;

Expand Down Expand Up @@ -38,6 +42,21 @@ public void exportBoardToPPTX(HttpServerRequest request) {
.putHeader("Content-Disposition", "attachment; filename=\"board.pptx\"");

ByteArrayOutputStream out = new ByteArrayOutputStream();
System.out.println("Parties du package avant sauvegarde :");
OPCPackage opcPackage = ppt.getPackage();
try {
for (PackagePart part : opcPackage.getParts()) {
System.out.println(part.getPartName());
}
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("\nRelations du package :");
for (PackageRelationship rel : opcPackage.getRelationships()) {
System.out.println(rel.getRelationshipType() + " : " + rel.getTargetURI());
}
ppt.write(out);
request.response().end(Buffer.buffer(out.toByteArray()));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Slide createSlide(SlideResourceType type, SlideProperties properties) {
case VIDEO:
case AUDIO:
return new SlideMedia(properties.getTitle(), properties.getCaption(),
properties.getResourceData(), properties.getExtension());
properties.getResourceData(), properties.getContentType());
case BOARD:
return new SlideBoard(
properties.getTitle(), properties.getDescription(),
Expand Down
615 changes: 602 additions & 13 deletions backend/src/main/java/fr/cgi/magneto/helper/SlideHelper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class SlideProperties {
private String content;
private String resourceUrl;
private String resourceId;
private String extension;
private String fileName;
private byte[] resourceData;
private String contentType;

private String ownerName;
private String modificationDate;
Expand Down Expand Up @@ -59,8 +59,8 @@ public Builder resourceId(String resourceId) {
return this;
}

public Builder extension(String extension) {
properties.extension = extension;
public Builder contentType(String contentType) {
properties.contentType = contentType;
return this;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ private boolean isValidForLink() {
}

private boolean isValidForMedia() {
return title != null && caption != null && extension != null && resourceData != null;
return title != null && caption != null && contentType != null && resourceData != null;
}

private boolean isValidForBoard() {
Expand Down Expand Up @@ -180,8 +180,8 @@ public String getResourceId() {
return resourceId;
}

public String getExtension() {
return extension;
public String getContentType() {
return contentType;
}

public String getFileName() {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/fr/cgi/magneto/model/slides/Slide.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public abstract class Slide {
protected String title;
protected String description = "";

public abstract Object createApacheSlide();
public abstract Object createApacheSlide(XSLFSlide newSlide);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public class SlideBoard extends Slide {
private final String ownerName;
private final String modificationDate;
Expand All @@ -19,7 +21,7 @@ public SlideBoard(String title, String description, String ownerName, String mod
}

@Override
public Object createApacheSlide() {
public Object createApacheSlide(XSLFSlide newSlide) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public class SlideDescription extends Slide {

public SlideDescription(String description) {
this.description = description;
}

@Override
public Object createApacheSlide() {
public Object createApacheSlide(XSLFSlide newSlide) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public class SlideFile extends Slide {
private final String fileName;
private final String desc;
Expand All @@ -10,7 +12,7 @@ public SlideFile(String fileName, String desc) {
}

@Override
public Object createApacheSlide() {
public Object createApacheSlide(XSLFSlide newSlide) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public class SlideLink extends Slide {
private final String link;

Expand All @@ -8,7 +10,7 @@ public SlideLink(String link) {
}

@Override
public Object createApacheSlide() {
public Object createApacheSlide(XSLFSlide newSlide) {
return null;
}
}
74 changes: 62 additions & 12 deletions backend/src/main/java/fr/cgi/magneto/model/slides/SlideMedia.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,83 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import fr.cgi.magneto.helper.SlideHelper;

public class SlideMedia extends Slide {

public enum MediaType {
IMAGE,
AUDIO,
VIDEO
}

private byte[] resourceData;
private final String fileExtension;
private final String fileContentType;
private final String caption;
private final MediaType mediaType;

public SlideMedia(String title, String caption, byte[] resourceData,
String fileExtension) {
String fileContentType) {
this.title = title;
this.caption = caption;
this.resourceData = resourceData;
this.fileExtension = fileExtension;
this.fileContentType = fileContentType;
this.mediaType = determineMediaType(fileContentType);

// Log des informations dans le constructeur
System.out.println("SlideMedia - Construction:");
System.out.println("Title: " + title);
System.out.println("Caption: " + caption);
System.out.println("File extension: " + fileContentType);
System.out.println(
"Resource data present: " + (resourceData != null ? "Yes (" + resourceData.length + " bytes)" : "No"));
System.out.println("Determined media type: " + mediaType);
}

@Override
public Object createApacheSlide() {
private MediaType determineMediaType(String contentType) {
String type = contentType.toLowerCase();
MediaType mediaType;

if (type.startsWith("audio/")) {
mediaType = MediaType.AUDIO;
} else if (type.startsWith("video/")) {
mediaType = MediaType.VIDEO;
} else {
mediaType = MediaType.IMAGE;
}

XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
// Log du type de média déterminé
System.out.println("determineMediaType - Content Type: " + contentType + " -> Type: " + mediaType);

return mediaType;
}

@Override
public Object createApacheSlide(XSLFSlide newSlide) {
// Log au début de la création de la slide
System.out.println("\nStarting createApacheSlide:");
System.out.println("Title: " + title);
System.out.println("Media type: " + mediaType);
System.out.println("Resource data size: " + (resourceData != null ? resourceData.length : "null") + " bytes");
System.out.println("File extension: " + fileContentType);

SlideHelper.createTitle(slide, title);
SlideHelper.createImage(slide, resourceData, fileExtension);
SlideHelper.createLegend(slide, caption);
SlideHelper.createTitle(newSlide, title);
switch (mediaType) {
case AUDIO:
System.out.println("Creating audio slide...");
SlideHelper.createAudio(newSlide, resourceData, fileContentType);
break;
case VIDEO:
System.out.println("Creating video slide...");
SlideHelper.createVideo(newSlide, resourceData, fileContentType);
break;
default:
System.out.println("Creating image slide...");
SlideHelper.createImage(newSlide, resourceData, fileContentType);
}
SlideHelper.createLegend(newSlide, caption);

return slide;
System.out.println("Slide creation completed.");
return newSlide;
}
}
10 changes: 4 additions & 6 deletions backend/src/main/java/fr/cgi/magneto/model/slides/SlideText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ public SlideText(String title, String description) {
}

@Override
public Object createApacheSlide() {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
public Object createApacheSlide(XSLFSlide newSlide) {

SlideHelper.createTitle(slide, title);
XSLFTextBox contentBox = SlideHelper.createContent(slide);
SlideHelper.createTitle(newSlide, title);
XSLFTextBox contentBox = SlideHelper.createContent(newSlide);

Document doc = Jsoup.parse(description);
processHtmlContent(contentBox, doc.body());

return slide;
return newSlide;
}

private void processHtmlContent(XSLFTextBox textBox, Element element) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.cgi.magneto.model.slides;

import org.apache.poi.xslf.usermodel.XSLFSlide;

public class SlideTitle extends Slide {
private final String title;

Expand All @@ -8,7 +10,7 @@ public SlideTitle(String title) {
}

@Override
public Object createApacheSlide() {
public Object createApacheSlide(XSLFSlide newSlide) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.cgi.magneto.service.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -8,6 +10,11 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.entcore.common.user.UserInfos;
Expand Down Expand Up @@ -110,13 +117,14 @@ private Future<List<Map<String, Object>>> getBoardDocuments(List<String> documen
String fileExtension = filename.contains(".")
? filename.substring(filename.lastIndexOf(".") + 1)
: "";

JsonObject metadata = document.getJsonObject("metadata", new JsonObject());
return Future.<Void>future(promise -> {
serviceFactory.storage().readFile(fileId, buffer -> {
if (buffer != null) {
Map<String, Object> docInfo = new HashMap<>();
docInfo.put("documentId", documentId);
docInfo.put("buffer", buffer);
docInfo.put("contentType", metadata.getString("content-type", ""));
docInfo.put("extension", fileExtension);
documents.add(docInfo);
promise.complete();
Expand Down Expand Up @@ -160,8 +168,13 @@ private Future<XMLSlideShow> createFreeLayoutSlideObjects(Board board, UserInfos
if (card != null) {
try {
Slide slide = createSlideFromCard(card, slideFactory, slideShowData, documents);
XSLFSlide apacheSlide = (XSLFSlide) slide.createApacheSlide();
ppt.createSlide().importContent(apacheSlide);
XSLFSlide newSlide = ppt.createSlide();
slide.createApacheSlide(newSlide);
// Inspecter le contenu du package après l'ajout de la diapositive
log.info("Package parts after adding slide:");
for (PackagePart part : ppt.getPackage().getParts()) {
log.info("- " + part.getPartName());
}
} catch (Exception e) {
String message = String.format(
"[Magneto@%s::createFreeLayoutSlideObjects] Failed to create slide for card %s: %s",
Expand Down Expand Up @@ -214,10 +227,16 @@ private Slide createSlideFromCard(Card card, SlideFactory slideFactory, JsonObje
}
Map<String, Object> documentData = documentMap.get(card.getResourceId());

System.out.println("Document data found for resource ID " + card.getResourceId() + ": " +
(documentData != null ? "yes" : "no"));

Buffer documentBuffer = documentData != null ? (Buffer) documentData.get("buffer") : null;
String fileExtension = documentData != null ? (String) documentData.get("extension") : "";
String contentType = documentData != null ? (String) documentData.get("contentType") : "";

System.out.println("Final contentType: " + contentType);
System.out.println("Buffer present: " + (documentBuffer != null));
propertiesBuilder
.extension(fileExtension)
.contentType(contentType)
.resourceData(documentBuffer != null ? documentBuffer.getBytes() : null)
.caption(card.getCaption());
break;
Expand Down
6 changes: 6 additions & 0 deletions backend/src/main/resources/img/audio_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e8e5fe

Please sign in to comment.