🐛 Fix custom download file helper with safari fallback (#751)
This commit is contained in:
@@ -17,6 +17,8 @@ import AlertManager from 'app/features/global/services/alert-manager-service';
|
|||||||
import FeatureTogglesService, {
|
import FeatureTogglesService, {
|
||||||
FeatureNames,
|
FeatureNames,
|
||||||
} from '@features/global/services/feature-toggles-service';
|
} from '@features/global/services/feature-toggles-service';
|
||||||
|
import Logger from '@features/global/framework/logger-service';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the children of a drive item
|
* Returns the children of a drive item
|
||||||
* @returns
|
* @returns
|
||||||
@@ -25,10 +27,38 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
|
|||||||
const companyId = useRouterCompany();
|
const companyId = useRouterCompany();
|
||||||
const sharedFilter = useRecoilValue(SharedWithMeFilterState);
|
const sharedFilter = useRecoilValue(SharedWithMeFilterState);
|
||||||
const sortItem = useRecoilValue(DriveItemSort);
|
const sortItem = useRecoilValue(DriveItemSort);
|
||||||
const [ paginateItem ] = useRecoilState(DriveItemPagination);
|
const [paginateItem] = useRecoilState(DriveItemPagination);
|
||||||
const { getQuota } = useUserQuota();
|
const { getQuota } = useUserQuota();
|
||||||
const AVEnabled = FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_AV_ENABLED);
|
const AVEnabled = FeatureTogglesService.isActiveFeatureName(FeatureNames.COMPANY_AV_ENABLED);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads a file from the given URL, ensuring compatibility across all browsers, including Safari.
|
||||||
|
*
|
||||||
|
* @param fileUrl - The URL of the file to download.
|
||||||
|
*/
|
||||||
|
const downloadFile = (fileUrl: string) => {
|
||||||
|
try {
|
||||||
|
// Attempt to open the URL in a new tab
|
||||||
|
const popupWindow = window.open(fileUrl, '_blank');
|
||||||
|
|
||||||
|
if (popupWindow) {
|
||||||
|
popupWindow.focus();
|
||||||
|
} else {
|
||||||
|
// Fallback for Safari or blocked pop-ups
|
||||||
|
const downloadLink = document.createElement('a');
|
||||||
|
downloadLink.href = fileUrl;
|
||||||
|
downloadLink.download = ''; // Suggests a download instead of navigation
|
||||||
|
downloadLink.target = '_self'; // Ensures it opens in the same tab
|
||||||
|
|
||||||
|
document.body.appendChild(downloadLink);
|
||||||
|
downloadLink.click();
|
||||||
|
document.body.removeChild(downloadLink);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error('Error during file download:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const refresh = useRecoilCallback(
|
const refresh = useRecoilCallback(
|
||||||
({ set, snapshot }) =>
|
({ set, snapshot }) =>
|
||||||
async (parentId: string, resetPagination?: boolean) => {
|
async (parentId: string, resetPagination?: boolean) => {
|
||||||
@@ -112,7 +142,7 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
|
|||||||
// toggle confirm for user
|
// toggle confirm for user
|
||||||
AlertManager.confirm(
|
AlertManager.confirm(
|
||||||
() => {
|
() => {
|
||||||
(window as any).open(url, '_blank').focus();
|
downloadFile(url);
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
return;
|
return;
|
||||||
@@ -122,10 +152,10 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
(window as any).open(url, '_blank').focus();
|
downloadFile(url);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(window as any).open(url, '_blank').focus();
|
downloadFile(url);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_download_file'));
|
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_download_file'));
|
||||||
@@ -139,7 +169,7 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
|
|||||||
try {
|
try {
|
||||||
const triggerDownload = async () => {
|
const triggerDownload = async () => {
|
||||||
const url = await DriveApiClient.getDownloadZipUrl(companyId, ids, isDirectory);
|
const url = await DriveApiClient.getDownloadZipUrl(companyId, ids, isDirectory);
|
||||||
(window as any).open(url, '_blank').focus();
|
downloadFile(url);
|
||||||
};
|
};
|
||||||
if (AVEnabled) {
|
if (AVEnabled) {
|
||||||
const containsMaliciousFiles =
|
const containsMaliciousFiles =
|
||||||
@@ -254,7 +284,7 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
|
|||||||
},
|
},
|
||||||
[paginateItem, refresh],
|
[paginateItem, refresh],
|
||||||
);
|
);
|
||||||
|
|
||||||
const checkMalware = useCallback(
|
const checkMalware = useCallback(
|
||||||
async (item: Partial<DriveItem>) => {
|
async (item: Partial<DriveItem>) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user