fix download

This commit is contained in:
smanylov
2026-05-27 13:29:49 +07:00
parent df82b7e451
commit e4508cb788
3 changed files with 45 additions and 11 deletions
+24 -1
View File
@@ -1,7 +1,7 @@
'use server' 'use server'
import { getSessionData } from '@/app/actions/session'; import { getSessionData } from '@/app/actions/session';
import { API_BASE_URL } from '@/app/actions/definitions'; import { API_BASE_URL, API_DASHBOARD_URL } from '@/app/actions/definitions';
export async function fetchModerationContentList(page?: number, size?: number, sortBy?: string, sortDirection?: 'asc' | 'desc' | string) { export async function fetchModerationContentList(page?: number, size?: number, sortBy?: string, sortDirection?: 'asc' | 'desc' | string) {
const token = await getSessionData('token'); const token = await getSessionData('token');
@@ -82,4 +82,27 @@ export async function changeModerationContentStatus(fileId?: string, fileStatus?
} catch (error) { } catch (error) {
return null; return null;
} }
}
export async function downloadFile(fileId: string, fileName: string) {
const token = await getSessionData('token');
const response = await fetch(`${API_DASHBOARD_URL}/api/v1/files/download/${fileId}`, {
headers: {
'Authorization': `Bearer ${token}`,
},
})
if (!response.ok) {
throw 'failed-to-download-file';
}
const arrayBuffer = await response.arrayBuffer()
const base64 = Buffer.from(arrayBuffer).toString('base64')
return {
data: base64,
contentType: response.headers.get('content-type') || 'application/octet-stream',
fileName: fileName
}
} }
+7 -1
View File
@@ -18,4 +18,10 @@ export const localDevelopmentUrl = process.env.DEV_URL
export const API_BASE_URL = process.env.NODE_ENV === 'development' export const API_BASE_URL = process.env.NODE_ENV === 'development'
? localDevelopmentUrl ? localDevelopmentUrl
: 'http://admin:8082'; : 'http://admin:8082';
export const localDashBoardDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost';
export const API_DASHBOARD_URL = process.env.NODE_ENV === 'development'
? localDashBoardDevelopmentUrl
: 'http://app:8080';
@@ -22,6 +22,7 @@ import FileModerationModal from './FileModerationModal';
import { FileTypeIcon } from '@/app/components/FileTypeIcon'; import { FileTypeIcon } from '@/app/components/FileTypeIcon';
import { getFileType } from '@/app/lib/getFileType'; import { getFileType } from '@/app/lib/getFileType';
import Image from 'next/image'; import Image from 'next/image';
import { downloadFile } from '@/app/actions/contentActions';
const cutFileExtension = (fileName: string) => { const cutFileExtension = (fileName: string) => {
const lastDotIndex = fileName.lastIndexOf('.'); const lastDotIndex = fileName.lastIndexOf('.');
@@ -134,29 +135,33 @@ export default function ContentModerationTable({ permission }: { permission: num
}, []); }, []);
const handleDownload = async (file: any) => { const handleDownload = async (file: any) => {
setIsFileLoading(true); setIsFileLoading(true)
console.log(file.downloadUrl);
try { try {
const response = await fetch(file.downloadUrl); const result = await downloadFile(
file.fileId,
file.fileName || `file-${file.fileId}`
);
if (!response.ok) { const byteCharacters = atob(result.data);
throw new Error('Download failed'); 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 url = window.URL.createObjectURL(blob);
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
a.download = file._original?.fileName || file.fileName || `file-${file.id}`; a.download = result.fileName;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
window.URL.revokeObjectURL(url); window.URL.revokeObjectURL(url);
document.body.removeChild(a); document.body.removeChild(a);
toast.success(`${t('file-is-downloading')} - ${file.fileName}`); toast.success(`${t('file-is-downloading')} - ${file.fileName}`);
} catch (error) { } catch (error) {
console.error('Download error:', error);
toast.error(t('failed-to-download-file')); toast.error(t('failed-to-download-file'));
} finally { } finally {
setIsFileLoading(false); setIsFileLoading(false);