server format commit

This commit is contained in:
Stefan Forstenlechner 2022-04-09 20:28:35 +02:00
parent 952d4dc68e
commit a50dbd0d36
7 changed files with 3874 additions and 69 deletions

View File

@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"airbnb-base",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never",
"tsx": "never"
}
]
},
"settings": {
"import/resolver": {
"typescript": {
}
}
}
}

View File

@ -0,0 +1,2 @@
node_modules
dist

File diff suppressed because it is too large Load Diff

View File

@ -8,18 +8,30 @@
"build-server": "npx tsc", "build-server": "npx tsc",
"start-server": "node dist/app.js", "start-server": "node dist/app.js",
"watch-server": "npx nodemon src/app.ts", "watch-server": "npx nodemon src/app.ts",
"run-server": "npm run build-server && npm run start-server" "run-server": "npm run build-server && npm run start-server",
"lint": "eslint src/*",
"lint:fix": "eslint --fix src/*",
"format": "prettier --write \"**/*.+(ts|tsx)\""
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"eslint": "^8.13.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.6.2",
"typescript": "^4.6.3" "typescript": "^4.6.3"
}, },
"dependencies": { "dependencies": {
"image-size": "^1.0.1",
"express": "^4.17.3", "express": "^4.17.3",
"image-size": "^1.0.1",
"nodemon": "^2.0.15", "nodemon": "^2.0.15",
"ts-node": "^10.7.0" "ts-node": "^10.7.0"
} }

View File

@ -1,36 +1,38 @@
import express from 'express'; import express from "express";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import sizeOf from "image-size"; import sizeOf from "image-size";
import {a, Folder, Folders, Image} from "./models"; import { a, Folder, Folders, Image } from "./models";
import {publicPath, walk} from "./fsExtension"; import { publicPath, walk } from "./fsExtension";
const app = express(); const app = express();
const PORT = process.env.PORT || 3001; const PORT = process.env.PORT || 3001;
app.use('/images', express.static(publicPath)) app.use("/images", express.static(publicPath));
app.use(express.static('../picture-gallery-client/build')); app.use(express.static("../picture-gallery-client/build"));
app.get("/api(/*)?", (req, res) => { app.get("/api(/*)?", (req, res) => {
const path = req.path.substring(4); const requestedPath = req.path.substring(4);
const dirents = fs.readdirSync(publicPath + path, {withFileTypes: true}); // TODO: error handling? const dirents = fs.readdirSync(publicPath + requestedPath, {
withFileTypes: true,
});
const normalizedPath = path === '/' ? "" : path; const normalizedPath = requestedPath === "/" ? "" : requestedPath;
const images: Image[] = dirents const images: Image[] = dirents
.filter(f => f.isFile()) .filter((f) => f.isFile())
.map(f => { .map((f) => {
const dimensions = sizeOf(`${publicPath}${path}/${f.name}`) const dimensions = sizeOf(`${publicPath}${requestedPath}/${f.name}`);
return { return {
src: `/images${normalizedPath}/${f.name}`, src: `/images${normalizedPath}/${f.name}`,
width: dimensions.width, width: dimensions.width,
height: dimensions.height, height: dimensions.height,
} };
}); });
res.json(a<Folder>({images: images})); res.json(a<Folder>({ images }));
}); });
app.get("/directories", (req, res) => { app.get("/directories", (req, res) => {
@ -38,10 +40,13 @@ app.get("/directories", (req, res) => {
}); });
// All other GET requests not handled before will return our React app // All other GET requests not handled before will return our React app
app.get('*', (req, res) => { app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, '../../picture-gallery-client/build/index.html')); res.sendFile(
path.resolve(__dirname, "../../picture-gallery-client/build/index.html")
);
}); });
app.listen(PORT, () => { app.listen(PORT, () => {
// eslint-disable-next-line no-console
return console.log(`Express is listening at http://localhost:${PORT}`); return console.log(`Express is listening at http://localhost:${PORT}`);
}); });

View File

@ -1,16 +1,18 @@
import {Folders} from "./models";
import fs from "fs"; import fs from "fs";
import * as path from "path"; import * as path from "path";
import { Folders } from "./models";
export const publicPath = '../public' export const publicPath = "../public";
export const walk = (dirPath: string): Folders => { export const walk = (dirPath: string): Folders => {
const dirEnts = fs.readdirSync(publicPath + dirPath, {withFileTypes: true}); const dirEnts = fs.readdirSync(publicPath + dirPath, { withFileTypes: true });
return { return {
name: path.basename(dirPath) || "Home", name: path.basename(dirPath) || "Home",
fullPath: dirPath, fullPath: dirPath,
numberOfFiles: dirEnts.filter(f => f.isFile()).length, numberOfFiles: dirEnts.filter((f) => f.isFile()).length,
children: dirEnts.filter(d => d.isDirectory()).map(d => walk(path.posix.join(dirPath, d.name))) children: dirEnts
} .filter((d) => d.isDirectory())
} .map((d) => walk(path.posix.join(dirPath, d.name))),
};
};

View File

@ -1,20 +1,20 @@
export interface Image { export interface Image {
src: string, src: string;
width: number, width: number;
height: number height: number;
} }
export interface Folder { export interface Folder {
images: Image[] images: Image[];
} }
export interface Folders { export interface Folders {
name: string name: string;
fullPath: string fullPath: string;
numberOfFiles: number numberOfFiles: number;
children: Folders[] children: Folders[];
} }
export const a = <T>(v: T): T => { export const a = <T>(v: T): T => {
return v; return v;
} };