diff --git a/controllers/page.controller.js b/controllers/page.controller.js index 50e84f0d..160e91bd 100644 --- a/controllers/page.controller.js +++ b/controllers/page.controller.js @@ -1,7 +1,6 @@ import * as fs from "node:fs"; import { v4 as uuidv4 } from "uuid"; -import sharp from "sharp"; import sql from "../lib/sql.js"; import File from "../middleware/file.middleware.js"; @@ -10,20 +9,18 @@ import DateMiddleware from "../middleware/date.middleware.js"; import Element from "./element.controller.js"; import Body from "../middleware/body.middleware.js"; -export default class Page { +import checkOwner from "../middleware/check_owner.js"; +import Page from "../models/Page.model.js"; + +export default class PageController { static async create(c) { - // checkOwner.check(c); + checkOwner.checkProfile(c); - const page_id = uuidv4(); - const user_id = c.props.client.auth.user_id; + const page = new Page(); + page.create(); - sql("pages") - .insert({ - page_id, - date_creation: Date.now(), - date_lastModify: Date.now(), - }) - .run(); + const page_id = page.id; + const user_id = c.props.client.auth.user_id; sql("authors").update({ user_id, type: "owner" }).where({ page_id }).run(); DateMiddleware.update({ page_id }); @@ -31,55 +28,62 @@ export default class Page { return Response.redirect(`/page/${page_id}?mode=editor`); } - static async update(req) { - // checkOwner.check(c); - - const url = new URL(req.url); - const page_id = url.pathname.split("/").at(2); - - const referer = req.headers.get("referer"); - - const body = Body(await req.formData()); - - const { cover, title, desc, style, script, markup } = body; - const dir = `./public/data_uploads/pages/${page_id}/`; - if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); - - if (cover.size) { - const extention = cover.type.split("/").at(1); - const buffer = await cover.arrayBuffer(); - - await File.remove(dir, "cover"); - await File.write(cover, dir, `cover.${extention}`); + static async update(c) { + console.log(c); - const card = new Image(buffer); - await card.convert("webp288"); - - await File.write(card.buffer, dir, "cover@webp288.webp"); - } - - if (style.size) { - await File.remove(dir, "style"); - await File.write(style, dir, "style.css"); - } - - if (script.size) { - await File.remove(dir, "script"); - await File.write(script, dir, "script.js"); - } - - try { - sql("pages") - .update({ title, desc, markup, date_lastModify: Date.now() }) - .where({ page_id }) - .run(); - - DateMiddleware.update({ page_id }); - } catch (e) { - throw new Error("запись не удалась("); - } - - return Response.redirect(referer); + const page_id = c.url.pathname.split("/").at(2); + const page = new Page(page_id); + + if (c.props.client.auth) { + const authUserId = c.props.client.auth.user_id; + const permissions = page.getPermissions(authUserId); + if (permissions !== "owner") + throw new Error("not have permissions to modify this page"); + } else throw new Error("not authentificated"); + + // const body = Body(await req.formData()); + + page.writeFile("buffer"); + + // const { cover, title, desc, style, script, markup } = body; + // const dir = `./public/data_uploads/pages/${page_id}/`; + // if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); + // + // if (cover.size) { + // const extention = cover.type.split("/").at(1); + // const buffer = await cover.arrayBuffer(); + // + // await File.remove(dir, "cover"); + // await File.write(cover, dir, `cover.${extention}`); + // + // const card = new Image(buffer); + // await card.convert("webp288"); + // + // await File.write(card.buffer, dir, "cover@webp288.webp"); + // } + // + // if (style.size) { + // await File.remove(dir, "style"); + // await File.write(style, dir, "style.css"); + // } + // + // if (script.size) { + // await File.remove(dir, "script"); + // await File.write(script, dir, "script.js"); + // } + // + // try { + // sql("pages") + // .update({ title, desc, markup, date_lastModify: Date.now() }) + // .where({ page_id }) + // .run(); + // + // DateMiddleware.update({ page_id }); + // } catch (e) { + // throw new Error("запись не удалась("); + // } + // + return Response.redirect(c.referer); } static async delete(c) { diff --git a/middleware/check_owner.js b/middleware/check_owner.js index d63f3b60..c37de83b 100644 --- a/middleware/check_owner.js +++ b/middleware/check_owner.js @@ -13,10 +13,12 @@ export default class checkOwner { } else throw new Error("check owner error"); } - static async checkUser(c) { - const { cookie, params } = c; - if (cookie.auth.username === params.username) return; - else throw new Error("check owner error"); + static async checkProfile(c) { + if (c.props.client.auth) { + const authUsername = c.props.client.auth.username; + if (authUsername === c.url.pathname.split("/")[1]) return; + } + throw new Error("check owner error"); } static async checkPage(c) { diff --git a/models/Page.model.js b/models/Page.model.js new file mode 100644 index 00000000..a8aaa3d9 --- /dev/null +++ b/models/Page.model.js @@ -0,0 +1,59 @@ +import { v4 as uuidv4 } from "uuid"; + +import sql from "../lib/sql.js"; + +export default class Page { + constructor(pageId) { + if (!pageId) { + pageId = uuidv4(); + + this.date_creation = Date.now(); + this.date_lastModify = Date.now(); + } + + this.id = pageId; + } + + create() { + sql("pages") + .insert({ + page_id: this.id, + date_creation: this.date_creation, + date_lastModify: this.date_lastModify, + }) + .run(); + } + + writeCover(buffer) { + // writeFile(buffer, cover) + } + + getCover(name) { + // return buffer + // or return filePath + } + + removeCover(name) { + removeFile(name); + } + + getFile(name) { + // return buffer + // or return filePath + } + writeFile(buffer) { + console.log(buffer); + } + + removeFile(name) {} + + setPermissions(userId) {} + + getPermissions(authUserId) { + const permissions = sql("authors") + .select("type") + .where({ page_id: this.id, user_id: authUserId }) + .get(); + return permissions; + } +} diff --git a/src/router.js b/src/router.js index a0815294..a053c749 100644 --- a/src/router.js +++ b/src/router.js @@ -2,7 +2,7 @@ import Render from "../controllers/render.controller.js"; import Auth from "../controllers/auth.controller.js"; import User from "../controllers/user.controller.js"; import Profile from "../controllers/profile.controller.js"; -import Page from "../controllers/page.controller.js"; +import PageController from "../controllers/page.controller.js"; import Element from "../controllers/element.controller.js"; import Static from "../controllers/static.controller.js"; @@ -70,11 +70,12 @@ export default async function Router(req) { if (p1 === "page") { if (c.method === "PUT") { - return Page.update(req); + c.body = Body(await req.formData()); + return PageController.update(c); } else if (c.method === "POST") { return Element.create(c); } else if (c.method === "DELETE") { - return Page.delete(c); + return PageController.delete(c); } else return Render.page(c); } @@ -90,7 +91,7 @@ export default async function Router(req) { if (c.method === "PUT") { return await Profile.update(req); } else if (c.method === "POST") { - return Page.create(c); + return PageController.create(c); } else if (c.method === "DELETE") { return User.delete(c); } else return await Render.profile(c);