diff --git a/src/app/actions/contentActions.ts b/src/app/actions/contentActions.ts index a8015ef..18e1fd8 100644 --- a/src/app/actions/contentActions.ts +++ b/src/app/actions/contentActions.ts @@ -1,7 +1,7 @@ 'use server' 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) { const token = await getSessionData('token'); @@ -82,4 +82,27 @@ export async function changeModerationContentStatus(fileId?: string, fileStatus? } catch (error) { 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 + } } \ No newline at end of file diff --git a/src/app/actions/definitions.ts b/src/app/actions/definitions.ts index dadb7b4..f88e427 100644 --- a/src/app/actions/definitions.ts +++ b/src/app/actions/definitions.ts @@ -18,4 +18,10 @@ export const localDevelopmentUrl = process.env.DEV_URL export const API_BASE_URL = process.env.NODE_ENV === 'development' ? localDevelopmentUrl - : 'http://admin:8082'; \ No newline at end of file + : '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'; \ No newline at end of file diff --git a/src/app/ui/content/content-moderation-table.tsx b/src/app/ui/content/content-moderation-table.tsx index c430e1f..05b1e94 100644 --- a/src/app/ui/content/content-moderation-table.tsx +++ b/src/app/ui/content/content-moderation-table.tsx @@ -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);