Compare commits

...

2 Commits

Author SHA1 Message Date
Maksim Karasev 50e0d80448 Bump version to 1.2.0 2023-05-17 18:55:06 +03:00
Maksim Karasev 9f00709893 Switch to async io 2023-05-17 18:53:26 +03:00
4 changed files with 12 additions and 10 deletions

View File

@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "personal-gallery",
"version": "1.1.3",
"version": "1.2.0",
"description": "",
"scripts": {
"server-dev": "yarn workspace server dev",

View File

@ -1,6 +1,6 @@
{
"name": "server",
"version": "1.1.3",
"version": "1.2.0",
"description": "",
"main": "index.js",
"scripts": {

View File

@ -1,4 +1,4 @@
import fs from 'fs';
import { Stats, promises as fs } from "fs";
import path from 'path';
import fileType from 'file-type';
import sanitize from "sanitize-filename";
@ -17,16 +17,16 @@ const nanoid = customAlphabet(alphabet, 7);
*/
export const registerFileInFolder = async () => {
try {
const files = fs.readdirSync(IMAGE_DIR);
const files = await fs.readdir(IMAGE_DIR);
const filesWithStats = await Promise.all(
files.map((filename) => fs.promises
files.map((filename) => fs
.stat(path.join(IMAGE_DIR, filename))
.then((stat) => ({ filename, stat }))),
);
const filteredFilesWithStats:{
filename: string;
stat: fs.Stats;
stat: Stats;
}[] = [];
const filePromises = filesWithStats.map(async (file) => {
@ -68,7 +68,7 @@ export const addImage = async (
const newFileName = `${await nanoid()}.${extension}`;
const newFilePath = path.join(IMAGE_DIR, newFileName);
logger.verbose(`Saving buffer into ${newFilePath}`);
fs.writeFileSync(newFilePath, fileData);
await fs.writeFile(newFilePath, fileData);
insertImageIntoDB(newFileName);
return newFileName;
};
@ -99,7 +99,7 @@ export const getImages = async (
export const getImage = async (filename: string): Promise<Image> => {
try {
logger.verbose(`Looking for file ${filename}...`);
const file = fs.readFileSync(path.join(IMAGE_DIR, sanitize(filename)));
const file = await fs.readFile(path.join(IMAGE_DIR, sanitize(filename)));
logger.verbose('Found.');
const type = await fileType.fromBuffer(file);
if (type === undefined) {
@ -118,7 +118,9 @@ export const getImage = async (filename: string): Promise<Image> => {
*/
export const imageExists = async (filename: string): Promise<boolean> => {
try {
if (fs.existsSync(path.join(IMAGE_DIR, sanitize(filename)))) {
if (await fs.access(path.join(IMAGE_DIR, sanitize(filename)), fs.constants.F_OK)
.then(() => true)
.catch(() => false)) {
return true;
}
return false;

View File

@ -1,6 +1,6 @@
{
"name": "web",
"version": "1.1.3",
"version": "1.2.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.10.5",