temp commit
This commit is contained in:
parent
809cae0fee
commit
10431fdede
|
|
@ -1,23 +1,2 @@
|
|||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -4,24 +4,31 @@
|
|||
"private": true,
|
||||
"proxy": "http://localhost:3001",
|
||||
"dependencies": {
|
||||
"@emotion/react": "^11.9.0",
|
||||
"@emotion/styled": "^11.8.1",
|
||||
"@mui/icons-material": "^5.6.0",
|
||||
"@mui/lab": "^5.0.0-alpha.76",
|
||||
"@mui/material": "^5.6.0",
|
||||
"@testing-library/jest-dom": "^5.16.4",
|
||||
"@testing-library/react": "^12.1.4",
|
||||
"@testing-library/user-event": "^13.5.0",
|
||||
"@types/jest": "^27.4.1",
|
||||
"@types/node": "^16.11.26",
|
||||
"@types/react": "^17.0.44",
|
||||
"@types/react-dom": "^17.0.15",
|
||||
"@types/react": "^18.0.0",
|
||||
"@types/react-dom": "^18.0.0",
|
||||
"react": "^18.0.0",
|
||||
"react-dom": "^18.0.0",
|
||||
"react-photo-album": "^1.12.0",
|
||||
"react-router-dom": "^6.3.0",
|
||||
"react-scripts": "5.0.0",
|
||||
"typescript": "^4.6.3",
|
||||
"web-vitals": "^2.1.4"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
"start-client": "react-scripts start",
|
||||
"build-client": "react-scripts build",
|
||||
"test-client": "react-scripts test",
|
||||
"eject-client": "react-scripts eject"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": [
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
.App {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.App-logo {
|
||||
height: 40vmin;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
.App-logo {
|
||||
animation: App-logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.App-header {
|
||||
background-color: #282c34;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: calc(10px + 2vmin);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.App-link {
|
||||
color: #61dafb;
|
||||
}
|
||||
|
||||
@keyframes App-logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
import React from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
render(<App />);
|
||||
const linkElement = screen.getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
import React from 'react';
|
||||
import logo from './logo.svg';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<p>
|
||||
Edit <code>src/App.tsx</code> and save to reload.
|
||||
</p>
|
||||
<a
|
||||
className="App-link"
|
||||
href="https://reactjs.org"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Learn React
|
||||
</a>
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import {useState, useEffect} from "react";
|
||||
|
||||
function ImageGallery() {
|
||||
const [images, setImages] = useState<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/api/images")
|
||||
.then((res) => res.json())
|
||||
.then((data) => setImages(data.images));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
{images.map(image => {
|
||||
return (<img src={image} alt={"dog"}/>)
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ImageGallery;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import PhotoAlbum, {Photo} from "react-photo-album";
|
||||
|
||||
function ImageGallery({images}: { images: Photo[] }) {
|
||||
// For all kind of settings see:
|
||||
// https://react-photo-album.com/examples/playground
|
||||
// https://codesandbox.io/s/github/igordanchenko/react-photo-album/tree/main/examples/playground
|
||||
|
||||
return (
|
||||
<>
|
||||
{images.length === 0
|
||||
? <p>No images available. You may want to add images in your root directory.</p>
|
||||
: <PhotoAlbum layout="masonry" photos={images}/>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default ImageGallery;
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import Toolbar from "@mui/material/Toolbar";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import MenuIcon from "@mui/icons-material/Menu";
|
||||
import Typography from "@mui/material/Typography";
|
||||
import AppBar from "../MuiLayout/AppBar";
|
||||
|
||||
function ImageGalleryAppBar({open, drawerWidth, onDrawerOpenClick}: { open: boolean, drawerWidth: number, onDrawerOpenClick: () => void }) {
|
||||
return (
|
||||
<AppBar position="fixed" open={open} drawerwidth={drawerWidth}>
|
||||
<Toolbar>
|
||||
<IconButton
|
||||
color="inherit"
|
||||
aria-label="open drawer"
|
||||
onClick={onDrawerOpenClick}
|
||||
edge="start"
|
||||
sx={{mr: 2, ...(open && {display: 'none'})}}
|
||||
>
|
||||
<MenuIcon/>
|
||||
</IconButton>
|
||||
<Typography variant="h6" noWrap component="div">
|
||||
{process.env.TITLE ?? "Image Gallery"}
|
||||
</Typography>
|
||||
</Toolbar>
|
||||
</AppBar>
|
||||
)
|
||||
}
|
||||
|
||||
export default ImageGalleryAppBar;
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
import Drawer from "@mui/material/Drawer";
|
||||
import DrawerHeader from "../MuiLayout/DrawerHeader";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
|
||||
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
|
||||
import FolderIcon from '@mui/icons-material/Folder';
|
||||
import FolderOpenIcon from '@mui/icons-material/FolderOpen';
|
||||
import Divider from "@mui/material/Divider";
|
||||
import {useTheme} from "@mui/material/styles";
|
||||
import {TreeItem, TreeView} from '@mui/lab';
|
||||
import {Folders} from "./models";
|
||||
import {useLocation, useNavigate} from "react-router-dom";
|
||||
import {useState} from "react";
|
||||
|
||||
function getDefaultExpanded(pathname: string): string[] {
|
||||
const pathParts = []
|
||||
let curPathName = pathname.substring(0, pathname.lastIndexOf("/"))
|
||||
while (curPathName.length > 0) {
|
||||
pathParts.push(curPathName)
|
||||
curPathName = curPathName.substring(0, curPathName.lastIndexOf("/"))
|
||||
}
|
||||
return pathParts
|
||||
}
|
||||
|
||||
function generateTreeViewChildren(folders: Folders[], navigateAndToggleExpand: (path: string, navigationAllowed: boolean) => void) {
|
||||
return (<>
|
||||
{folders.map(f => {
|
||||
const label = f.numberOfFiles === 0 ? f.name : `${f.name} - (${f.numberOfFiles})`;
|
||||
const containsImages = f.numberOfFiles > 0;
|
||||
if (f.children.length === 0) {
|
||||
return (<TreeItem key={f.fullPath} nodeId={f.fullPath} label={label} onClick={() => navigateAndToggleExpand(f.fullPath, containsImages)}/>)
|
||||
}
|
||||
return (
|
||||
<TreeItem key={f.fullPath} nodeId={f.fullPath} label={label} onClick={() => navigateAndToggleExpand(f.fullPath, containsImages)}>
|
||||
{generateTreeViewChildren(f.children, navigateAndToggleExpand)}
|
||||
</TreeItem>
|
||||
)
|
||||
})}
|
||||
</>)
|
||||
}
|
||||
|
||||
function GenerateTreeView({root}: { root: Folders }) {
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const [expanded, setExpanded] = useState<string[]>(getDefaultExpanded(location.pathname))
|
||||
|
||||
const toggleExpanded = (path: string) => {
|
||||
if (expanded.includes(path)) {
|
||||
setExpanded(expanded.filter(p => p !== path))
|
||||
} else {
|
||||
setExpanded([path, ...expanded])
|
||||
}
|
||||
}
|
||||
|
||||
const navigateAndToggleExpand = (path: string, navigationAllowed: boolean) => {
|
||||
if (!navigationAllowed || location.pathname === path) {
|
||||
toggleExpanded(path);
|
||||
return;
|
||||
}
|
||||
toggleExpanded(path);
|
||||
navigate(path)
|
||||
}
|
||||
|
||||
return (
|
||||
<TreeView
|
||||
disableSelection={true}
|
||||
defaultCollapseIcon={<FolderOpenIcon/>}
|
||||
defaultExpandIcon={<FolderIcon/>}
|
||||
expanded={expanded}
|
||||
>
|
||||
<TreeItem key={root.fullPath} nodeId={root.fullPath} label={`${root.name} - (${root.numberOfFiles})`} onClick={() => navigate(root.fullPath)}/>
|
||||
{root.children.length > 0 ? generateTreeViewChildren(root.children, navigateAndToggleExpand) : <></>}
|
||||
</TreeView>
|
||||
)
|
||||
}
|
||||
|
||||
function ImageGalleryDrawer({open, drawerWidth, folder, onDrawerCloseClick}: { open: boolean, drawerWidth: number, folder: Folders, onDrawerCloseClick: () => void }) {
|
||||
const theme = useTheme();
|
||||
return (<Drawer
|
||||
sx={{
|
||||
width: drawerWidth,
|
||||
flexShrink: 0,
|
||||
'& .MuiDrawer-paper': {
|
||||
width: drawerWidth,
|
||||
boxSizing: 'border-box',
|
||||
},
|
||||
}}
|
||||
variant="persistent"
|
||||
anchor="left"
|
||||
open={open}
|
||||
>
|
||||
<DrawerHeader>
|
||||
<IconButton onClick={onDrawerCloseClick}>
|
||||
{theme.direction === 'ltr' ? <ChevronLeftIcon/> : <ChevronRightIcon/>}
|
||||
</IconButton>
|
||||
</DrawerHeader>
|
||||
<Divider/>
|
||||
<GenerateTreeView root={folder}/>
|
||||
</Drawer>)
|
||||
}
|
||||
|
||||
export default ImageGalleryDrawer;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
export interface Folders {
|
||||
name: string
|
||||
fullPath: string
|
||||
numberOfFiles: number
|
||||
children: Folders[]
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
import {useEffect, useState} from "react";
|
||||
import {Photo} from "react-photo-album";
|
||||
import Box from '@mui/material/Box';
|
||||
import CssBaseline from '@mui/material/CssBaseline';
|
||||
import {Folders} from "./ImageGallery/models";
|
||||
import {useLocation} from "react-router-dom";
|
||||
import ImageGalleryAppBar from "./ImageGallery/ImageGalleryAppBar";
|
||||
import DrawerHeader from "./MuiLayout/DrawerHeader";
|
||||
import ImageGalleryDrawer from "./ImageGallery/ImageGalleryDrawer";
|
||||
import ImageGallery from "./ImageGallery/ImageGallery";
|
||||
import Main from "./MuiLayout/Main";
|
||||
|
||||
const drawerWidth = 240;
|
||||
|
||||
function ImageGalleryLayout() {
|
||||
const [open, setOpen] = useState(true);
|
||||
const [folders, setFolders] = useState<Folders>({name: "Home", fullPath: "/", numberOfFiles: 0, children: []});
|
||||
const location = useLocation();
|
||||
|
||||
const handleDrawerOpen = () => {
|
||||
setOpen(true);
|
||||
};
|
||||
|
||||
const handleDrawerClose = () => {
|
||||
setOpen(false);
|
||||
};
|
||||
const [images, setImages] = useState<Photo[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
fetch(`/api/${location.pathname}`)
|
||||
.then((res) => res.json())
|
||||
.then((data) => setImages(data.images));
|
||||
}, [location.pathname]);
|
||||
|
||||
useEffect(() => {
|
||||
fetch("/directories")
|
||||
.then((res) => res.json())
|
||||
.then((data) => setFolders(data));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Box sx={{display: 'flex'}}>
|
||||
<CssBaseline/>
|
||||
<ImageGalleryAppBar open={open} drawerWidth={drawerWidth} onDrawerOpenClick={handleDrawerOpen}/>
|
||||
<ImageGalleryDrawer open={open} drawerWidth={drawerWidth} folder={folders} onDrawerCloseClick={handleDrawerClose}/>
|
||||
<Main open={open} drawerwidth={drawerWidth}>
|
||||
<DrawerHeader/>
|
||||
<ImageGallery images={images}/>
|
||||
</Main>
|
||||
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
export default ImageGalleryLayout;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import {styled} from "@mui/material/styles";
|
||||
import MuiAppBar from "@mui/material/AppBar";
|
||||
|
||||
const AppBar = styled(MuiAppBar, {
|
||||
shouldForwardProp: (prop) => prop !== 'open',
|
||||
})<{
|
||||
open?: boolean;
|
||||
drawerwidth: number;
|
||||
}>(({theme, open, drawerwidth}) => ({
|
||||
transition: theme.transitions.create(['margin', 'width'], {
|
||||
easing: theme.transitions.easing.sharp,
|
||||
duration: theme.transitions.duration.leavingScreen,
|
||||
}),
|
||||
...(open && {
|
||||
width: `calc(100% - ${drawerwidth}px)`,
|
||||
marginLeft: `${drawerwidth}px`,
|
||||
transition: theme.transitions.create(['margin', 'width'], {
|
||||
easing: theme.transitions.easing.easeOut,
|
||||
duration: theme.transitions.duration.enteringScreen,
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
|
||||
export default AppBar;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import {styled} from "@mui/material/styles";
|
||||
|
||||
const DrawerHeader = styled('div')(({theme}) => ({
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
padding: theme.spacing(0, 1),
|
||||
// necessary for content to be below app bar
|
||||
...theme.mixins.toolbar,
|
||||
justifyContent: 'flex-end',
|
||||
}));
|
||||
|
||||
export default DrawerHeader;
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
import {styled} from "@mui/material/styles";
|
||||
|
||||
const Main = styled('main', {shouldForwardProp: (prop) => prop !== 'open'})<{
|
||||
open?: boolean;
|
||||
drawerwidth: number;
|
||||
}>(
|
||||
({theme, open, drawerwidth}) => ({
|
||||
flexGrow: 1,
|
||||
padding: theme.spacing(3),
|
||||
transition: theme.transitions.create('margin', {
|
||||
easing: theme.transitions.easing.sharp,
|
||||
duration: theme.transitions.duration.leavingScreen,
|
||||
}),
|
||||
marginLeft: `-${drawerwidth}px`,
|
||||
...(open && {
|
||||
transition: theme.transitions.create('margin', {
|
||||
easing: theme.transitions.easing.easeOut,
|
||||
duration: theme.transitions.duration.enteringScreen,
|
||||
}),
|
||||
marginLeft: 0,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
export default Main;
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import ReactDOM from "react-dom/client";
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import reportWebVitals from './reportWebVitals';
|
||||
import ImageGallery from "./ImageGallery";
|
||||
import ImageGalleryLayout from "./ImageGalleryLayout";
|
||||
import {BrowserRouter} from "react-router-dom";
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<ImageGallery />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
|
||||
root.render(
|
||||
<BrowserRouter>
|
||||
<ImageGalleryLayout/>
|
||||
</BrowserRouter>
|
||||
)
|
||||
|
||||
// If you want to start measuring performance in your app, pass a function
|
||||
// to log results (for example: reportWebVitals(console.log))
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
|
||||
|
Before Width: | Height: | Size: 2.6 KiB |
File diff suppressed because it is too large
Load Diff
|
|
@ -5,9 +5,10 @@
|
|||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "npx tsc",
|
||||
"start": "node dist/app.js",
|
||||
"run": "npm run build && npm run start"
|
||||
"build-server": "npx tsc",
|
||||
"start-server": "node dist/app.js",
|
||||
"watch-server": "npx nodemon src/app.ts",
|
||||
"run-server": "npm run build-server && npm run start-server"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
|
@ -17,6 +18,9 @@
|
|||
"typescript": "^4.6.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.17.3"
|
||||
"image-size": "^1.0.1",
|
||||
"express": "^4.17.3",
|
||||
"nodemon": "^2.0.15",
|
||||
"ts-node": "^10.7.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,32 +1,47 @@
|
|||
import express from 'express';
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import sizeOf from "image-size";
|
||||
import {a, Folder, Folders, Image} from "./models";
|
||||
import {publicPath, walk} from "./fsExtension";
|
||||
|
||||
const app = express();
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
app.use('/images', express.static('../public'))
|
||||
app.use('/images', express.static(publicPath))
|
||||
|
||||
app.use(express.static('../picture-gallery-client/build'));
|
||||
|
||||
app.get("/api(/*)?", (req, res) => {
|
||||
const path = req.path.substring(4)
|
||||
const path = req.path.substring(4);
|
||||
|
||||
const dirents = fs.readdirSync('../public' + path, {withFileTypes: true}) // TODO: error handling?
|
||||
const dirents = fs.readdirSync(publicPath + path, {withFileTypes: true}); // TODO: error handling?
|
||||
|
||||
const normalizedPath = path === '/' ? "" : path
|
||||
const images = dirents.filter(f => f.isFile()).map(f => `/images${normalizedPath}/${f.name}`)
|
||||
const directories = dirents.filter(f => f.isDirectory()).map(d => `${normalizedPath}/${d.name}`)
|
||||
const normalizedPath = path === '/' ? "" : path;
|
||||
const images: Image[] = dirents
|
||||
.filter(f => f.isFile())
|
||||
.map(f => {
|
||||
const dimensions = sizeOf(`${publicPath}${path}/${f.name}`)
|
||||
return {
|
||||
src: `/images${normalizedPath}/${f.name}`,
|
||||
width: dimensions.width,
|
||||
height: dimensions.height,
|
||||
}
|
||||
});
|
||||
|
||||
res.json({ images: images, directories: directories });
|
||||
res.json(a<Folder>({images: images}));
|
||||
});
|
||||
|
||||
app.get("/directories", (req, res) => {
|
||||
res.json(a<Folders>(walk("/")));
|
||||
});
|
||||
|
||||
// All other GET requests not handled before will return our React app
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.resolve(__dirname, '../../picture-gallery-client/build/index.html')); // TODO: make usage of paths easier or configurabe?
|
||||
res.sendFile(path.resolve(__dirname, '../../picture-gallery-client/build/index.html'));
|
||||
});
|
||||
|
||||
// TODO: get nodemon or something similar to work to watch for changes
|
||||
app.listen(PORT, () => {
|
||||
return console.log(`Express is listening at http://localhost:${PORT}`);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
import {Folders} from "./models";
|
||||
import fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
export const publicPath = '../public'
|
||||
|
||||
export const walk = (dirPath: string): Folders => {
|
||||
const dirEnts = fs.readdirSync(publicPath + dirPath, {withFileTypes: true});
|
||||
|
||||
return {
|
||||
name: path.basename(dirPath) || "Home",
|
||||
fullPath: dirPath,
|
||||
numberOfFiles: dirEnts.filter(f => f.isFile()).length,
|
||||
children: dirEnts.filter(d => d.isDirectory()).map(d => walk(path.posix.join(dirPath, d.name)))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
export interface Image {
|
||||
src: string,
|
||||
width: number,
|
||||
height: number
|
||||
}
|
||||
|
||||
export interface Folder {
|
||||
images: Image[]
|
||||
}
|
||||
|
||||
export interface Folders {
|
||||
name: string
|
||||
fullPath: string
|
||||
numberOfFiles: number
|
||||
children: Folders[]
|
||||
}
|
||||
|
||||
export const a = <T>(v: T): T => {
|
||||
return v;
|
||||
}
|
||||
Loading…
Reference in New Issue