Files
ihaleist/server/document-metadata.mjs

30 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function decodeDocumentUpload(value) {
if (typeof value !== 'string' || !value.length || value.length % 4 !== 0 ||
!/^[A-Za-z0-9+/]*={0,2}$/.test(value))
throw new Error('Dosya içeriği geçersiz; dosyayı yeniden seçin.');
const bytes = Buffer.from(value, 'base64');
if (!bytes.length || bytes.toString('base64') !== value) throw new Error('Dosya içeriği boş veya bozuk.');
if (bytes.length > 10 * 1024 * 1024) throw new Error('Dosya en fazla 10 MB olabilir.');
return bytes;
}
export function documentMetadataPatch(body) {
const patch = {};
if (Object.hasOwn(body, 'sonKullanma')) {
const value = body.sonKullanma;
if (value !== null && value !== '') {
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value))
throw new Error('Son kullanma tarihi YYYY-AA-GG biçiminde olmalı.');
const date = new Date(value + 'T00:00:00Z');
if (!Number.isFinite(date.getTime()) || date.toISOString().slice(0,10) !== value)
throw new Error('Son kullanma tarihi geçerli bir takvim günü olmalı.');
}
patch.sonKullanma = value || null;
}
if (Object.hasOwn(body, 'kategori')) {
if (body.kategori !== null && typeof body.kategori !== 'string') throw new Error('Belge kategorisi metin olmalı.');
patch.kategori = body.kategori?.trim().slice(0,60) || null;
}
return patch;
}