Skip to content

Commit

Permalink
Merge pull request #15 from effektivnayarabota1/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kirill-ivanovvv authored Nov 12, 2023
2 parents 5800567 + 713a0be commit f2cdb8b
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 108 deletions.
28 changes: 6 additions & 22 deletions controllers/element.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import sql from "../lib/sql.js";
import DateMiddleware from "../middleware/date.middleware.js";
import Body from "../middleware/body.middleware.js";
import File from "../middleware/file.middleware.js";
import Image from "../middleware/image.middleware.js";

export default class Element {
static create(c) {
Expand Down Expand Up @@ -43,32 +44,15 @@ export default class Element {

if (cover.size) {
const extention = cover.type.split("/").at(1);
const buf = await cover.arrayBuffer();
const buffer = await cover.arrayBuffer();

await File.remove(dir, "cover");
await File.write(cover, dir, `cover.${extention}`);

const metadata = await sharp(buf).metadata();
let { width, height } = metadata;
if (width > height) {
width = undefined;
height = 288;
} else if (width < height) {
width = 190;
height = undefined;
} else {
width = 190;
height = undefined;
}

const webp288 = await sharp(buf, { animated: true })
.webp({ quality: 100, smartSubsample: true })
.resize(width, height, {
withoutEnlargement: true,
fastShrinkOnLoad: true,
})
.toBuffer();
await File.write(webp288, dir, "cover@webp288.webp");
const card = new Image(buffer);
await card.convert("webp288");

await File.write(card.buffer, dir, "cover@webp288.webp");
}

if (style.size) {
Expand Down
30 changes: 6 additions & 24 deletions controllers/page.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sharp from "sharp";

import sql from "../lib/sql.js";
import File from "../middleware/file.middleware.js";
import Image from "../middleware/image.middleware.js";
import DateMiddleware from "../middleware/date.middleware.js";
import Element from "./element.controller.js";
import Body from "../middleware/body.middleware.js";
Expand Down Expand Up @@ -46,34 +47,15 @@ export default class Page {

if (cover.size) {
const extention = cover.type.split("/").at(1);
const buf = await cover.arrayBuffer();
const buffer = await cover.arrayBuffer();

await File.remove(dir, "cover");
await File.write(cover, dir, `cover.${extention}`);

const metadata = await sharp(buf).metadata();
let { width, height } = metadata;
if (width > height) {
width = undefined;
height = 288;
} else if (width < height) {
width = 190;
height = undefined;
} else {
width = 190;
height = undefined;
}

const webp288 = await sharp(buf, { animated: true })
.webp({ quality: 100, smartSubsample: true })
// .resize(288, 288, { fit: "outside", withoutEnlargement: true })
.resize(width, height, {
// fit: "outside",
withoutEnlargement: true,
fastShrinkOnLoad: true,
})
.toBuffer();
await File.write(webp288, dir, "cover@webp288.webp");
const card = new Image(buffer);
await card.convert("webp288");

await File.write(card.buffer, dir, "cover@webp288.webp");
}

if (style.size) {
Expand Down
12 changes: 6 additions & 6 deletions controllers/profile.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sharp from "sharp";
import Body from "../middleware/body.middleware.js";
import DateMiddleware from "../middleware/date.middleware.js";
import File from "../middleware/file.middleware.js";
import Image from "../middleware/image.middleware.js";
import sql from "../lib/sql.js";

export default class Profile {
Expand All @@ -23,16 +24,15 @@ export default class Profile {

if (avatar.size) {
const extention = avatar.type.split("/").at(1);
const buf = await avatar.arrayBuffer();
const buffer = await avatar.arrayBuffer();

await File.remove(dir, "avatar");
await File.write(avatar, dir, `avatar.${extention}`);

const webp64 = await sharp(buf, { animated: true })
.webp({ quality: 100, smartSubsample: true })
.resize(64, 64, { fit: "cover", withoutEnlargement: true })
.toBuffer();
await File.write(webp64, dir, "avatar@webp64.webp");
const avatar64 = new Image(buffer);
await avatar64.convert("webp64");

await File.write(avatar64.buffer, dir, "avatar@webp64.webp");
}

if (style.size) {
Expand Down
4 changes: 3 additions & 1 deletion middleware/data.middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { marked } from "marked";
import DOMPurify from "isomorphic-dompurify";

import Page from "../controllers/page.controller";
import File from "./file.middleware";
import DateMiddleware from "./date.middleware";
import sql from "../lib/sql";
Expand Down Expand Up @@ -107,7 +108,8 @@ export default class Data {
.select("username")
.where({ user_id: author.user_id })
.get();
data.authors.push(author);
if (author) data.authors.push(author);
else Page.deleteSingle(page_id);
});

const elements_query = await sql().custom_all(
Expand Down
9 changes: 9 additions & 0 deletions middleware/file.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export default class File {
}
}

static async removeVariant(path_file) {
const filename = path_file.split("/").at(-1);
if (filename.split("@").length > 1) {
fs.rmSync(path_file);
return true;
}
return false;
}

static async write_image() {}

static get_src() {
Expand Down
50 changes: 50 additions & 0 deletions middleware/image.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sharp from "sharp";

export default class Image {
constructor(buffer, format = "original") {
this.buffer = buffer;
}

async convert(format) {
switch (format) {
case "webp64":
await this.webp64();
break;
case "webp288":
await this.webp288();
break;
default:
throw new Error("need know format to convert");
}
}

async webp64() {
this.buffer = await sharp(this.buffer, { animated: true })
.webp({ quality: 100, smartSubsample: true })
.resize(64, 64, { fit: "cover", withoutEnlargement: true })
.toBuffer();
}

async webp288() {
const metadata = await sharp(this.buffer).metadata();
let { width, height } = metadata;
if (width > height) {
width = undefined;
height = 288;
} else if (width < height) {
width = 190;
height = undefined;
} else {
width = 288;
height = undefined;
}

this.buffer = await sharp(this.buffer, { animated: true })
.webp({ quality: 100, smartSubsample: true })
.resize(width, height, {
withoutEnlargement: true,
fastShrinkOnLoad: true,
})
.toBuffer();
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"uuid": "^9.0.1"
},
"scripts": {
"dev": "bun run --watch src/index.js"
"dev": "bun run --watch src/index.js",
"format": "bun run src/format.js",
"clean": "bun run src/clean.js"
}
}
8 changes: 2 additions & 6 deletions public/style/font.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ p {
font-style: normal;
font-weight: 400;
line-height: 2.4rem;
margin-bottom: 1.2rem;
margin-bottom: 0.8rem;
}
h1 {
margin-bottom: 4.8rem;
Expand All @@ -34,7 +34,7 @@ h2 {
letter-spacing: -0.056rem;
}
h3 {
margin-bottom: 1rem;
margin-bottom: 1.2rem;

font-size: 1.8em;
font-style: normal;
Expand Down Expand Up @@ -67,10 +67,6 @@ a:hover {
text-decoration: underline;
}

/* HTML */
.html > p:not(:last-child) {
margin-bottom: 2.4rem;
}
@media screen and (max-width: 444px) {
:root {
/* font-size: 80%; */
Expand Down
13 changes: 13 additions & 0 deletions src/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Page from "../controllers/page.controller";
import Element from "../controllers/element.controller";

import sql from "../lib/sql";
const authors = sql("authors").select(["user_id", "page_id"]).all();
authors.forEach((author) => {
if (!author.user_id) Page.deleteSingle(author.page_id);
});

const connections = sql("connections").select(["page_id", "element_id"]).all();
connections.forEach((connection) => {
if (!connection.page_id) Element.deleteSingle(connection.element_id);
});
47 changes: 47 additions & 0 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as fs from "node:fs";

import File from "../middleware/file.middleware";
import Image from "../middleware/image.middleware";

const uploads = `./public/data_uploads/`;

const getDirectories = async (source) =>
(await fs.promises.readdir(source, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

const convert = async (path, name, format) => {
fs.readFile(path, async (error, buffer) => {
if (error) throw new Error(error.message);

const path_dir = path.split("/").slice(0, -1).join("/") + "/";

const image = new Image(buffer);
await image.convert(format);

await File.write(image.buffer, path_dir, `${name}@${format}.webp`);
});
};

const formatProject = async (root, name, format) => {
if (fs.existsSync(root)) {
const dirs = await getDirectories(root);
dirs.forEach(async (dir) => {
const path_dir = root + dir + "/";
const files = await fs.promises.readdir(path_dir, {
withFileTypes: true,
});

files.forEach(async (file) => {
const path_file = path_dir + file.name;
if (!(await File.removeVariant(path_file))) {
await convert(path_file, name, format);
}
});
});
}
};

await formatProject(uploads + "users/", "avatar", "webp64");
await formatProject(uploads + "pages/", "cover", "webp288");
await formatProject(uploads + "elements/", "cover", "webp288");
6 changes: 0 additions & 6 deletions templates/Index.eta
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<% } %>
</div>

<div class="grid">
<hr>
</div>

<div class="grid">
<h5 style="grid-column: 1/-1;">Случайные страницы:</h5>
Expand All @@ -31,9 +28,6 @@
<% } %>
</div>

<div class="grid">
<hr>
</div>

<div class="grid">
<h5 style="grid-column: 1/-1;">Случайные элементы:</h5>
Expand Down
Loading

0 comments on commit f2cdb8b

Please sign in to comment.