Sortieungsoptionen in Verwaltung, Umstellung der PDF generation auf generierung bei abgabe und ablage auf dem Sateisystem um einen Festen Stand zu garantieren
This commit is contained in:
44
services/pdf-storage-service.js
Normal file
44
services/pdf-storage-service.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const fs = require('fs');
|
||||
const fsp = require('fs/promises');
|
||||
const path = require('path');
|
||||
const { buildTimesheetPdfLocation, getPdfBaseDir } = require('../helpers/pdf-paths');
|
||||
|
||||
async function ensureDirectoryExists(dirPath) {
|
||||
await fsp.mkdir(dirPath, { recursive: true });
|
||||
}
|
||||
|
||||
async function fileExists(filePath) {
|
||||
try {
|
||||
await fsp.access(filePath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function savePdfBufferAtomic(targetPath, buffer) {
|
||||
const dir = path.dirname(targetPath);
|
||||
await ensureDirectoryExists(dir);
|
||||
|
||||
const tmpPath = `${targetPath}.tmp-${process.pid}-${Date.now()}`;
|
||||
await fsp.writeFile(tmpPath, buffer);
|
||||
await fsp.rename(tmpPath, targetPath);
|
||||
}
|
||||
|
||||
function resolvePdfLocationFromTimesheetRow(timesheetRow) {
|
||||
return buildTimesheetPdfLocation(timesheetRow);
|
||||
}
|
||||
|
||||
function resolveAbsolutePathFromRelative(relativePath) {
|
||||
const baseDir = getPdfBaseDir();
|
||||
return path.join(baseDir, relativePath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ensureDirectoryExists,
|
||||
fileExists,
|
||||
savePdfBufferAtomic,
|
||||
resolvePdfLocationFromTimesheetRow,
|
||||
resolveAbsolutePathFromRelative,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user