Skip to content

Commit

Permalink
Merge pull request #18 from effektivnayarabota1/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kirill-ivanovvv authored Jan 4, 2024
2 parents 40d9f96 + fc3724d commit c511e52
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 68 deletions.
124 changes: 64 additions & 60 deletions controllers/page.controller.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,76 +9,81 @@ 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 });

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, "[email protected]");
}

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, "[email protected]");
// }
//
// 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) {
Expand Down
10 changes: 6 additions & 4 deletions middleware/check_owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions models/Page.model.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 5 additions & 4 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down

0 comments on commit c511e52

Please sign in to comment.