update download function
This commit is contained in:
@@ -27,6 +27,10 @@ import { fetchReferralUserStats } from '@/app/actions/referralsActions';
|
||||
import { MonitoringDropDown } from '@/app/components/tanstak-table/MonitoringDropDown';
|
||||
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
|
||||
import { useViewport } from '@/app/hooks/useViewport';
|
||||
import { API_BASE_URL } from '@/app/actions/definitions';
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
|
||||
import { downloadFile } from '@/app/actions/fileEntity'
|
||||
|
||||
type FileType = 'image' | 'video' | 'audio';
|
||||
|
||||
@@ -482,31 +486,38 @@ export default function TanstakFilesTable({ fileType }: { fileType: string }) {
|
||||
};
|
||||
|
||||
const handleDownload = async (file: FileItem) => {
|
||||
setIsFileLoading(true);
|
||||
setIsFileLoading(true)
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/download/${file.id}`);
|
||||
const result = await downloadFile(
|
||||
file.id,
|
||||
file._original?.fileName || file.fileName || `file-${file.id}`
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`error: ${response.status}`);
|
||||
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) {
|
||||
toast.error(t('failed-to-download-file'))
|
||||
toast.error(t('failed-to-download-file'));
|
||||
} finally {
|
||||
setIsFileLoading(false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const handleMonitoring = async (file: FileItem) => {
|
||||
console.log(`Мониторинг:`, file);
|
||||
|
||||
Reference in New Issue
Block a user