From 0f836d96d195385bf659f670b73c1c32b8ce59bd Mon Sep 17 00:00:00 2001 From: guillaume-pages Date: Mon, 27 May 2024 15:38:49 +0200 Subject: [PATCH 1/2] modif button download --- .../admin/mediatheque/DownloadButton.tsx | 67 +++++++++++++++---- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/src/components/admin/mediatheque/DownloadButton.tsx b/src/components/admin/mediatheque/DownloadButton.tsx index 89f679f84..03eec0c5e 100644 --- a/src/components/admin/mediatheque/DownloadButton.tsx +++ b/src/components/admin/mediatheque/DownloadButton.tsx @@ -12,17 +12,6 @@ export default function DownloadButton() { const { allFiltered } = useContext(MediathequeContext); console.log(allFiltered); - // const createJsonFiles = async (data) => { - // const zip = new JSZip(); - // data.forEach((item, index) => { - // const jsonString = JSON.stringify(item, null, 2); - // zip.file(`media-${index + 1}.json`, jsonString); - // }); - - // const content = await zip.generateAsync({ type: 'blob' }); - // saveAs(content, 'media-library.zip'); - // }; - const getActivityLabel = (type) => { const activityEntry = Object.entries(activityNumberMapper).find(([label, number]) => number === type); return activityEntry ? activityEntry[0] : `UnknownType${type}`; @@ -35,14 +24,64 @@ export default function DownloadButton() { return subThemeEntry ? subThemeEntry[0] : `UnknownSubType${subType}`; }; + const getFileExtension = (url) => { + const match = url.match(/\.(jpeg|jpg|png)$/i); + return match ? match[1] : 'png'; + }; + const createJsonFiles = async (data) => { const zip = new JSZip(); - data.forEach((item, index) => { + const imagePromises = []; + const videoLinks = []; + + data.forEach((item) => { const activityLabel = getActivityLabel(item.type); const subThemeLabel = getSubThemeLabel(item.type, item.subType); - const jsonString = JSON.stringify(item, null, 2); - zip.file(`media-${activityLabel}-${subThemeLabel}-${index + 1}.json`, jsonString); + + // Process images and videos + item.content.forEach((contentItem, contentIndex) => { + if (contentItem.type === 'image') { + const imageUrl = contentItem.value; + const imageExtension = getFileExtension(imageUrl); + const imageFileName = `media-${activityLabel}-${subThemeLabel}-image-activity_id_${item.id}-${contentIndex + 1}.${imageExtension}`; + console.log(contentIndex); + + // Fetch the image and add it to the zip + const imagePromise = fetch(imageUrl) + .then((response) => response.blob()) + .then((blob) => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onloadend = () => { + const base64data = reader.result.split(',')[1]; + zip.file(imageFileName, base64data, { base64: true }); + resolve(); + }; + reader.onerror = reject; + reader.readAsDataURL(blob); + }); + }) + .catch((err) => { + console.error(`Failed to fetch image from ${imageUrl}:`, err); + }); + + imagePromises.push(imagePromise); + } else if (contentItem.type === 'video') { + const videoUrl = contentItem.value; + videoLinks.push(`Video ${item.id}-${contentIndex + 1}: ${videoUrl}`); + } + }); }); + + // Wait for all images to be fetched and added to the zip + await Promise.all(imagePromises); + + // Add video links file + if (videoLinks.length > 0) { + const videoLinksFileContent = videoLinks.join('\n'); + zip.file('video-links.txt', videoLinksFileContent); + } + const content = await zip.generateAsync({ type: 'blob' }); saveAs(content, 'media-library.zip'); }; From 572aaaf690fa75b7e339193eea3567389b6409dc Mon Sep 17 00:00:00 2001 From: guillaume-pages Date: Mon, 27 May 2024 16:08:02 +0200 Subject: [PATCH 2/2] resolve pubish game --- server/controllers/game.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/controllers/game.ts b/server/controllers/game.ts index 664f5044f..1975e7f43 100644 --- a/server/controllers/game.ts +++ b/server/controllers/game.ts @@ -1,8 +1,9 @@ import type { JSONSchemaType } from 'ajv'; import type { NextFunction, Request, Response } from 'express'; -import type { ActivityContent, AnyData } from '../entities/activity'; -import { Activity, ActivityStatus } from '../entities/activity'; +import type { ActivityContent, AnyData } from '../../types/activity.type'; +import { ActivityStatus } from '../../types/activity.type'; +import { Activity } from '../entities/activity'; import { Game } from '../entities/game'; import { GameResponse } from '../entities/gameResponse'; import { UserType } from '../entities/user';