fix download
This commit is contained in:
@@ -22,6 +22,7 @@ import FileModerationModal from './FileModerationModal';
|
||||
import { FileTypeIcon } from '@/app/components/FileTypeIcon';
|
||||
import { getFileType } from '@/app/lib/getFileType';
|
||||
import Image from 'next/image';
|
||||
import { downloadFile } from '@/app/actions/contentActions';
|
||||
|
||||
const cutFileExtension = (fileName: string) => {
|
||||
const lastDotIndex = fileName.lastIndexOf('.');
|
||||
@@ -134,29 +135,33 @@ export default function ContentModerationTable({ permission }: { permission: num
|
||||
}, []);
|
||||
|
||||
const handleDownload = async (file: any) => {
|
||||
setIsFileLoading(true);
|
||||
console.log(file.downloadUrl);
|
||||
setIsFileLoading(true)
|
||||
|
||||
try {
|
||||
const response = await fetch(file.downloadUrl);
|
||||
const result = await downloadFile(
|
||||
file.fileId,
|
||||
file.fileName || `file-${file.fileId}`
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Download failed');
|
||||
const byteCharacters = atob(result.data);
|
||||
const byteNumbers = new Array(byteCharacters.length);
|
||||
for (let i = 0; i < byteCharacters.length; i++) {
|
||||
byteNumbers[i] = byteCharacters.charCodeAt(i)
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const byteArray = new Uint8Array(byteNumbers);
|
||||
const blob = new Blob([byteArray], { type: result.contentType });
|
||||
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = file._original?.fileName || file.fileName || `file-${file.id}`;
|
||||
a.download = result.fileName;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
|
||||
toast.success(`${t('file-is-downloading')} - ${file.fileName}`);
|
||||
} catch (error) {
|
||||
console.error('Download error:', error);
|
||||
toast.error(t('failed-to-download-file'));
|
||||
} finally {
|
||||
setIsFileLoading(false);
|
||||
|
||||
Reference in New Issue
Block a user