class FileService {
    download(file: File, name?: string) {
        const link = document.createElement('a');
        const fileUrl = URL.createObjectURL(file);

        link.download = name || file.name;
        link.href = fileUrl;
        link.click();

        URL.revokeObjectURL(fileUrl);
    }

    downloadByDisposition(disposition: string, blob: Blob) {
        const fileUrl = URL.createObjectURL(blob);
        const contentDisposition = disposition;
        const filename = contentDisposition.split('filename=')[1] as string;

        const downloadFileName = filename.slice(1, filename.length - 1);

        const link = document.createElement('a');
        link.download = downloadFileName;
        link.href = fileUrl;
        link.click();

        URL.revokeObjectURL(fileUrl);
    }
}

export const file_service = new FileService();
