fix and update
This commit is contained in:
76
apps/frontend/lib/document-attachments.ts
Normal file
76
apps/frontend/lib/document-attachments.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
export type DocumentAttachmentMeta = {
|
||||
fileName?: string;
|
||||
contentType?: string;
|
||||
};
|
||||
|
||||
export const DOCUMENT_FILE_ACCEPT =
|
||||
'image/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.rtf,.odt,application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
|
||||
const EXTENSION_TO_MIME: Record<string, string> = {
|
||||
jpg: 'image/jpeg',
|
||||
jpeg: 'image/jpeg',
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
gif: 'image/gif',
|
||||
pdf: 'application/pdf',
|
||||
doc: 'application/msword',
|
||||
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
xls: 'application/vnd.ms-excel',
|
||||
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
ppt: 'application/vnd.ms-powerpoint',
|
||||
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
txt: 'text/plain',
|
||||
rtf: 'application/rtf',
|
||||
odt: 'application/vnd.oasis.opendocument.text'
|
||||
};
|
||||
|
||||
export function resolveDocumentFileContentType(file: File) {
|
||||
const fromType = file.type?.trim();
|
||||
if (fromType && fromType !== 'application/octet-stream') {
|
||||
return fromType;
|
||||
}
|
||||
const extension = file.name.split('.').pop()?.toLowerCase() ?? '';
|
||||
return EXTENSION_TO_MIME[extension] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
export function inferAttachmentContentType(storageKey: string, meta?: DocumentAttachmentMeta) {
|
||||
if (meta?.contentType) return meta.contentType;
|
||||
const extension = storageKey.split('.').pop()?.toLowerCase() ?? '';
|
||||
return EXTENSION_TO_MIME[extension] ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
export function isImageAttachment(contentType: string) {
|
||||
return contentType.startsWith('image/');
|
||||
}
|
||||
|
||||
export function isPdfAttachment(contentType: string) {
|
||||
return contentType === 'application/pdf';
|
||||
}
|
||||
|
||||
export function attachmentKindLabel(contentType: string) {
|
||||
if (isImageAttachment(contentType)) return 'Изображение';
|
||||
if (isPdfAttachment(contentType)) return 'PDF';
|
||||
if (contentType.includes('word') || contentType === 'application/msword') return 'Word';
|
||||
if (contentType.includes('excel') || contentType === 'application/vnd.ms-excel') return 'Excel';
|
||||
if (contentType.includes('powerpoint') || contentType === 'application/vnd.ms-powerpoint') return 'PowerPoint';
|
||||
if (contentType === 'text/plain') return 'Текст';
|
||||
return 'Файл';
|
||||
}
|
||||
|
||||
export function attachmentDisplayName(storageKey: string, meta?: DocumentAttachmentMeta) {
|
||||
if (meta?.fileName?.trim()) return meta.fileName.trim();
|
||||
const parts = storageKey.split('/');
|
||||
return parts[parts.length - 1] ?? 'Файл';
|
||||
}
|
||||
|
||||
export function attachmentFileExtension(contentType: string, fileName?: string) {
|
||||
const fromName = fileName?.split('.').pop()?.toUpperCase();
|
||||
if (fromName && fromName.length <= 5) return fromName;
|
||||
if (isPdfAttachment(contentType)) return 'PDF';
|
||||
if (contentType.includes('word')) return 'DOC';
|
||||
if (contentType.includes('excel')) return 'XLS';
|
||||
if (contentType.includes('powerpoint')) return 'PPT';
|
||||
if (contentType === 'text/plain') return 'TXT';
|
||||
if (isImageAttachment(contentType)) return 'IMG';
|
||||
return 'FILE';
|
||||
}
|
||||
Reference in New Issue
Block a user