Skip to content

Commit

Permalink
feat: local files work
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Feb 4, 2022
1 parent fd107b9 commit 1b61de1
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 18 deletions.
49 changes: 47 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
"author": "Sebastian Rettig",
"license": "MIT",
"dependencies": {
"@types/yargs": "^17.0.1",
"axios": "^0.21.1",
"buffer-image-size": "^0.6.4",
"chalk": "^4.1.1",
"fs-extra": "^10.0.0",
"jszip": "^3.6.0",
"mime-types": "^2.1.34",
"papaparse": "^5.3.1",
"ts-node": "^10.0.0",
"tslint": "^5.20.1",
"typescript": "^4.3.5",
"yargs": "^17.0.1"
},
"devDependencies": {
"@types/mime-types": "^2.1.1",
"@types/yargs": "^17.0.1",
"@types/fs-extra": "^9.0.11",
"@types/jszip": "^3.4.0",
"@types/node": "^16.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/content-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class ContentCreator<T extends H5pContent> {
*/
protected content: T;

public constructor(protected h5pPackage: H5pPackage) {
public constructor(protected h5pPackage: H5pPackage, protected sourcePath: string) {
this.clearPackageContent();
this.content = this.contentObjectFactory();
}
Expand Down
31 changes: 27 additions & 4 deletions src/dialogcards-creator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from "path";

import { ContentCreator } from "./content-creator";
import { H5pPackage } from "./h5p-package";
import { H5pAudio } from "./models/h5p-audio";
Expand All @@ -13,9 +15,10 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
image?: string;
audio?: string;
}>,
private mode: "repetition" | "normal"
private mode: "repetition" | "normal",
sourcePath: string
) {
super(h5pPackage);
super(h5pPackage, sourcePath);
}

/**
Expand Down Expand Up @@ -46,7 +49,17 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
};
if (line.image) {
try {
let ret = await H5pImage.fromDownload(line.image);
let ret: { extension: string; buffer: Buffer; image: H5pImage };
if (
!line.image.startsWith("http://") &&
!line.image.startsWith("https://")
) {
ret = await H5pImage.fromLocalFile(
path.join(this.sourcePath, line.image)
);
} else {
ret = await H5pImage.fromDownload(line.image);
}
let filename = this.getFilenameForImage(
imageCounter++,
ret.extension
Expand All @@ -64,7 +77,17 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
}
if (line.audio) {
try {
let ret = await H5pAudio.fromDownload(line.audio);
let ret: { extension: string; buffer: Buffer; audio: H5pAudio };
if (
!line.audio.startsWith("http://") &&
!line.audio.startsWith("https://")
) {
ret = await H5pAudio.fromLocalFile(
path.join(this.sourcePath, line.audio)
);
} else {
ret = await H5pAudio.fromDownload(line.audio);
}
let filename = this.getFilenameForAudio(
audioCounter++,
ret.extension
Expand Down
4 changes: 3 additions & 1 deletion src/dialogcards-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as papa from "papaparse";
import * as yargs from "yargs";
import * as path from "path";

import { DialogCardsCreator } from "./dialogcards-creator";
import { H5pPackage } from "./h5p-package";
Expand Down Expand Up @@ -76,7 +77,8 @@ export class DialogCardsModule implements yargs.CommandModule {
let creator = new DialogCardsCreator(
h5pPackage,
csvParsed.data as any,
mode
mode,
path.dirname(csvfile)
);
await creator.create();
creator.setTitle(title);
Expand Down
22 changes: 18 additions & 4 deletions src/flashcards-creator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from "path";

import { ContentCreator } from "./content-creator";
import { H5pPackage } from "./h5p-package";
import { H5pFlashcardsContent } from "./models/h5p-flashcards-content";
Expand All @@ -13,9 +15,10 @@ export class FlashcardsCreator extends ContentCreator<H5pFlashcardsContent> {
tip?: string;
}>,
private description: string,
private title: string
private title: string,
sourcePath: string
) {
super(h5pPackage);
super(h5pPackage, sourcePath);
}

protected contentObjectFactory(): H5pFlashcardsContent {
Expand All @@ -32,11 +35,21 @@ export class FlashcardsCreator extends ContentCreator<H5pFlashcardsContent> {
for (const line of this.data) {
const card = {
answer: line.answer,
text: line.question
text: line.question,
};
if (line.image) {
try {
let ret = await H5pImage.fromDownload(line.image);
let ret: { extension: string; buffer: Buffer; image: H5pImage };
if (
!line.image.startsWith("http://") &&
!line.image.startsWith("https://")
) {
ret = await H5pImage.fromLocalFile(
path.join(this.sourcePath, line.image)
);
} else {
ret = await H5pImage.fromDownload(line.image);
}
let filename = this.getFilenameForImage(
imageCounter++,
ret.extension
Expand All @@ -52,6 +65,7 @@ export class FlashcardsCreator extends ContentCreator<H5pFlashcardsContent> {
card["image"] = undefined;
}
}

if (line.tip) {
card["tip"] = line.tip;
}
Expand Down
4 changes: 3 additions & 1 deletion src/flashcards-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";
import * as papa from "papaparse";
import * as path from "path";
import * as yargs from "yargs";

import { FlashcardsCreator } from "./flashcards-creator";
Expand Down Expand Up @@ -73,7 +74,8 @@ export class FlashcardsModule implements yargs.CommandModule {
h5pPackage,
csvParsed.data as any,
description,
title
title,
path.dirname(csvfile)
);
await flashcardsCreator.create();
flashcardsCreator.savePackage(outputfile);
Expand Down
16 changes: 16 additions & 0 deletions src/models/h5p-audio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from "axios";
import { lookup } from "mime-types";
import * as fs from "fs";

import { extname } from "path";
import { toBuffer } from "../helpers";
Expand Down Expand Up @@ -31,6 +33,20 @@ export class H5pAudio extends H5pContent {
};
}

public static async fromLocalFile(
path: string
): Promise<{ audio: H5pAudio; buffer: Buffer; extension: string }> {
let a = new H5pAudio();
a.mime = (lookup(path) || "image").replace("audio/mp3", "audio/mpeg");
a.copyright.license = "U";
const buffer = fs.readFileSync(path);
return {
audio: a,
buffer,
extension: extname(path),
};
}

public path: string;
public mime: string;
public copyright: H5pCopyrightInformation = new H5pCopyrightInformation();
Expand Down
21 changes: 21 additions & 0 deletions src/models/h5p-image.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from "axios";
import * as imageSize from "buffer-image-size";
import { lookup } from "mime-types";
import * as fs from "fs";

import { extname } from "path";
import { toBuffer } from "../helpers";
Expand Down Expand Up @@ -35,6 +37,25 @@ export class H5pImage extends H5pContent {
};
}

public static async fromLocalFile(path: string): Promise<{
image: H5pImage;
buffer: Buffer;
extension: string;
}> {
let i = new H5pImage();
i.mime = lookup(path) || "image";
i.copyright.license = "U";
const buffer = fs.readFileSync(path);
const dim = imageSize(buffer);
i.width = dim.width;
i.height = dim.height;
return {
image: i,
buffer,
extension: extname(path),
};
}

public path: string;
public mime: string;
public copyright: H5pCopyrightInformation = new H5pCopyrightInformation();
Expand Down
4 changes: 2 additions & 2 deletions tests/dialog1.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
front;back;image;audio
question 1;answer 1;https://homepages.cae.wisc.edu/~ece533/images/cat.png
question 1;answer 1;https://cdn.pixabay.com/photo/2016/09/27/03/39/overcoming-1697546_960_720.jpg;
question 2;answer 2;;https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3
question 3;answer 3;
question 3;answer 3;;sound.wav
question 4;answer 4;
question 5;answer 5;
question 6;answer 6;
Expand Down
4 changes: 2 additions & 2 deletions tests/flash1.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
question;answer;tip;image
question 1;answer 1;#1;https://homepages.cae.wisc.edu/~ece533/images/cat.png
question 2;answer 2;#2;
question 1;answer 1;#1;image1.jpg
question 2;answer 2;#2;https://cdn.pixabay.com/photo/2022/01/10/18/09/budapest-6928973_960_720.jpg
question 3;answer 3;#3;
question 4;answer 4;#4;
question 5;answer 5;#5;
Expand Down
Binary file added tests/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/sound.wav
Binary file not shown.

0 comments on commit 1b61de1

Please sign in to comment.