-
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 #15 from effektivnayarabota1/develop
Develop
- Loading branch information
Showing
14 changed files
with
180 additions
and
108 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
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
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
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,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(); | ||
} | ||
} |
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
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,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); | ||
}); |
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,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"); |
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
Oops, something went wrong.