mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
restructuring file storage service
This commit is contained in:
@@ -24,7 +24,7 @@ export default async function RootLayout({
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head></head>
|
<head></head>
|
||||||
<body className="flex justify-center">
|
<body className="flex justify-center">
|
||||||
<div className="bg-slate-900 h-screen p-1 text-slate-300 w-full">
|
<div className="bg-slate-900 h-screen text-slate-300 w-full p-1">
|
||||||
<MyToaster />
|
<MyToaster />
|
||||||
<Suspense>
|
<Suspense>
|
||||||
<Providers>
|
<Providers>
|
||||||
|
|||||||
@@ -155,6 +155,7 @@ function OtherSettings({
|
|||||||
label={"Storage Folder"}
|
label={"Storage Folder"}
|
||||||
options={emptyDirectories}
|
options={emptyDirectories}
|
||||||
getOptionName={(d) => d}
|
getOptionName={(d) => d}
|
||||||
|
emptyOptionText="--- add a new folder to your docker compose to add more folders ---"
|
||||||
/>
|
/>
|
||||||
<div>
|
<div>
|
||||||
New folders will not be created automatically, you are expected to mount
|
New folders will not be created automatically, you are expected to mount
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import AddNewCourse from "./newCourse/AddNewCourse";
|
|||||||
|
|
||||||
export default async function Home() {
|
export default async function Home() {
|
||||||
return (
|
return (
|
||||||
<main className="min-h-screen flex justify-center">
|
<main className="min-h-0 flex justify-center">
|
||||||
<div>
|
<div>
|
||||||
<CourseList />
|
<CourseList />
|
||||||
<br />
|
<br />
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ export default function SelectInput<T>({
|
|||||||
label,
|
label,
|
||||||
options,
|
options,
|
||||||
getOptionName,
|
getOptionName,
|
||||||
|
emptyOptionText,
|
||||||
}: {
|
}: {
|
||||||
value: T | undefined;
|
value: T | undefined;
|
||||||
setValue: (newValue: T | undefined) => void;
|
setValue: (newValue: T | undefined) => void;
|
||||||
label: string;
|
label: string;
|
||||||
options: T[];
|
options: T[];
|
||||||
getOptionName: (item: T) => string;
|
getOptionName: (item: T) => string;
|
||||||
|
emptyOptionText?: string;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<label className="block">
|
<label className="block">
|
||||||
@@ -25,6 +27,7 @@ export default function SelectInput<T>({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<option></option>
|
<option></option>
|
||||||
|
{emptyOptionText && <option>{emptyOptionText}</option>}
|
||||||
{options.map((o) => (
|
{options.map((o) => (
|
||||||
<option key={getOptionName(o)}>{getOptionName(o)}</option>
|
<option key={getOptionName(o)}>{getOptionName(o)}</option>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -17,13 +17,14 @@ export const useCanvasTermsQuery = (queryDate: Date) => {
|
|||||||
return useSuspenseQuery({
|
return useSuspenseQuery({
|
||||||
queryKey: canvasKeys.allAroundDate(queryDate),
|
queryKey: canvasKeys.allAroundDate(queryDate),
|
||||||
queryFn: () => {
|
queryFn: () => {
|
||||||
const currentTerms = terms
|
const finiteTerms = terms.filter((t) => {
|
||||||
.filter((t) => {
|
|
||||||
if (!t.end_at) return false;
|
if (!t.end_at) return false;
|
||||||
|
|
||||||
const endDate = new Date(t.end_at);
|
const endDate = new Date(t.end_at);
|
||||||
return endDate > queryDate;
|
return endDate > queryDate;
|
||||||
})
|
});
|
||||||
|
console.log("finite terms", finiteTerms, terms);
|
||||||
|
const currentTerms = finiteTerms
|
||||||
.sort(
|
.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
new Date(a.start_at ?? "").getTime() -
|
new Date(a.start_at ?? "").getTime() -
|
||||||
|
|||||||
@@ -1,24 +1,25 @@
|
|||||||
import { CanvasEnrollmentTermModel } from "@/models/canvas/enrollmentTerms/canvasEnrollmentTermModel";
|
import { CanvasEnrollmentTermModel } from "@/models/canvas/enrollmentTerms/canvasEnrollmentTermModel";
|
||||||
import { canvasApi } from "./canvasServiceUtils";
|
import { canvasApi, paginatedRequest } from "./canvasServiceUtils";
|
||||||
import { CanvasCourseModel } from "@/models/canvas/courses/canvasCourseModel";
|
import { CanvasCourseModel } from "@/models/canvas/courses/canvasCourseModel";
|
||||||
import { CanvasEnrollmentModel } from "@/models/canvas/enrollments/canvasEnrollmentModel";
|
import { CanvasEnrollmentModel } from "@/models/canvas/enrollments/canvasEnrollmentModel";
|
||||||
import { axiosClient } from "../axiosUtils";
|
import { axiosClient } from "../axiosUtils";
|
||||||
|
|
||||||
const getAllTerms = async () => {
|
const getAllTerms = async () => {
|
||||||
const url = `${canvasApi}/accounts/10/terms`;
|
const url = `${canvasApi}/accounts/10/terms?per_page=100`;
|
||||||
const { data } = await axiosClient.get<{
|
const data = await paginatedRequest<
|
||||||
|
{
|
||||||
enrollment_terms: CanvasEnrollmentTermModel[];
|
enrollment_terms: CanvasEnrollmentTermModel[];
|
||||||
}>(url);
|
}[]
|
||||||
const terms = data.enrollment_terms;
|
>({ url });
|
||||||
|
const terms = data.flatMap((t) => t.enrollment_terms);
|
||||||
return terms;
|
return terms;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const canvasService = {
|
export const canvasService = {
|
||||||
getAllTerms,
|
getAllTerms,
|
||||||
async getCourses(termId: number) {
|
async getCourses(termId: number) {
|
||||||
const url = `${canvasApi}/courses`;
|
const url = `${canvasApi}/courses?per_page=100`;
|
||||||
const response = await axiosClient.get<CanvasCourseModel[]>(url);
|
const allCourses = await paginatedRequest<CanvasCourseModel[]>({ url });
|
||||||
const allCourses = response.data;
|
|
||||||
const coursesInTerm = allCourses
|
const coursesInTerm = allCourses
|
||||||
.flatMap((l) => l)
|
.flatMap((l) => l)
|
||||||
.filter((c) => c.enrollment_term_id === termId);
|
.filter((c) => c.enrollment_term_id === termId);
|
||||||
|
|||||||
@@ -44,19 +44,14 @@ export async function paginatedRequest<T extends any[]>(request: {
|
|||||||
url.toString()
|
url.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
// if (!Array.isArray(firstData)) {
|
var returnData = Array.isArray(firstData) ? [...firstData] : [firstData]; // terms come across as nested objects {enrolmentTerms: terms[]}
|
||||||
// return firstData;
|
|
||||||
// }
|
|
||||||
|
|
||||||
var returnData = [...firstData];
|
|
||||||
var nextUrl = getNextUrl(firstHeaders);
|
var nextUrl = getNextUrl(firstHeaders);
|
||||||
// console.log("got first request", nextUrl, firstHeaders);
|
|
||||||
|
|
||||||
while (nextUrl) {
|
while (nextUrl) {
|
||||||
requestCount += 1;
|
requestCount += 1;
|
||||||
const { data, headers } = await axiosClient.get<T>(nextUrl);
|
const { data, headers } = await axiosClient.get<T>(nextUrl);
|
||||||
if (data) {
|
if (data) {
|
||||||
returnData = returnData.concat(data);
|
returnData = returnData.concat(Array.isArray(data) ? [...data] : [data]);
|
||||||
}
|
}
|
||||||
nextUrl = getNextUrl(headers);
|
nextUrl = getNextUrl(headers);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,230 +1,19 @@
|
|||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import {
|
|
||||||
LocalCourseSettings,
|
|
||||||
localCourseYamlUtils,
|
|
||||||
} from "@/models/local/localCourse";
|
|
||||||
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
||||||
import {
|
|
||||||
LocalQuiz,
|
|
||||||
localQuizMarkdownUtils,
|
|
||||||
} from "@/models/local/quiz/localQuiz";
|
|
||||||
import {
|
|
||||||
LocalCoursePage,
|
|
||||||
localPageMarkdownUtils,
|
|
||||||
} from "@/models/local/page/localCoursePage";
|
|
||||||
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
|
|
||||||
import { assignmentsFileStorageService } from "./assignmentsFileStorageService";
|
import { assignmentsFileStorageService } from "./assignmentsFileStorageService";
|
||||||
|
import { quizFileStorageService } from "./quizFileStorageService";
|
||||||
const quizzes = {
|
import { pageFileStorageService } from "./pageFileStorageService";
|
||||||
async getQuizNames(courseName: string, moduleName: string) {
|
import { moduleFileStorageService } from "./moduleFileStorageService";
|
||||||
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
|
import { settingsFileStorageService } from "./settingsFileStorageService";
|
||||||
if (!(await directoryOrFileExists(filePath))) {
|
|
||||||
console.log(
|
|
||||||
`Error loading course by name, quiz folder does not exist in ${filePath}`
|
|
||||||
);
|
|
||||||
await fs.mkdir(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
const files = await fs.readdir(filePath);
|
|
||||||
return files.map((f) => f.replace(/\.md$/, ""));
|
|
||||||
},
|
|
||||||
async getQuiz(courseName: string, moduleName: string, quizName: string) {
|
|
||||||
const filePath = path.join(
|
|
||||||
basePath,
|
|
||||||
courseName,
|
|
||||||
moduleName,
|
|
||||||
"quizzes",
|
|
||||||
quizName + ".md"
|
|
||||||
);
|
|
||||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
|
||||||
/\r\n/g,
|
|
||||||
"\n"
|
|
||||||
);
|
|
||||||
return localQuizMarkdownUtils.parseMarkdown(rawFile);
|
|
||||||
},
|
|
||||||
|
|
||||||
async updateQuiz(
|
|
||||||
courseName: string,
|
|
||||||
moduleName: string,
|
|
||||||
quizName: string,
|
|
||||||
quiz: LocalQuiz
|
|
||||||
) {
|
|
||||||
const folder = path.join(basePath, courseName, moduleName, "quizzes");
|
|
||||||
await fs.mkdir(folder, { recursive: true });
|
|
||||||
const filePath = path.join(
|
|
||||||
basePath,
|
|
||||||
courseName,
|
|
||||||
moduleName,
|
|
||||||
"quizzes",
|
|
||||||
quizName + ".md"
|
|
||||||
);
|
|
||||||
|
|
||||||
const quizMarkdown = quizMarkdownUtils.toMarkdown(quiz);
|
|
||||||
console.log(`Saving quiz ${filePath}`);
|
|
||||||
await fs.writeFile(filePath, quizMarkdown);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const pages = {
|
|
||||||
async getPageNames(courseName: string, moduleName: string) {
|
|
||||||
const filePath = path.join(basePath, courseName, moduleName, "pages");
|
|
||||||
if (!(await directoryOrFileExists(filePath))) {
|
|
||||||
console.log(
|
|
||||||
`Error loading course by name, pages folder does not exist in ${filePath}`
|
|
||||||
);
|
|
||||||
await fs.mkdir(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
const files = await fs.readdir(filePath);
|
|
||||||
return files.map((f) => f.replace(/\.md$/, ""));
|
|
||||||
},
|
|
||||||
|
|
||||||
async getPage(courseName: string, moduleName: string, pageName: string) {
|
|
||||||
const filePath = path.join(
|
|
||||||
basePath,
|
|
||||||
courseName,
|
|
||||||
moduleName,
|
|
||||||
"pages",
|
|
||||||
pageName + ".md"
|
|
||||||
);
|
|
||||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
|
||||||
/\r\n/g,
|
|
||||||
"\n"
|
|
||||||
);
|
|
||||||
return localPageMarkdownUtils.parseMarkdown(rawFile);
|
|
||||||
},
|
|
||||||
async updatePage(
|
|
||||||
courseName: string,
|
|
||||||
moduleName: string,
|
|
||||||
pageName: string,
|
|
||||||
page: LocalCoursePage
|
|
||||||
) {
|
|
||||||
const folder = path.join(basePath, courseName, moduleName, "pages");
|
|
||||||
await fs.mkdir(folder, { recursive: true });
|
|
||||||
|
|
||||||
const filePath = path.join(
|
|
||||||
basePath,
|
|
||||||
courseName,
|
|
||||||
moduleName,
|
|
||||||
"pages",
|
|
||||||
page.name + ".md"
|
|
||||||
);
|
|
||||||
|
|
||||||
const pageMarkdown = localPageMarkdownUtils.toMarkdown(page);
|
|
||||||
console.log(`Saving page ${filePath}`);
|
|
||||||
await fs.writeFile(filePath, pageMarkdown);
|
|
||||||
|
|
||||||
const pageNameIsChanged = pageName !== page.name;
|
|
||||||
if (pageNameIsChanged) {
|
|
||||||
console.log("removing old page after name change " + pageName);
|
|
||||||
const oldFilePath = path.join(
|
|
||||||
basePath,
|
|
||||||
courseName,
|
|
||||||
moduleName,
|
|
||||||
"pages",
|
|
||||||
pageName + ".md"
|
|
||||||
);
|
|
||||||
await fs.unlink(oldFilePath);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const modules = {
|
|
||||||
async getModuleNames(courseName: string) {
|
|
||||||
const courseDirectory = path.join(basePath, courseName);
|
|
||||||
const moduleDirectories = await fs.readdir(courseDirectory, {
|
|
||||||
withFileTypes: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const modulePromises = moduleDirectories
|
|
||||||
.filter((dirent) => dirent.isDirectory())
|
|
||||||
.map((dirent) => dirent.name);
|
|
||||||
|
|
||||||
const modules = await Promise.all(modulePromises);
|
|
||||||
return modules.sort((a, b) => a.localeCompare(b));
|
|
||||||
},
|
|
||||||
async createModule(courseName: string, moduleName: string) {
|
|
||||||
const courseDirectory = path.join(basePath, courseName);
|
|
||||||
|
|
||||||
await fs.mkdir(courseDirectory + "/" + moduleName, { recursive: true });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const settings = {
|
|
||||||
async getAllCoursesSettings() {
|
|
||||||
const courses = await fileStorageService.getCourseNames();
|
|
||||||
|
|
||||||
const courseSettings = await Promise.all(
|
|
||||||
courses.map(
|
|
||||||
async (c) => await fileStorageService.settings.getCourseSettings(c)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return courseSettings;
|
|
||||||
},
|
|
||||||
async getCourseSettings(courseName: string): Promise<LocalCourseSettings> {
|
|
||||||
const courseDirectory = path.join(basePath, courseName);
|
|
||||||
const settingsPath = path.join(courseDirectory, "settings.yml");
|
|
||||||
if (!(await directoryOrFileExists(settingsPath))) {
|
|
||||||
const errorMessage = `Error loading settings for ${courseName}, settings file ${settingsPath}`;
|
|
||||||
console.log(errorMessage);
|
|
||||||
throw new Error(errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
const settingsString = await fs.readFile(settingsPath, "utf-8");
|
|
||||||
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
|
|
||||||
|
|
||||||
const folderName = path.basename(courseDirectory);
|
|
||||||
return { ...settings, name: folderName };
|
|
||||||
},
|
|
||||||
|
|
||||||
async updateCourseSettings(
|
|
||||||
courseName: string,
|
|
||||||
settings: LocalCourseSettings
|
|
||||||
) {
|
|
||||||
const courseDirectory = path.join(basePath, courseName);
|
|
||||||
const settingsPath = path.join(courseDirectory, "settings.yml");
|
|
||||||
|
|
||||||
const { name, ...settingsWithoutName } = settings;
|
|
||||||
const settingsMarkdown =
|
|
||||||
localCourseYamlUtils.settingsToYaml(settingsWithoutName);
|
|
||||||
|
|
||||||
console.log(`Saving settings ${settingsPath}`);
|
|
||||||
await fs.writeFile(settingsPath, settingsMarkdown);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const fileStorageService = {
|
export const fileStorageService = {
|
||||||
async getCourseNames() {
|
settings: settingsFileStorageService,
|
||||||
console.log("loading course ids");
|
modules: moduleFileStorageService,
|
||||||
const courseDirectories = await fs.readdir(basePath, {
|
|
||||||
withFileTypes: true,
|
|
||||||
});
|
|
||||||
const coursePromises = await Promise.all(
|
|
||||||
courseDirectories
|
|
||||||
.filter((dirent) => dirent.isDirectory())
|
|
||||||
.map(async (dirent) => {
|
|
||||||
const coursePath = path.join(basePath, dirent.name);
|
|
||||||
const settingsPath = path.join(coursePath, "settings.yml");
|
|
||||||
const hasSettings = await directoryOrFileExists(settingsPath);
|
|
||||||
return {
|
|
||||||
dirent,
|
|
||||||
hasSettings,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
const courseNamesFromDirectories = coursePromises
|
|
||||||
.filter(({ hasSettings }) => hasSettings)
|
|
||||||
.map(({ dirent }) => dirent.name);
|
|
||||||
|
|
||||||
return courseNamesFromDirectories;
|
|
||||||
},
|
|
||||||
settings,
|
|
||||||
modules,
|
|
||||||
assignments: assignmentsFileStorageService,
|
assignments: assignmentsFileStorageService,
|
||||||
quizzes,
|
quizzes: quizFileStorageService,
|
||||||
pages,
|
pages: pageFileStorageService,
|
||||||
|
|
||||||
|
|
||||||
async getEmptyDirectories(): Promise<string[]> {
|
async getEmptyDirectories(): Promise<string[]> {
|
||||||
if (!(await directoryOrFileExists(basePath))) {
|
if (!(await directoryOrFileExists(basePath))) {
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
import { basePath } from "./utils/fileSystemUtils";
|
||||||
|
|
||||||
|
export const moduleFileStorageService = {
|
||||||
|
async getModuleNames(courseName: string) {
|
||||||
|
const courseDirectory = path.join(basePath, courseName);
|
||||||
|
const moduleDirectories = await fs.readdir(courseDirectory, {
|
||||||
|
withFileTypes: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const modulePromises = moduleDirectories
|
||||||
|
.filter((dirent) => dirent.isDirectory())
|
||||||
|
.map((dirent) => dirent.name);
|
||||||
|
|
||||||
|
const modules = await Promise.all(modulePromises);
|
||||||
|
return modules.sort((a, b) => a.localeCompare(b));
|
||||||
|
},
|
||||||
|
async createModule(courseName: string, moduleName: string) {
|
||||||
|
const courseDirectory = path.join(basePath, courseName);
|
||||||
|
|
||||||
|
await fs.mkdir(courseDirectory + "/" + moduleName, { recursive: true });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { localPageMarkdownUtils, LocalCoursePage } from "@/models/local/page/localCoursePage";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
||||||
|
|
||||||
|
export const pageFileStorageService = {
|
||||||
|
async getPageNames(courseName: string, moduleName: string) {
|
||||||
|
const filePath = path.join(basePath, courseName, moduleName, "pages");
|
||||||
|
if (!(await directoryOrFileExists(filePath))) {
|
||||||
|
console.log(
|
||||||
|
`Error loading course by name, pages folder does not exist in ${filePath}`
|
||||||
|
);
|
||||||
|
await fs.mkdir(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const files = await fs.readdir(filePath);
|
||||||
|
return files.map((f) => f.replace(/\.md$/, ""));
|
||||||
|
},
|
||||||
|
|
||||||
|
async getPage(courseName: string, moduleName: string, pageName: string) {
|
||||||
|
const filePath = path.join(
|
||||||
|
basePath,
|
||||||
|
courseName,
|
||||||
|
moduleName,
|
||||||
|
"pages",
|
||||||
|
pageName + ".md"
|
||||||
|
);
|
||||||
|
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||||
|
/\r\n/g,
|
||||||
|
"\n"
|
||||||
|
);
|
||||||
|
return localPageMarkdownUtils.parseMarkdown(rawFile);
|
||||||
|
},
|
||||||
|
async updatePage(
|
||||||
|
courseName: string,
|
||||||
|
moduleName: string,
|
||||||
|
pageName: string,
|
||||||
|
page: LocalCoursePage
|
||||||
|
) {
|
||||||
|
const folder = path.join(basePath, courseName, moduleName, "pages");
|
||||||
|
await fs.mkdir(folder, { recursive: true });
|
||||||
|
|
||||||
|
const filePath = path.join(
|
||||||
|
basePath,
|
||||||
|
courseName,
|
||||||
|
moduleName,
|
||||||
|
"pages",
|
||||||
|
page.name + ".md"
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageMarkdown = localPageMarkdownUtils.toMarkdown(page);
|
||||||
|
console.log(`Saving page ${filePath}`);
|
||||||
|
await fs.writeFile(filePath, pageMarkdown);
|
||||||
|
|
||||||
|
const pageNameIsChanged = pageName !== page.name;
|
||||||
|
if (pageNameIsChanged) {
|
||||||
|
console.log("removing old page after name change " + pageName);
|
||||||
|
const oldFilePath = path.join(
|
||||||
|
basePath,
|
||||||
|
courseName,
|
||||||
|
moduleName,
|
||||||
|
"pages",
|
||||||
|
pageName + ".md"
|
||||||
|
);
|
||||||
|
await fs.unlink(oldFilePath);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import {
|
||||||
|
localQuizMarkdownUtils,
|
||||||
|
LocalQuiz,
|
||||||
|
} from "@/models/local/quiz/localQuiz";
|
||||||
|
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
|
||||||
|
import path from "path";
|
||||||
|
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
|
||||||
|
export const quizFileStorageService = {
|
||||||
|
async getQuizNames(courseName: string, moduleName: string) {
|
||||||
|
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
|
||||||
|
if (!(await directoryOrFileExists(filePath))) {
|
||||||
|
console.log(
|
||||||
|
`Error loading course by name, quiz folder does not exist in ${filePath}`
|
||||||
|
);
|
||||||
|
await fs.mkdir(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const files = await fs.readdir(filePath);
|
||||||
|
return files.map((f) => f.replace(/\.md$/, ""));
|
||||||
|
},
|
||||||
|
async getQuiz(courseName: string, moduleName: string, quizName: string) {
|
||||||
|
const filePath = path.join(
|
||||||
|
basePath,
|
||||||
|
courseName,
|
||||||
|
moduleName,
|
||||||
|
"quizzes",
|
||||||
|
quizName + ".md"
|
||||||
|
);
|
||||||
|
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||||
|
/\r\n/g,
|
||||||
|
"\n"
|
||||||
|
);
|
||||||
|
return localQuizMarkdownUtils.parseMarkdown(rawFile);
|
||||||
|
},
|
||||||
|
|
||||||
|
async updateQuiz(
|
||||||
|
courseName: string,
|
||||||
|
moduleName: string,
|
||||||
|
quizName: string,
|
||||||
|
quiz: LocalQuiz
|
||||||
|
) {
|
||||||
|
const folder = path.join(basePath, courseName, moduleName, "quizzes");
|
||||||
|
await fs.mkdir(folder, { recursive: true });
|
||||||
|
const filePath = path.join(
|
||||||
|
basePath,
|
||||||
|
courseName,
|
||||||
|
moduleName,
|
||||||
|
"quizzes",
|
||||||
|
quizName + ".md"
|
||||||
|
);
|
||||||
|
|
||||||
|
const quizMarkdown = quizMarkdownUtils.toMarkdown(quiz);
|
||||||
|
console.log(`Saving quiz ${filePath}`);
|
||||||
|
await fs.writeFile(filePath, quizMarkdown);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import {
|
||||||
|
LocalCourseSettings,
|
||||||
|
localCourseYamlUtils,
|
||||||
|
} from "@/models/local/localCourse";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
import {
|
||||||
|
basePath,
|
||||||
|
directoryOrFileExists,
|
||||||
|
getCourseNames,
|
||||||
|
} from "./utils/fileSystemUtils";
|
||||||
|
|
||||||
|
const getCourseSettings = async (
|
||||||
|
courseName: string
|
||||||
|
): Promise<LocalCourseSettings> => {
|
||||||
|
const courseDirectory = path.join(basePath, courseName);
|
||||||
|
const settingsPath = path.join(courseDirectory, "settings.yml");
|
||||||
|
if (!(await directoryOrFileExists(settingsPath))) {
|
||||||
|
const errorMessage = `Error loading settings for ${courseName}, settings file ${settingsPath}`;
|
||||||
|
console.log(errorMessage);
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
const settingsString = await fs.readFile(settingsPath, "utf-8");
|
||||||
|
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
|
||||||
|
|
||||||
|
const folderName = path.basename(courseDirectory);
|
||||||
|
return { ...settings, name: folderName };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const settingsFileStorageService = {
|
||||||
|
getCourseSettings,
|
||||||
|
async getAllCoursesSettings() {
|
||||||
|
const courses = await getCourseNames();
|
||||||
|
|
||||||
|
const courseSettings = await Promise.all(
|
||||||
|
courses.map(async (c) => await getCourseSettings(c))
|
||||||
|
);
|
||||||
|
return courseSettings;
|
||||||
|
},
|
||||||
|
|
||||||
|
async updateCourseSettings(
|
||||||
|
courseName: string,
|
||||||
|
settings: LocalCourseSettings
|
||||||
|
) {
|
||||||
|
const courseDirectory = path.join(basePath, courseName);
|
||||||
|
const settingsPath = path.join(courseDirectory, "settings.yml");
|
||||||
|
|
||||||
|
const { name, ...settingsWithoutName } = settings;
|
||||||
|
const settingsMarkdown =
|
||||||
|
localCourseYamlUtils.settingsToYaml(settingsWithoutName);
|
||||||
|
|
||||||
|
console.log(`Saving settings ${settingsPath}`);
|
||||||
|
await fs.writeFile(settingsPath, settingsMarkdown);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export const hasFileSystemEntries = async (
|
export const hasFileSystemEntries = async (
|
||||||
directoryPath: string
|
directoryPath: string
|
||||||
@@ -19,6 +20,32 @@ export const directoryOrFileExists = async (directoryPath: string): Promise<bool
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function getCourseNames() {
|
||||||
|
console.log("loading course ids");
|
||||||
|
const courseDirectories = await fs.readdir(basePath, {
|
||||||
|
withFileTypes: true,
|
||||||
|
});
|
||||||
|
const coursePromises = await Promise.all(
|
||||||
|
courseDirectories
|
||||||
|
.filter((dirent) => dirent.isDirectory())
|
||||||
|
.map(async (dirent) => {
|
||||||
|
const coursePath = path.join(basePath, dirent.name);
|
||||||
|
const settingsPath = path.join(coursePath, "settings.yml");
|
||||||
|
const hasSettings = await directoryOrFileExists(settingsPath);
|
||||||
|
return {
|
||||||
|
dirent,
|
||||||
|
hasSettings,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const courseNamesFromDirectories = coursePromises
|
||||||
|
.filter(({ hasSettings }) => hasSettings)
|
||||||
|
.map(({ dirent }) => dirent.name);
|
||||||
|
|
||||||
|
return courseNamesFromDirectories;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
|
export const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
|
||||||
|
|||||||
Reference in New Issue
Block a user