-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from effektivnayarabota1/develop
Develop
- Loading branch information
Showing
4 changed files
with
134 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters