@defikitdotnet/education-module-ai
Version:
AI Education Module using Agent Framework
220 lines (219 loc) • 11 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.H5PContentController = void 0;
var path_1 = __importDefault(require("path"));
var fs_1 = __importDefault(require("fs"));
var errorHandler_1 = require("../middleware/errorHandler");
/**
* Controller for H5P content management
*/
var H5PContentController = /** @class */ (function () {
function H5PContentController(contentDirectory) {
var _this = this;
/**
* Get a list of all H5P content
*/
this.getAllContent = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
var contentDirs, contentList;
var _this = this;
return __generator(this, function (_a) {
try {
// If the directory doesn't exist yet or is empty, return an empty array
if (!fs_1.default.existsSync(this.contentDirectory)) {
res.status(200).json([]);
return [2 /*return*/];
}
contentDirs = fs_1.default
.readdirSync(this.contentDirectory, { withFileTypes: true })
.filter(function (dirent) { return dirent.isDirectory(); })
.map(function (dirent) { return dirent.name; });
contentList = contentDirs.map(function (dir) {
try {
// Try to read the h5p.json file to get content metadata
var h5pJsonPath = path_1.default.join(_this.contentDirectory, dir, "h5p.json");
if (fs_1.default.existsSync(h5pJsonPath)) {
var h5pJson = JSON.parse(fs_1.default.readFileSync(h5pJsonPath, "utf8"));
return {
id: dir,
title: h5pJson.title || "Untitled",
contentType: h5pJson.mainLibrary || "Unknown",
};
}
return {
id: dir,
title: "Content ".concat(dir),
contentType: "Unknown",
};
}
catch (error) {
console.error("Error processing H5P content ".concat(dir, ":"), error);
return {
id: dir,
title: "Content ".concat(dir),
contentType: "Unknown",
};
}
});
res.status(200).json(contentList);
}
catch (error) {
console.error("Error fetching H5P content:", error);
throw new errorHandler_1.ApiError("Failed to fetch H5P content", 500);
}
return [2 /*return*/];
});
}); };
/**
* Get a specific H5P content by ID
*/
this.getContentById = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
var id, contentPath, h5pJsonPath, h5pJson;
return __generator(this, function (_a) {
try {
id = req.params.id;
contentPath = path_1.default.join(this.contentDirectory, id);
if (!fs_1.default.existsSync(contentPath)) {
throw new errorHandler_1.ApiError("H5P content not found", 404);
}
h5pJsonPath = path_1.default.join(contentPath, "h5p.json");
if (!fs_1.default.existsSync(h5pJsonPath)) {
throw new errorHandler_1.ApiError("Invalid H5P content", 400);
}
h5pJson = JSON.parse(fs_1.default.readFileSync(h5pJsonPath, "utf8"));
res.status(200).json(__assign({ id: id }, h5pJson));
}
catch (error) {
if (error instanceof errorHandler_1.ApiError) {
throw error;
}
console.error("Error fetching H5P content:", error);
throw new errorHandler_1.ApiError("Failed to fetch H5P content", 500);
}
return [2 /*return*/];
});
}); };
/**
* Serve H5P content files
*/
this.serveContentFile = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
var _a, id, filename, filePath, ext, contentType;
return __generator(this, function (_b) {
try {
_a = req.params, id = _a.id, filename = _a.filename;
filePath = path_1.default.join(this.contentDirectory, id, filename);
// Check if the file exists
if (!fs_1.default.existsSync(filePath)) {
throw new errorHandler_1.ApiError("File not found", 404);
}
ext = path_1.default.extname(filePath).toLowerCase();
contentType = "application/octet-stream";
switch (ext) {
case ".json":
contentType = "application/json";
break;
case ".js":
contentType = "application/javascript";
break;
case ".css":
contentType = "text/css";
break;
case ".html":
contentType = "text/html";
break;
case ".jpg":
case ".jpeg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".svg":
contentType = "image/svg+xml";
break;
}
// Set content type and send the file
res.setHeader("Content-Type", contentType);
fs_1.default.createReadStream(filePath).pipe(res);
}
catch (error) {
if (error instanceof errorHandler_1.ApiError) {
throw error;
}
console.error("Error serving H5P content file:", error);
throw new errorHandler_1.ApiError("Failed to serve H5P content file", 500);
}
return [2 /*return*/];
});
}); };
// Set the default H5P content directory or use the provided one
this.contentDirectory =
contentDirectory || path_1.default.join(process.cwd(), "public", "h5p", "content");
// Ensure the directory exists
this.ensureDirectoryExists();
}
/**
* Ensure the H5P content directory exists
*/
H5PContentController.prototype.ensureDirectoryExists = function () {
try {
if (!fs_1.default.existsSync(this.contentDirectory)) {
fs_1.default.mkdirSync(this.contentDirectory, { recursive: true });
}
}
catch (error) {
console.error("Failed to create H5P content directory:", error);
}
};
return H5PContentController;
}());
exports.H5PContentController = H5PContentController;