Compare commits
No commits in common. "888e35f8390e70ab615e7b77db2d1a2f3dc1fc3a" and "d63f096aaa0afe90080754b766f13d0a7f5f3d50" have entirely different histories.
888e35f839
...
d63f096aaa
|
|
@ -1,16 +0,0 @@
|
|||
node_modules
|
||||
Dockerfile*
|
||||
.dockerignore
|
||||
.git
|
||||
.gitignore
|
||||
README.md
|
||||
LICENSE
|
||||
|
||||
databases
|
||||
docs
|
||||
documents
|
||||
pdfgen
|
||||
!pdfgen/template-*
|
||||
scripts
|
||||
tmp-user-data
|
||||
user-data
|
||||
|
|
@ -33,5 +33,3 @@ pdfgen/*
|
|||
!pdfgen/template*.tex
|
||||
|
||||
documents/*
|
||||
user-data/*
|
||||
tmp-user-data/*
|
||||
|
|
|
|||
49
Dockerfile
49
Dockerfile
|
|
@ -1,49 +0,0 @@
|
|||
FROM oven/bun:alpine AS base
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
ENV APP_USER_DATA_PATH="/app-data/user-data"
|
||||
ENV APP_TMP_USER_DATA_PATH="/app-data/tmp"
|
||||
|
||||
## Stage 1: Install dependencies
|
||||
FROM base AS install
|
||||
|
||||
# install development dependencies
|
||||
RUN mkdir -p /temp/dev
|
||||
COPY package.json bun.lockb /temp/dev/
|
||||
RUN cd /temp/dev && bun install --frozen-lockfile
|
||||
|
||||
# install production dependencies
|
||||
RUN mkdir -p /temp/prod
|
||||
COPY package.json bun.lockb /temp/prod/
|
||||
RUN cd /temp/prod && bun install --frozen-lockfile --production
|
||||
|
||||
## Stage 2: Build app
|
||||
FROM base AS prerelease
|
||||
|
||||
# copy dependencies into workdir
|
||||
COPY --from=install /temp/dev/node_modules node_modules
|
||||
|
||||
# add project files
|
||||
COPY . .
|
||||
|
||||
# build project
|
||||
RUN bun run build
|
||||
|
||||
## Stage 3: Put everything together
|
||||
FROM base AS release
|
||||
|
||||
# compose the final image
|
||||
COPY --from=install /temp/prod/node_modules node_modules
|
||||
COPY --from=prerelease /usr/src/app/build .
|
||||
#COPY --from=prerelease /usr/src/app/package.json .
|
||||
|
||||
RUN mkdir /app-data && chown bun:bun /app-data
|
||||
|
||||
VOLUME ["/app-data"]
|
||||
|
||||
USER bun
|
||||
EXPOSE 3000/tcp
|
||||
ENTRYPOINT [ "bun", "--bun", "run", "start" ]
|
||||
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
services:
|
||||
application:
|
||||
build: .
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
name: Stundenaufzeichnung
|
||||
|
||||
volumes:
|
||||
user-data:
|
||||
|
||||
services:
|
||||
application:
|
||||
build: https://git.maschek.info/patrick/stundenaufzeichnung.git#main
|
||||
ports:
|
||||
- 3000:3000
|
||||
volumes:
|
||||
- type: volume
|
||||
source: user-data
|
||||
target: /app-data
|
||||
|
||||
|
|
@ -6,7 +6,6 @@
|
|||
"@sveltejs/adapter-node": "^5.2.12",
|
||||
"@sveltejs/kit": "^2.20.4",
|
||||
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
||||
"@types/bun": "^1.2.10",
|
||||
"@types/sqlite3": "^3.1.11",
|
||||
"svelte": "^5.25.6",
|
||||
"svelte-adapter-bun": "^0.5.2",
|
||||
|
|
|
|||
|
|
@ -1,14 +1,9 @@
|
|||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||
|
||||
import type { User } from "$lib/server/database";
|
||||
|
||||
// for information about these interfaces
|
||||
declare global {
|
||||
namespace App {
|
||||
// interface Error {}
|
||||
interface Locals {
|
||||
user: User
|
||||
}
|
||||
// interface Locals {}
|
||||
// interface PageData {}
|
||||
// interface PageState {}
|
||||
// interface Platform {}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,11 @@
|
|||
import type { Handle } from "@sveltejs/kit";
|
||||
import { error } from "@sveltejs/kit";
|
||||
|
||||
import { init_db, close_db, get_user } from "$lib/server/database";
|
||||
import { User, UserDB, init_db, close_db, create_user, get_user, get_user_db } from "$lib/server/database";
|
||||
|
||||
async function init() {
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
if (process.env.APP_USER_DATA_PATH == null) {
|
||||
console.log("APP_USER_DATA_PATH is not defined. Exiting.");
|
||||
process.exit(-1);
|
||||
}
|
||||
|
||||
if (process.env.APP_TMP_USER_DATA_PATH == null) {
|
||||
console.log("APP_TMP_USER_DATA_PATH is not defined. Exiting.");
|
||||
process.exit(-1);
|
||||
}
|
||||
function init() {
|
||||
|
||||
await init_db();
|
||||
init_db();
|
||||
|
||||
console.log("started");
|
||||
|
||||
|
|
@ -26,27 +16,24 @@ function deinit() {
|
|||
console.log('exit');
|
||||
}
|
||||
|
||||
await init();
|
||||
init();
|
||||
|
||||
process.on('exit', (_) => {
|
||||
process.on('exit', (reason) => {
|
||||
deinit();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on('SIGINT', (_) => {
|
||||
process.on('SIGINT', (reason) => {
|
||||
console.log("SIGINT")
|
||||
process.exit(0);
|
||||
})
|
||||
|
||||
export let handle: Handle = async function ({ event, resolve }) {
|
||||
import { getAllFiles } from "$lib/server/docstore"
|
||||
|
||||
export async function handle({ event, resolve }) {
|
||||
|
||||
let user = get_user();
|
||||
|
||||
if (!user) {
|
||||
return error(404, "No user"); // redirect login
|
||||
}
|
||||
|
||||
event.locals.user = user;
|
||||
event.locals.user = get_user();
|
||||
console.log("handle");
|
||||
|
||||
return await resolve(event);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
|
||||
import { Database } from 'bun:sqlite';
|
||||
|
||||
export interface UserEntry {
|
||||
id: number;
|
||||
gender: string;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import fs from "node:fs/promises";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { Database, SQLiteError } from "bun:sqlite";
|
||||
|
||||
import { UserEntry, RecordEntry, EstimatesEntry } from "$lib/db_types";
|
||||
import { calculateDuration, parseDate, toInt, isTimeValidHHMM } from "$lib/util";
|
||||
|
||||
const DATABASES_PATH: string = (process.env.APP_USER_DATA_PATH ?? ".") + "/databases/";
|
||||
const DATABASES_PATH: string = "./databases/";
|
||||
const USER_DATABASE_PATH: string = DATABASES_PATH + "users.sqlite";
|
||||
|
||||
const CHECK_QUERY: string =
|
||||
|
|
@ -291,7 +291,7 @@ function is_db_initialized(db: Database): boolean {
|
|||
throw exception;
|
||||
}
|
||||
|
||||
console.log(exception);
|
||||
console.log(e);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -305,10 +305,8 @@ function setup_db(db: Database, setup_queries: string[]) {
|
|||
setup_queries.forEach((q) => { /*console.log(q);*/ db.query(q).run(); });
|
||||
}
|
||||
|
||||
export async function init_db() {
|
||||
export function init_db() {
|
||||
|
||||
const stdout = await fs.mkdir(DATABASES_PATH, { recursive: true });
|
||||
console.log(stdout)
|
||||
user_database = new Database(USER_DATABASE_PATH, { strict: true, create: true });
|
||||
|
||||
if (!is_db_initialized(user_database)) {
|
||||
|
|
@ -368,8 +366,6 @@ export function get_user(): User | null {
|
|||
const db_name = get_user_db_name(user);
|
||||
|
||||
try {
|
||||
|
||||
fs.mkdir(DATABASES_PATH, { recursive: true });
|
||||
|
||||
let userdb = new Database(db_name, { create: true, strict: true });
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,21 @@
|
|||
import b from "bun";
|
||||
import fs from "node:fs/promises"
|
||||
|
||||
import type { RecordEntry } from "$lib/db_types";
|
||||
import { User } from "$lib/server/database";
|
||||
import { MONTHS, toInt, padInt, parseDate, calculateDuration, isoToLocalDate, month_of, weekday_of } from "$lib/util"
|
||||
|
||||
|
||||
export class UserBusyException extends Error {
|
||||
userid: number
|
||||
|
||||
constructor(message: string, userid: number) {
|
||||
super(`user (id: ${userid}) is busy: ${message}`);
|
||||
this.name = this.constructor.name;
|
||||
this.userid = userid;
|
||||
}
|
||||
constructor(message, userid) {
|
||||
super(`user (id: ${userid}) is busy: ${message}`);
|
||||
this.name = this.constructor.name;
|
||||
this.userid = userid;
|
||||
}
|
||||
}
|
||||
|
||||
export class LatexException extends Error {
|
||||
errno: number
|
||||
stdout: string
|
||||
|
||||
constructor(message: string, userid: number, errno: number, stdout: string) {
|
||||
constructor(message, userid, errno, stdout) {
|
||||
super(message + ` (userid: ${userid}, errno: ${errno})\n${stdout}`);
|
||||
this.name = this.constructor.name;
|
||||
this.errno = errno;
|
||||
|
|
@ -28,129 +24,107 @@ export class LatexException extends Error {
|
|||
}
|
||||
|
||||
export class FileExistsException extends Error {
|
||||
path: string
|
||||
|
||||
constructor(path: string) {
|
||||
constructor(path) {
|
||||
super("File already exists: " + path);
|
||||
this.name = this.constructor.name;
|
||||
this.path = path;
|
||||
}
|
||||
}
|
||||
|
||||
export interface FileProperties {
|
||||
identifier: string
|
||||
path: string
|
||||
name: string
|
||||
cdate: Date
|
||||
}
|
||||
|
||||
|
||||
const DOCUMENTS_PATH: string = (process.env.APP_USER_DATA_PATH ?? ".") + "/documents"
|
||||
const DOCUMENTS_PATH: string = "./documents"
|
||||
const ESTIMATES_FOLD: string = "estimates"
|
||||
const RECORDS_FOLD: string = "records"
|
||||
|
||||
const GENERATION_PATH: string = (process.env.APP_TMP_USER_DATA_PATH ?? ".") + "/pdfgen";
|
||||
const GENERATION_PATH: string = "./pdfgen";
|
||||
|
||||
const TEMPLATE_REC = "template-rec.tex";
|
||||
const TEMPLATE_REC_PATH = `./templates/${TEMPLATE_REC}`;
|
||||
const TEMPLATE_REC_PATH = `${GENERATION_PATH}/${TEMPLATE_REC}`;
|
||||
|
||||
const TEMPLATE_EST = "template-est.tex";
|
||||
const TEMPLATE_EST_PATH = `./templates/${TEMPLATE_EST}`;
|
||||
const TEMPLATE_EST_PATH = `${GENERATION_PATH}/${TEMPLATE_EST}`;
|
||||
|
||||
const SALUTATION: Record<string, string> = {
|
||||
const SALUTATION = {
|
||||
m: "Herrn",
|
||||
w: "Frau"
|
||||
}
|
||||
|
||||
const GENDER_END: Record<string, string> = {
|
||||
const GENDER_END = {
|
||||
m: "",
|
||||
w: "in",
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
const SALUTATION: Map<string, string> = new Map(Object.entries({
|
||||
m: "Herrn",
|
||||
w: "Frau"
|
||||
}))
|
||||
|
||||
const GENDER_END: Map<string, string> = new Map(Object.entries({
|
||||
"m": "",
|
||||
"w": "in",
|
||||
}))
|
||||
*/
|
||||
|
||||
// this may be a race condition???
|
||||
// is a mutex needed in JS?
|
||||
let locked_users: Set<Number> = new Set();
|
||||
let locked_users: number[] = new Set();
|
||||
|
||||
export async function getAllFiles(user: User) {
|
||||
const path = `${DOCUMENTS_PATH}/user-${user.id}`;
|
||||
|
||||
let file_names = (await fs.readdir(path, { recursive: true }).catch((_) => {
|
||||
let file_names = (await fs.readdir(path, { recursive: true }).catch((err) => {
|
||||
return [];
|
||||
})).filter((v) => {
|
||||
return !(v == ESTIMATES_FOLD || v == RECORDS_FOLD);
|
||||
});
|
||||
|
||||
let files: FileProperties[] = (await Promise.all(file_names.map(async (v) => {
|
||||
let files = (await Promise.all(file_names.map(async (v) => {
|
||||
return {
|
||||
identifier: v.split("/").pop(),
|
||||
path: v,
|
||||
name: v.split("/").pop(),
|
||||
cdate: (await fs.stat(`${path}/${v}`)).ctime
|
||||
} as FileProperties;
|
||||
timestamp: (await fs.stat(`${path}/${v}`)).mtime
|
||||
}
|
||||
}))).sort((a, b) => {
|
||||
return a.cdate.getTime() - b.cdate.getTime()
|
||||
return a.timestamp - b.timestamp
|
||||
});
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function getRecordFiles(user: User): Promise<FileProperties[]> {
|
||||
export async function getRecordFiles(user: User) {
|
||||
const path = `${DOCUMENTS_PATH}/user-${user.id}/${RECORDS_FOLD}`;
|
||||
|
||||
let file_names = await fs.readdir(path).catch((err) => { console.log(err); return [] });
|
||||
|
||||
let files: FileProperties[] = await Promise.all(file_names.map(async (v) => {
|
||||
let files = await Promise.all(file_names.map(async (v) => {
|
||||
return {
|
||||
identifier: v.replace(/^Stundenliste-/, "").replace(/\.pdf$/, ""),
|
||||
name: v,
|
||||
filename: v,
|
||||
path: `${RECORDS_FOLD}/${v}`,
|
||||
cdate: (await fs.stat(`${path}/${v}`)).ctime,
|
||||
} as FileProperties;
|
||||
}
|
||||
}))
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function getEstimateFiles(user: User): Promise<FileProperties[]> {
|
||||
export async function getEstimateFiles(user: User) {
|
||||
const path = `${DOCUMENTS_PATH}/user-${user.id}/${ESTIMATES_FOLD}`;
|
||||
|
||||
let file_names = await fs.readdir(path).catch((err) => { console.log(err); return [] });
|
||||
|
||||
let files: FileProperties[] = await Promise.all(file_names.map(async (v) => {
|
||||
let files = await Promise.all(file_names.map(async (v) => {
|
||||
return {
|
||||
identifier: v.match(/\d.Quartal_\d\d\d\d/)?.[0].replace(/.Quartal_/, ".").split(".").reverse().join("-") ?? "",
|
||||
name: v,
|
||||
identifier: v.match(/\d.Quartal_\d\d\d\d/)[0].replace(/.Quartal_/, ".").split(".").reverse().join("-"),
|
||||
filename: v,
|
||||
path: `${ESTIMATES_FOLD}/${v}`,
|
||||
cdate: (await fs.stat(`${path}/${v}`)).ctime,
|
||||
} as FileProperties;
|
||||
}
|
||||
}))
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function getFile(user: User, filename: string): Promise<Uint8Array> {
|
||||
export async function getFile(user: User, filename: string) {
|
||||
const path = `${DOCUMENTS_PATH}/user-${user.id}`;
|
||||
|
||||
let file = Bun.file(`${path}/${filename}`)
|
||||
|
||||
if (!await file.exists()) {
|
||||
return new Promise(_ => null);
|
||||
return null;
|
||||
}
|
||||
|
||||
return file.bytes();
|
||||
return await file.bytes();
|
||||
}
|
||||
|
||||
export async function generateEstimatePDF(user: User, year: number, quarter: number) {
|
||||
|
|
@ -160,10 +134,10 @@ export async function generateRecordPDF(user: User, year: number, quarter: numbe
|
|||
return await _runProtocted(_generateRecordPDF, user, year, quarter);
|
||||
}
|
||||
|
||||
async function _runProtocted(f: Function, user: User, ...args: any[]) {
|
||||
async function _runProtocted(f, user: User, ...args) {
|
||||
|
||||
if (locked_users.has(user.id)) {
|
||||
throw new UserBusyException(`Cannot generate pdf for user: ` ,user.id);
|
||||
throw UserBusyException(`Cannot generate pdf (type: ${type})` ,user.id);
|
||||
}
|
||||
|
||||
locked_users.add(user.id);
|
||||
|
|
@ -234,7 +208,7 @@ async function _genLatexRec(user: User, file_pref: string, year: number, month:
|
|||
|
||||
if (estimate == null || isNaN(estimate)) {
|
||||
throw new Error("No estimate found")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: escape semicolon in comment
|
||||
|
||||
|
|
@ -292,7 +266,7 @@ async function _genLatexEst(user: User, file_pref: string, year: number, quarter
|
|||
csvfilewriter.write(`${SALUTATION[user.gender]} ${user.name};${user.address};Arbeitnehmer${GENDER_END[user.gender]};Teilzeitmitarbeiter${GENDER_END[user.gender]};${isoToLocalDate(new Date().toISOString())}\n`)
|
||||
|
||||
for (let i = 0; i < 3; ++i) {
|
||||
csvfilewriter.write(`${MONTHS[(quarter - 1) * 3 + i]} ${year};${(estimates as any)[`estimate_${i}`]}\n`);
|
||||
csvfilewriter.write(`${MONTHS[(quarter - 1) * 3 + i]} ${year};${estimates[`estimate_${i}`]}\n`);
|
||||
}
|
||||
|
||||
csvfilewriter.end();
|
||||
|
|
@ -300,7 +274,7 @@ async function _genLatexEst(user: User, file_pref: string, year: number, quarter
|
|||
const { stdout, exitCode } = await b.$`pdflatex -interaction=nonstopmode -halt-on-error -jobname=${file_pref} -output-format=pdf ${TEMPLATE_EST}`.cwd(dir).quiet().nothrow();
|
||||
|
||||
if (exitCode != 0) {
|
||||
throw new LatexException("Failed to create estimate PDF", user.id, exitCode, stdout.toString());
|
||||
throw new LatexException("Failed to create estimate PDF", user.id, exitCode, stdout);
|
||||
}
|
||||
|
||||
return `${dir}/${file_pref}.pdf`;
|
||||
|
|
|
|||
Loading…
Reference in New Issue