Skip to content

Commit

Permalink
Merge branch 'pull-game-into-media' of https://github.com/parlemonde/…
Browse files Browse the repository at this point in the history
…1village into Feature_Mediatheque_VIL97
  • Loading branch information
nathan7594 committed May 27, 2024
2 parents 98fe04a + 572aaaf commit 5242c76
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
5 changes: 3 additions & 2 deletions server/controllers/game.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
67 changes: 53 additions & 14 deletions src/components/admin/mediatheque/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -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');
};
Expand Down

0 comments on commit 5242c76

Please sign in to comment.