chore(backend): Aanpassingen Dwengo Learning-Object-Repository
Processing uit Dwengo Learning-Object-Repository geconverteerd naar TypeScript en aangepast aan onze app. Functionaliteit van Dwengo Learning-Object-Repository in ons project gekopiëerd en deels aanBestanden die enkel types of interfaces exporteren hernoemd naar *.d.tsgepast aan TypeScript en ons project.
This commit is contained in:
parent
ba3da01d2d
commit
463c8c9fc0
45 changed files with 1258 additions and 3747 deletions
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Based on https://github.com/dwengovzw/Learning-Object-Repository/blob/main/app/processors/image/block_image_processor.js
|
||||
*/
|
||||
|
||||
import InlineImageProcessor from "./inline-image-processor.js"
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
|
||||
class BlockImageProcessor extends InlineImageProcessor {
|
||||
constructor(){
|
||||
super();
|
||||
}
|
||||
|
||||
override renderFn(imageUrl: string){
|
||||
let inlineHtml = super.render(imageUrl);
|
||||
return DOMPurify.sanitize(`<div>${inlineHtml}</div>`);
|
||||
}
|
||||
}
|
||||
|
||||
export default BlockImageProcessor;
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
import InlineImageProcessor from "./inline_image_processor.js"
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
import path from 'path'
|
||||
|
||||
class BlockImageProcessor extends InlineImageProcessor{
|
||||
constructor(){
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} imageUrl
|
||||
* @param {object} args Optional arguments specific to the render function of the BlockImageProcessor
|
||||
* @returns
|
||||
*/
|
||||
render(imageUrl, args = { altText: ""}){
|
||||
let inlineHtml = super.render(imageUrl, args);
|
||||
return DOMPurify.sanitize(`<div>${inlineHtml}</div>`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BlockImageProcessor;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* Based on https://github.com/dwengovzw/Learning-Object-Repository/blob/main/app/processors/image/inline_image_processor.js
|
||||
*/
|
||||
|
||||
import Processor from "../processor.js";
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
import {DwengoContentType} from "../content-type.js";
|
||||
import {ProcessingError} from "../processing-error.js";
|
||||
import {isValidHttpUrl} from "../../../../util/links";
|
||||
|
||||
class InlineImageProcessor extends Processor<string> {
|
||||
constructor(contentType: DwengoContentType = DwengoContentType.IMAGE_INLINE) {
|
||||
super(contentType);
|
||||
}
|
||||
|
||||
override renderFn(imageUrl: string) {
|
||||
if (!isValidHttpUrl(imageUrl)) {
|
||||
throw new ProcessingError(`Image URL is invalid: ${imageUrl}`);
|
||||
}
|
||||
return DOMPurify.sanitize(`<img src="${imageUrl}" alt="">`);
|
||||
}
|
||||
}
|
||||
|
||||
export default InlineImageProcessor;
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
import Processor from "../processor.ts";
|
||||
import { isValidHttpUrl } from '../../utils/utils.js'
|
||||
import InvalidArgumentError from '../../utils/invalid_argument_error.js'
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
import ProcessingHistory from "../../models/processing_history.js";
|
||||
import path from "path"
|
||||
|
||||
class InlineImageProcessor extends Processor {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} imageUrl
|
||||
* @param {object} args Optional arguments specific to the render function of the InlineImageProcessor
|
||||
* @returns
|
||||
*/
|
||||
render(imageUrl, args = { altText: "", metadata: {} }) {
|
||||
|
||||
if (!isValidHttpUrl(imageUrl) && (!imageUrl || !imageUrl.toLowerCase().match(/^(?!http.*$)[^.].*\.(jpe?g|png|svg|gif)/))) {
|
||||
if (args.metadata && args.metadata.hruid && args.metadata.version && args.metadata.language){
|
||||
ProcessingHistory.error(args.metadata.hruid, args.metadata.version, args.metadata.language, "The image cannot be found. Please check if the url is spelled correctly.")
|
||||
}else{
|
||||
ProcessingHistory.error("generalError", "99999999", "en", "The image cannot be found. Please check if the url is spelled correctly.")
|
||||
}
|
||||
throw new InvalidArgumentError("The image cannot be found. Please check if the url is spelled correctly.");
|
||||
}
|
||||
|
||||
if (typeof args.altText == 'undefined') {
|
||||
args.altText = "";
|
||||
}
|
||||
|
||||
if (isValidHttpUrl(imageUrl)) {
|
||||
return DOMPurify.sanitize(`<img src="${imageUrl}" alt="${args.altText}">`);
|
||||
|
||||
}
|
||||
|
||||
if (!args.metadata._id) {
|
||||
throw new InvalidArgumentError("The metadata for for the object which uses the file '" + imageUrl + "' is not loaded in the processor.");
|
||||
}
|
||||
return DOMPurify.sanitize(`<img src="@@URL_REPLACE@@/${process.env.LEARNING_OBJECT_STORAGE_NAME}/${args.metadata._id}/${imageUrl}" alt="${args.altText}">`);
|
||||
}
|
||||
|
||||
processFiles(files, metadata){
|
||||
let args = {};
|
||||
let inputString = "";
|
||||
let file = files.find((f) => {
|
||||
let ext = path.extname(f.originalname);
|
||||
if (ext.match(/\.(jpe?g)|(png)|(svg)$/)){
|
||||
inputString = f["originalname"];
|
||||
args.metadata = metadata
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return [this.render(inputString, args), files]
|
||||
}
|
||||
}
|
||||
|
||||
export default InlineImageProcessor;
|
||||
Reference in a new issue