import { ActionState } from '@internxt/inxt-js/build/api/ActionState' ; import analyticsService from 'app/analytics/services/analytics.service' ; import localStorageService from 'app/core/services/local-storage.service' ; import { DevicePlatform } from 'app/core/types' ; import { DriveFileData } from '../../types' ; import downloadFileFromBlob from './downloadFileFromBlob' ; import fetchFileBlob from './fetchFileBlob' ; const trackFileDownloadStart = ( userEmail , file_id , file_name , file_size , file_type , folder_id ) => { const data = { file_id , file_name , file_size , file_type , email : userEmail , folder_id , platform : DevicePlatform . Web } ; analyticsService . trackFileDownloadStart ( data ) ; } ; const trackFileDownloadError = ( userEmail , file_id , msg ) => { const data = { file_id , email : userEmail , msg , platform : DevicePlatform . Web } ; analyticsService . trackFileDownloadError ( data ) ; } ; export default function downloadFile ( itemData , isTeam , updateProgressCallback ) { const userEmail = localStorageService . getUser ( ) ?. email || '' ; const fileId = itemData . fileId ; const completeFilename = itemData . type ? `${ itemData . name }.${ itemData . type }` : `${ itemData . name }` ; trackFileDownloadStart ( userEmail , fileId , itemData . name , itemData . size , itemData . type , itemData . folderId ) ; const [ blobPromise , actionState ] = fetchFileBlob ( fileId , { isTeam , updateProgressCallback } ) ; const fileBlobPromise = blobPromise . then ( ( fileBlob ) => { downloadFileFromBlob ( fileBlob , completeFilename ) ; analyticsService . trackFileDownloadCompleted ( { size : itemData . size , extension : itemData . type , } ) ; } ) . catch ( ( err ) => { const errMessage = err instanceof Error ? err . message : ( err as string ) ; trackFileDownloadError ( userEmail , fileId , errMessage ) ; throw new Error ( errMessage ) ; } ) ; return [ fileBlobPromise . then ( ) , actionState ] ; }