mirror of
https://github.com/janishutz/fundamentals-of-webengineering.git
synced 2025-11-25 13:54:25 +00:00
Compare commits
5 Commits
5fa2b1f618
...
7e336dfef0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e336dfef0 | ||
|
|
6fd4db5520 | ||
| 2d8ac9a33e | |||
| 0429e3057f | |||
| 13cc143888 |
@@ -30,6 +30,6 @@
|
||||
"@vitejs/plugin-react": "^4.3.2",
|
||||
"nodemon": "^3.1.7",
|
||||
"prettier": "^3.3.3",
|
||||
"vite": "^5.4.9"
|
||||
"vite": "^7.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +1,125 @@
|
||||
import "./css/App.css";
|
||||
import './css/App.css';
|
||||
import '@fortawesome/fontawesome-free/css/all.css';
|
||||
import { CSV_Data, fileInfo, responseObject } from './types';
|
||||
import { readCSV, convertCSVtoJSON } from './csv';
|
||||
import {
|
||||
CSV_Data, fileInfo, responseObject
|
||||
} from './types';
|
||||
import React, {
|
||||
useEffect,
|
||||
useRef, useState
|
||||
} from 'react';
|
||||
import {
|
||||
convertCSVtoJSON, readCSV
|
||||
} from './csv';
|
||||
import CSVCard from './components/CSVCard';
|
||||
import DataTable from './components/DataTable';
|
||||
import FileCard from './components/FileCard';
|
||||
import InfoCard from './components/InfoCard';
|
||||
import Layout from './components/Layout';
|
||||
import "./sse"
|
||||
|
||||
import React, { useState, useRef, useEffect } from "react";
|
||||
import Layout from "./components/Layout";
|
||||
import CSVCard from "./components/CSVCard";
|
||||
import InfoCard from "./components/InfoCard";
|
||||
import DataTable from "./components/DataTable";
|
||||
import FileCard from "./components/FileCard";
|
||||
function App () {
|
||||
const [
|
||||
data,
|
||||
setData
|
||||
] = useState( [] as CSV_Data );
|
||||
const [
|
||||
info,
|
||||
setInfo
|
||||
] = useState( {
|
||||
'filename': 'None',
|
||||
'filetype': 'None',
|
||||
'filesize': 'None',
|
||||
'rowcount': 0
|
||||
} );
|
||||
const [
|
||||
fileList,
|
||||
setFileList
|
||||
] = useState( null as responseObject | null );
|
||||
|
||||
function App() {
|
||||
const [data, setData] = useState([] as CSV_Data);
|
||||
const [info, setInfo] = useState({
|
||||
filename: "None",
|
||||
filetype: "None",
|
||||
filesize: "None",
|
||||
rowcount: 0
|
||||
});
|
||||
// Add evenbt listener for server-sent events on first render
|
||||
const [active, setActive] = useState(false);
|
||||
|
||||
const [fileList, setFileList] = useState(null as responseObject | null);
|
||||
// Effect has to be in top level of the component
|
||||
useEffect(() => {
|
||||
fetch("/status", { method: "GET" })
|
||||
.then((response) => response.json())
|
||||
.then((response) => setFileList(response))
|
||||
.catch((error) => console.log(error));
|
||||
});
|
||||
useEffect( ()=> {
|
||||
if (!active) {
|
||||
document.addEventListener("sse:uploaded", async _ev => {
|
||||
await fetch( '/status', {
|
||||
'method': 'GET'
|
||||
} )
|
||||
.then( response => response.json() )
|
||||
.then( response => setFileList( response ) )
|
||||
.catch( error => console.log( error ) );
|
||||
});
|
||||
document.addEventListener("sse:deleted", async _ev => {
|
||||
await fetch( '/status', {
|
||||
'method': 'GET'
|
||||
} )
|
||||
.then( response => response.json() )
|
||||
.then( response => setFileList( response ) )
|
||||
.catch( error => console.log( error ) );
|
||||
});
|
||||
setActive(true);
|
||||
// Initial fetch of file list at first component render
|
||||
fetch( '/status', {
|
||||
'method': 'GET'
|
||||
} )
|
||||
.then( response => response.json() )
|
||||
.then( response => setFileList( response ) )
|
||||
.catch( error => console.log( error ) );
|
||||
}
|
||||
})
|
||||
|
||||
const formRef = useRef(null);
|
||||
const formRef = useRef( null );
|
||||
|
||||
// This is triggered in CSVCard
|
||||
const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>): Promise<void> => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) throw new Error("No file received");
|
||||
// This is triggered in CSVCard
|
||||
const handleFileUpload = async ( e: React.ChangeEvent<HTMLInputElement> ): Promise<void> => {
|
||||
const file = e.target.files?.[0];
|
||||
|
||||
const data = await readCSV(e);
|
||||
if ( !file ) throw new Error( 'No file received' );
|
||||
|
||||
const newFileInfo: fileInfo = {
|
||||
filename: file.name,
|
||||
filetype: ".csv", // file.type delivers weird name
|
||||
filesize: String(file.size) + "B",
|
||||
rowcount: data.length
|
||||
}
|
||||
setInfo(newFileInfo);
|
||||
setData(data);
|
||||
const data = await readCSV( e );
|
||||
const newFileInfo: fileInfo = {
|
||||
'filename': file.name,
|
||||
'filetype': '.csv', // file.type delivers weird name
|
||||
'filesize': String( file.size ) + 'B',
|
||||
'rowcount': data.length
|
||||
};
|
||||
|
||||
if (formRef.current) {
|
||||
// Upload to server
|
||||
const formData = new FormData(formRef.current);
|
||||
await fetch("/upload", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
});
|
||||
}
|
||||
}
|
||||
setInfo( newFileInfo );
|
||||
setData( data );
|
||||
|
||||
const handleFileChange = async (fileName: string) => {
|
||||
const response = await fetch(`/download/${fileName}`);
|
||||
const blob = await response.blob();
|
||||
const text = await blob.text();
|
||||
if ( formRef.current ) {
|
||||
// Upload to server
|
||||
const formData = new FormData( formRef.current );
|
||||
|
||||
if (!response) throw new Error("No file received");
|
||||
await fetch( '/upload', {
|
||||
'method': 'POST',
|
||||
'body': formData
|
||||
} );
|
||||
}
|
||||
};
|
||||
|
||||
const data = await convertCSVtoJSON(text);
|
||||
const handleFileChange = async ( fileName: string ) => {
|
||||
const response = await fetch( `/download/${ fileName }` );
|
||||
const blob = await response.blob();
|
||||
const text = await blob.text();
|
||||
|
||||
// Updating fileInfo requires more effort since blob doesn't have the metadata
|
||||
if ( !response ) throw new Error( 'No file received' );
|
||||
|
||||
setData(data);
|
||||
}
|
||||
const data = await convertCSVtoJSON( text );
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<CSVCard handleChange={handleFileUpload} formRef={formRef}></CSVCard>
|
||||
<FileCard fileList={fileList as responseObject} fileChangeHandle={handleFileChange}></FileCard>
|
||||
<InfoCard info={info}></InfoCard>
|
||||
<DataTable data={data}></DataTable>
|
||||
</Layout>
|
||||
);
|
||||
// Updating fileInfo requires more effort since blob doesn't have the metadata
|
||||
|
||||
setData( data );
|
||||
};
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<CSVCard handleChange={handleFileUpload} formRef={formRef}></CSVCard>
|
||||
<FileCard fileList={fileList as responseObject} fileChangeHandle={handleFileChange}></FileCard>
|
||||
<InfoCard info={info}></InfoCard>
|
||||
<DataTable data={data}></DataTable>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
|
||||
@@ -1,24 +1,29 @@
|
||||
import React from "react";
|
||||
import "../css/Layout.css";
|
||||
import '../css/Layout.css';
|
||||
import React from 'react';
|
||||
|
||||
const CSVCard = (props: {
|
||||
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void,
|
||||
formRef: React.RefObject<HTMLFormElement>
|
||||
}) => {
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Upload CSV data</h2>
|
||||
</header>
|
||||
<form ref={props.formRef} action="/upload" method="post" encType="multipart/form-data" >
|
||||
<label htmlFor="file-input" className="custom-file-upload">
|
||||
<i className="fa fa-file-csv"></i> Select CSV file to explore
|
||||
</label>
|
||||
<input id="file-input" type="file" name="dataFile" aria-describedby="fileHelp" accept="text/csv" onChange={props.handleChange}/>
|
||||
<small>Please upload a CSV file, where the first row is the header.</small>
|
||||
</form>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
const CSVCard = ( props: {
|
||||
'handleChange': ( e: React.ChangeEvent<HTMLInputElement> ) => void,
|
||||
'formRef': React.RefObject<HTMLFormElement>
|
||||
} ) => {
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Upload CSV data</h2>
|
||||
</header>
|
||||
<form ref={props.formRef} action="/upload" method="post" encType="multipart/form-data" >
|
||||
<label htmlFor="file-input" className="custom-file-upload">
|
||||
<i className="fa fa-file-csv"></i> Select CSV file to explore
|
||||
</label>
|
||||
<input
|
||||
id="file-input"
|
||||
type="file"
|
||||
name="dataFile"
|
||||
aria-describedby="fileHelp" accept="text/csv" onChange={props.handleChange}/>
|
||||
<small>Please upload a CSV file, where the first row is the header.</small>
|
||||
</form>
|
||||
</article>
|
||||
);
|
||||
};
|
||||
|
||||
export default CSVCard;
|
||||
|
||||
|
||||
@@ -1,37 +1,53 @@
|
||||
import { Key, SetStateAction, useState } from "react";
|
||||
import { CSV_Data } from "../types";
|
||||
import {
|
||||
Key, SetStateAction, useState
|
||||
} from 'react';
|
||||
import {
|
||||
CSV_Data
|
||||
} from '../types';
|
||||
|
||||
const DataTable = (props: {data: CSV_Data}) => {
|
||||
if (props.data.length == 0) return <></>;
|
||||
const DataTable = ( props: {
|
||||
'data': CSV_Data
|
||||
} ) => {
|
||||
if ( props.data.length == 0 ) return <></>;
|
||||
|
||||
const header = Object.keys(props.data[0]!);
|
||||
const [sortCol, setSortCol] = useState("None");
|
||||
const [sortType, setSortType] = useState("asc");
|
||||
const header = Object.keys( props.data[0]! );
|
||||
const [
|
||||
sortCol,
|
||||
setSortCol
|
||||
] = useState( 'None' );
|
||||
const [
|
||||
sortType,
|
||||
setSortType
|
||||
] = useState( 'asc' );
|
||||
|
||||
const sortingHandler = (col: String) => {
|
||||
if (sortCol !== col) {
|
||||
setSortCol(col as SetStateAction<string>);
|
||||
setSortType("asc");
|
||||
} else if (sortType === "asc") {
|
||||
setSortType("desc");
|
||||
const sortingHandler = ( col: string ) => {
|
||||
if ( sortCol !== col ) {
|
||||
setSortCol( col as SetStateAction<string> );
|
||||
setSortType( 'asc' );
|
||||
} else if ( sortType === 'asc' ) {
|
||||
setSortType( 'desc' );
|
||||
} else {
|
||||
setSortCol("None");
|
||||
setSortType("None");
|
||||
setSortCol( 'None' );
|
||||
setSortType( 'None' );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if ( sortCol !== 'None' && sortType === 'asc' ) {
|
||||
props.data.sort( ( a, b ) => {
|
||||
if ( a[sortCol]! < b[sortCol]! ) return -1;
|
||||
|
||||
if ( a[sortCol]! > b[sortCol]! ) return 1;
|
||||
|
||||
if (sortCol !== "None" && sortType === "asc") {
|
||||
props.data.sort( (a, b) => {
|
||||
if (a[sortCol]! < b[sortCol]!) return -1;
|
||||
if (a[sortCol]! > b[sortCol]!) return 1;
|
||||
return 0;
|
||||
});
|
||||
} else if (sortCol !== "None" && sortType === "desc") {
|
||||
props.data.sort( (a, b) => {
|
||||
if (a[sortCol]! > b[sortCol]!) return -1;
|
||||
if (a[sortCol]! < b[sortCol]!) return 1;
|
||||
} );
|
||||
} else if ( sortCol !== 'None' && sortType === 'desc' ) {
|
||||
props.data.sort( ( a, b ) => {
|
||||
if ( a[sortCol]! > b[sortCol]! ) return -1;
|
||||
|
||||
if ( a[sortCol]! < b[sortCol]! ) return 1;
|
||||
|
||||
return 0;
|
||||
});
|
||||
} );
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -43,49 +59,61 @@ const DataTable = (props: {data: CSV_Data}) => {
|
||||
<table id="table-content">
|
||||
<thead>
|
||||
<tr>
|
||||
{
|
||||
header.map( (col) => (
|
||||
<ColHeader col={col} sortingHandle={sortingHandler} isSelected={col == sortCol} sortType={sortType}></ColHeader>
|
||||
))
|
||||
}
|
||||
{
|
||||
header.map( col => <ColHeader
|
||||
col={col}
|
||||
sortingHandle={sortingHandler}
|
||||
isSelected={col == sortCol}
|
||||
sortType={sortType}></ColHeader> )
|
||||
}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
props.data.map( (row, i) => (
|
||||
props.data.map( ( row, i ) => (
|
||||
<tr key={i}>
|
||||
{
|
||||
header.map( (col) => (
|
||||
<Row key={i} col={col} content={row[col] as String}></Row>
|
||||
))
|
||||
header.map( col => <Row key={i} col={col} content={row[col] as string}></Row> )
|
||||
}
|
||||
</tr>
|
||||
))
|
||||
) )
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</article>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const ColHeader = (props: {col: String, sortingHandle: (s: String) => void, isSelected: boolean, sortType: String}) => {
|
||||
const ColHeader = ( props: {
|
||||
'col': string,
|
||||
'sortingHandle': ( s: string ) => void,
|
||||
'isSelected': boolean,
|
||||
'sortType': string
|
||||
} ) => {
|
||||
return (
|
||||
<th
|
||||
className={
|
||||
props.isSelected
|
||||
? (props.sortType === "asc" ? "active sorting asc" : "active sorting desc")
|
||||
: "sortable"
|
||||
? ( props.sortType === 'asc' ? 'active sorting asc' : 'active sorting desc' )
|
||||
: 'sortable'
|
||||
}
|
||||
onClick={() => {props.sortingHandle!(props.col)}}
|
||||
onClick={() => {
|
||||
props.sortingHandle!( props.col );
|
||||
}}
|
||||
key={props.col as Key}>
|
||||
{props.col}
|
||||
</th>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const Row = (props: {col: String, content: String, key: Key}) => {
|
||||
const Row = ( props: {
|
||||
'col': string,
|
||||
'content': string,
|
||||
'key': Key
|
||||
} ) => {
|
||||
return <td key={props.col as Key}>{props.content}</td>;
|
||||
}
|
||||
};
|
||||
|
||||
export default DataTable;
|
||||
|
||||
|
||||
@@ -1,58 +1,65 @@
|
||||
import { responseObject } from "../types";
|
||||
import {
|
||||
responseObject
|
||||
} from '../types';
|
||||
|
||||
const FileCard = (props: {
|
||||
fileList: responseObject,
|
||||
fileChangeHandle: (fileName: string) => Promise<void>
|
||||
}) => {
|
||||
const FileCard = ( props: {
|
||||
'fileList': responseObject,
|
||||
'fileChangeHandle': ( fileName: string ) => Promise<void>
|
||||
} ) => {
|
||||
const convert = ( res: responseObject ) => {
|
||||
const list = [];
|
||||
|
||||
const convert = (res: responseObject) => {
|
||||
let list = [];
|
||||
for (let i = 0; i < res.names.length; i++) {
|
||||
for ( let i = 0; i < res.names.length; i++ ) {
|
||||
const elem = {
|
||||
filename: res.names[i],
|
||||
uploadTime: res.uploadTimes[i]
|
||||
}
|
||||
list.push(elem);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
'filename': res.names[i],
|
||||
'uploadTime': res.uploadTimes[i]
|
||||
};
|
||||
|
||||
const list = props.fileList != null ? convert(props.fileList) : null;
|
||||
list.push( elem );
|
||||
}
|
||||
|
||||
return list;
|
||||
};
|
||||
|
||||
const list = props.fileList != null ? convert( props.fileList ) : null;
|
||||
|
||||
return (
|
||||
<article className="wide">
|
||||
<header>
|
||||
<h2>Select a File</h2>
|
||||
</header>
|
||||
<table id="table-content">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Filename</th>
|
||||
<th>Upload Time</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
list ? list.map( (file, i) => (
|
||||
<FileRow key={i} filename={file.filename!} uploadTime={file.uploadTime!} fileChangeHandle={props.fileChangeHandle}></FileRow>
|
||||
)) : <tr></tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
<table id="table-content">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Filename</th>
|
||||
<th>Upload Time</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
list ? list.map( ( file, i ) => <FileRow
|
||||
key={i}
|
||||
filename={file.filename!}
|
||||
uploadTime={file.uploadTime!}
|
||||
fileChangeHandle={props.fileChangeHandle}/> ) : <tr></tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const FileRow = (props: {
|
||||
filename: string,
|
||||
uploadTime: string,
|
||||
fileChangeHandle: (fileName: string) => Promise<void>
|
||||
}) => {
|
||||
|
||||
const remFile = async () => {
|
||||
await fetch(`/delete/${props.filename}`, { method: "DELETE" });
|
||||
}
|
||||
const FileRow = ( props: {
|
||||
'filename': string,
|
||||
'uploadTime': string,
|
||||
'fileChangeHandle': ( fileName: string ) => Promise<void>
|
||||
} ) => {
|
||||
const rmFile = async () => {
|
||||
await fetch( `/delete/${ props.filename }`, {
|
||||
'method': 'DELETE'
|
||||
} );
|
||||
};
|
||||
|
||||
return (
|
||||
<tr>
|
||||
@@ -60,12 +67,17 @@ const FileRow = (props: {
|
||||
<td>{props.uploadTime}</td>
|
||||
<td>
|
||||
<div className="action-icons">
|
||||
<i onClick={() => { remFile()} } className="fa-solid fa-trash-can"></i>
|
||||
<i onClick={() => {props.fileChangeHandle(props.filename)}} className="fa-solid fa-file-arrow-down"></i>
|
||||
<i onClick={() => {
|
||||
rmFile();
|
||||
} } className="fa-solid fa-trash-can"></i>
|
||||
<i onClick={() => {
|
||||
props.fileChangeHandle( props.filename );
|
||||
}} className="fa-solid fa-file-arrow-down"></i>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default FileCard;
|
||||
|
||||
|
||||
@@ -1,34 +1,37 @@
|
||||
import { fileInfo } from "../types";
|
||||
import {
|
||||
fileInfo
|
||||
} from '../types';
|
||||
|
||||
const InfoCard = (props: {
|
||||
info: fileInfo
|
||||
}) => {
|
||||
const InfoCard = ( props: {
|
||||
'info': fileInfo
|
||||
} ) => {
|
||||
let noFileMessage = <div></div>;
|
||||
|
||||
let noFileMessage = <div></div>
|
||||
if (props.info.filename === "None")
|
||||
noFileMessage = <div id="data-info-placeholder">No file selected</div>;
|
||||
if ( props.info.filename === 'None' )
|
||||
noFileMessage = <div id="data-info-placeholder">No file selected</div>;
|
||||
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Data infos</h2>
|
||||
</header>
|
||||
<div className="info">
|
||||
{noFileMessage}
|
||||
<h4>Filename</h4>
|
||||
<p>{props.info.filename}</p>
|
||||
return (
|
||||
<article>
|
||||
<header>
|
||||
<h2>Data infos</h2>
|
||||
</header>
|
||||
<div className="info">
|
||||
{noFileMessage}
|
||||
<h4>Filename</h4>
|
||||
<p>{props.info.filename}</p>
|
||||
|
||||
<h4>File type</h4>
|
||||
<p>{props.info.filetype}</p>
|
||||
<h4>File type</h4>
|
||||
<p>{props.info.filetype}</p>
|
||||
|
||||
<h4>File size</h4>
|
||||
<p>{props.info.filesize}</p>
|
||||
<h4>File size</h4>
|
||||
<p>{props.info.filesize}</p>
|
||||
|
||||
<h4>Number of rows</h4>
|
||||
<p>{props.info.rowcount}</p>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
<h4>Number of rows</h4>
|
||||
<p>{props.info.rowcount}</p>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
};
|
||||
|
||||
export default InfoCard;
|
||||
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import React from "react";
|
||||
import "../css/Layout.css";
|
||||
import '../css/Layout.css';
|
||||
import React from 'react';
|
||||
|
||||
const Layout = (props: { children: React.ReactNode }) => {
|
||||
return (
|
||||
<>
|
||||
<nav className="container-fluid">
|
||||
<ul>
|
||||
<li>
|
||||
<h1>Open data explorer</h1>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<main className="container-fluid">
|
||||
{props.children}
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
const Layout = ( props: {
|
||||
'children': React.ReactNode
|
||||
} ) => {
|
||||
return (
|
||||
<>
|
||||
<nav className="container-fluid">
|
||||
<ul>
|
||||
<li>
|
||||
<h1>Open data explorer</h1>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<main className="container-fluid">
|
||||
{props.children}
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Layout;
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { CSV_Data } from './types';
|
||||
import { csv2json } from 'json-2-csv';
|
||||
import {
|
||||
CSV_Data
|
||||
} from './types';
|
||||
import {
|
||||
csv2json
|
||||
} from 'json-2-csv';
|
||||
|
||||
export const convertCSVtoJSON = async ( csvText: string ) => {
|
||||
// Type cast OK, as the typing of the external library is not perfect -> Actually it is.
|
||||
|
||||
21
task_3_react/src/client/sse.ts
Normal file
21
task_3_react/src/client/sse.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
interface SSEMessage {
|
||||
'event': string,
|
||||
'data': string
|
||||
}
|
||||
const eventSource: EventSource = new EventSource( '/sse' );
|
||||
|
||||
eventSource.onopen = () => {
|
||||
document.dispatchEvent( new CustomEvent( 'sse:connect', {
|
||||
'detail': 'success',
|
||||
'cancelable': false
|
||||
} ) );
|
||||
};
|
||||
|
||||
eventSource.onmessage = event => {
|
||||
const data: SSEMessage = JSON.parse( event.data );
|
||||
|
||||
document.dispatchEvent( new CustomEvent( 'sse:' + data.event, {
|
||||
'cancelable': false,
|
||||
'detail': data.data
|
||||
} ) );
|
||||
};
|
||||
@@ -1,81 +1,160 @@
|
||||
import express from "express";
|
||||
import ViteExpress from "vite-express";
|
||||
import multer from "multer";
|
||||
import * as fs from "node:fs/promises";
|
||||
import { responseObject } from "./types";
|
||||
import path from "path";
|
||||
import * as fs from 'node:fs/promises';
|
||||
import {
|
||||
EventEmitter
|
||||
} from 'node:stream';
|
||||
import ViteExpress from 'vite-express';
|
||||
import express from 'express';
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import {
|
||||
responseObject
|
||||
} from './types';
|
||||
|
||||
const app = express();
|
||||
|
||||
// Set up file storage
|
||||
const storage = multer.diskStorage({
|
||||
destination: "./src/server/uploads",
|
||||
filename: (_req, file, cb) => {
|
||||
const storage = multer.diskStorage( {
|
||||
'destination': './src/server/uploads',
|
||||
'filename': (
|
||||
_req, file, cb
|
||||
) => {
|
||||
// Suggested in Multer's readme
|
||||
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1E3);
|
||||
cb(null, file.fieldname + "-" + uniqueSuffix);
|
||||
}
|
||||
});
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round( Math.random() * 1E3 );
|
||||
|
||||
fileEvent.emit( 'uploaded', file.fieldname + '-' + uniqueSuffix );
|
||||
|
||||
cb( null, file.fieldname + '-' + uniqueSuffix );
|
||||
}
|
||||
} );
|
||||
// CSV file upload endpoint
|
||||
const upload = multer({ storage: storage });
|
||||
const upload = multer( {
|
||||
'storage': storage
|
||||
} );
|
||||
|
||||
class FileEvent extends EventEmitter {}
|
||||
|
||||
const fileEvent = new FileEvent();
|
||||
|
||||
app.post(
|
||||
"/upload",
|
||||
upload.single("dataFile"),
|
||||
(req, res, next) => {
|
||||
console.log(req, res, next)
|
||||
}
|
||||
'/upload',
|
||||
upload.single( 'dataFile' ),
|
||||
(
|
||||
req, res, next
|
||||
) => {
|
||||
console.log(
|
||||
req, res, next
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// Endpoint to send back file names/upload times
|
||||
app.get("/status", async (_req, res) => {
|
||||
const resObject: responseObject = {
|
||||
names: [],
|
||||
uploadTimes: []
|
||||
};
|
||||
app.get( '/status', async ( _req, res ) => {
|
||||
const resObject: responseObject = {
|
||||
'names': [],
|
||||
'uploadTimes': []
|
||||
};
|
||||
const dir = await fs.opendir( './src/server/uploads/' );
|
||||
|
||||
const dir = await fs.opendir("./src/server/uploads/");
|
||||
for await (const file of dir) {
|
||||
resObject.names.push(file.name);
|
||||
const stats = await fs.stat(`./src/server/uploads/${file.name}`);
|
||||
resObject.uploadTimes.push(stats.birthtime.toString());
|
||||
}
|
||||
for await ( const file of dir ) {
|
||||
resObject.names.push( file.name );
|
||||
const stats = await fs.stat( `./src/server/uploads/${ file.name }` );
|
||||
|
||||
res.status(200).json(resObject);
|
||||
})
|
||||
resObject.uploadTimes.push( stats.birthtime.toString() );
|
||||
}
|
||||
|
||||
res.status( 200 ).json( resObject );
|
||||
} );
|
||||
|
||||
// Endpoint to send back whole files
|
||||
app.get("/download/:fileName", (req, res) => {
|
||||
const fileName = req.params.fileName;
|
||||
const filePath = path.join(__dirname, "uploads", fileName); // Filepaths must be absolute
|
||||
res.sendFile(filePath, err => {
|
||||
if (err) {
|
||||
console.error("Error sending file:", err);
|
||||
res.status(500).send("Error downloading file");
|
||||
}
|
||||
});
|
||||
})
|
||||
app.get( '/download/:fileName', ( req, res ) => {
|
||||
const fileName = req.params.fileName;
|
||||
const filePath = path.join(
|
||||
__dirname, 'uploads', fileName
|
||||
); // Filepaths must be absolute
|
||||
|
||||
res.sendFile( filePath, err => {
|
||||
if ( err ) {
|
||||
console.error( 'Error sending file:', err );
|
||||
res.status( 500 ).send( 'Error downloading file' );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
|
||||
// Endpoint to remove files from server
|
||||
app.delete("/delete/:fileName", async (req, res) => {
|
||||
const fileName = req.params.fileName;
|
||||
const filePath = path.join(__dirname, "uploads", fileName);
|
||||
try {
|
||||
await fs.unlink(filePath); // deletes the file
|
||||
res.status(200).send("File deleted successfully");
|
||||
} catch (error) {
|
||||
console.error("Error deleting file:", error);
|
||||
res.status(500).send("Error deleting file");
|
||||
}
|
||||
});
|
||||
app.delete( '/delete/:fileName', async ( req, res ) => {
|
||||
const fileName = req.params.fileName;
|
||||
const filePath = path.join(
|
||||
__dirname, 'uploads', fileName
|
||||
);
|
||||
|
||||
try {
|
||||
await fs.rm( filePath ); // deletes the file
|
||||
res.status( 200 ).send( 'File deleted successfully' );
|
||||
fileEvent.emit( 'deleted', filePath );
|
||||
} catch ( error ) {
|
||||
console.error( 'Error deleting file:', error );
|
||||
res.status( 500 ).send( 'Error deleting file' );
|
||||
}
|
||||
} );
|
||||
|
||||
// example route which returns a message
|
||||
app.get("/hello", async function (_req, res) {
|
||||
res.status(200).json({ message: "Hello World!" });
|
||||
});
|
||||
app.get( '/hello', async function ( _req, res ) {
|
||||
res.status( 200 ).json( {
|
||||
'message': 'Hello World!'
|
||||
} );
|
||||
} );
|
||||
|
||||
|
||||
interface SSESubscriber {
|
||||
'uuid': string;
|
||||
'response': express.Response;
|
||||
}
|
||||
|
||||
interface SSESubscribers {
|
||||
[id: string]: SSESubscriber | undefined;
|
||||
}
|
||||
const subscribers: SSESubscribers = {};
|
||||
|
||||
app.get( '/sse', async ( request: express.Request, response: express.Response ) => {
|
||||
response.writeHead( 200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
} );
|
||||
response.status( 200 );
|
||||
response.flushHeaders();
|
||||
response.write( `data: ${ JSON.stringify( [] ) }\n\n` );
|
||||
|
||||
const uuid = crypto.randomUUID();
|
||||
|
||||
subscribers[uuid] = {
|
||||
'uuid': uuid,
|
||||
'response': response
|
||||
};
|
||||
|
||||
request.on( 'close', () => {
|
||||
subscribers[ uuid ] = undefined;
|
||||
} );
|
||||
} );
|
||||
|
||||
const sendSSEData = ( event: string, data: string ) => {
|
||||
const subs = Object.values( subscribers );
|
||||
|
||||
for ( let i = 0; i < subs.length; i++ ) {
|
||||
subs[i]!.response.write( `data: ${ JSON.stringify( {
|
||||
'event': event,
|
||||
'data': data
|
||||
} ) }\n\n` );
|
||||
}
|
||||
};
|
||||
|
||||
fileEvent.on( 'uploaded', file => {
|
||||
sendSSEData( 'uploaded', file );
|
||||
} );
|
||||
fileEvent.on( 'deleted', file => {
|
||||
sendSSEData( 'deleted', file );
|
||||
} );
|
||||
|
||||
// Do not change below this line
|
||||
ViteExpress.listen(app, 5173, () =>
|
||||
console.log("Server is listening on http://localhost:5173"),
|
||||
ViteExpress.listen(
|
||||
app, 5173, () => console.log( 'Server is listening on http://localhost:5173' ),
|
||||
);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user