import * as Mime from 'mime-types';
import { randomUUID } from 'crypto';
getFilename(
basename: string,
type: string,
id: number | string, // так то number должен быть, но хз
mime: string,
): string {
if (!basename || basename.startsWith('.')) {
return this.createFilename(type, id, mime);
}
return this.getFilenameWithExtension(basename, mime);
}
private getFilenameWithExtension(basename: string, mime: string): string {
const extFromMime = this.getExtension(mime);
const parts = basename.split('.').filter((p) => p.length > 0);
if (parts.length > 1) {
return parts.join('.');
}
const namePart = parts[0] || this.getBasename();
return `${namePart}${extFromMime ? `.${extFromMime}` : ''}`;
}
private createFilename(
type: string,
id: number | string,
mime: string,
): string {
const basename = this.getBasename(type, id);
return this.getFilenameWithExtension(basename, mime);
}
private getBasename(type?: string, id?: number | string): string {
if (type && id) {
return `${type}-${id}`;
}
const baseId = randomUUID();
if (type) {
return `${type}-${baseId}`;
}
return `${baseId}`;
}
private getExtension(mime: string): string {
const ext = Mime.extension(mime);
return ext || '';
}
// usage
coreMessage.filename = this.getFilename(
storeResponse.filename,
attach._type,
attach.fileId,
storeResponse.contentType,
);
Last active
October 21, 2025 14:51
-
-
Save Kirill255/b0abfb7e83c6c5d5c52fb432512dac1e to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment