Add digital catalog with shareable flipbook and ktg.montzar.ir deploy files.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { isJpeg } from "./magic";
|
||||
|
||||
export class StorageError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "StorageError";
|
||||
}
|
||||
}
|
||||
|
||||
export function pageFileName(n: number): string {
|
||||
return `page-${String(n).padStart(3, "0")}.jpg`;
|
||||
}
|
||||
|
||||
export function catalogDir(storageDir: string, id: string): string {
|
||||
return path.join(storageDir, "catalogs", id);
|
||||
}
|
||||
|
||||
export async function saveCatalogPages(
|
||||
storageDir: string,
|
||||
id: string,
|
||||
pages: Buffer[],
|
||||
): Promise<void> {
|
||||
const dir = catalogDir(storageDir, id);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
let i = 1;
|
||||
for (const page of pages) {
|
||||
if (!isJpeg(page)) {
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
throw new StorageError("نوع فایل پشتیبانی نمیشود");
|
||||
}
|
||||
await fs.writeFile(path.join(dir, pageFileName(i)), page);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveQr(
|
||||
storageDir: string,
|
||||
id: string,
|
||||
png: Buffer,
|
||||
): Promise<void> {
|
||||
const dir = catalogDir(storageDir, id);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
await fs.writeFile(path.join(dir, "qr.png"), png);
|
||||
}
|
||||
|
||||
export async function readPage(
|
||||
storageDir: string,
|
||||
id: string,
|
||||
n: number,
|
||||
): Promise<Buffer> {
|
||||
const file = path.join(catalogDir(storageDir, id), pageFileName(n));
|
||||
try {
|
||||
return await fs.readFile(file);
|
||||
} catch {
|
||||
throw new StorageError("کاتالوگ پیدا نشد");
|
||||
}
|
||||
}
|
||||
|
||||
export async function readQr(storageDir: string, id: string): Promise<Buffer> {
|
||||
const file = path.join(catalogDir(storageDir, id), "qr.png");
|
||||
try {
|
||||
return await fs.readFile(file);
|
||||
} catch {
|
||||
throw new StorageError("کاتالوگ پیدا نشد");
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteCatalogFiles(
|
||||
storageDir: string,
|
||||
id: string,
|
||||
): Promise<void> {
|
||||
await fs.rm(catalogDir(storageDir, id), { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user