fix: Améliorer logique de téléchargement .txt avec logs debug

This commit is contained in:
odentas 2025-12-10 15:49:46 +01:00
parent 8bf19016a9
commit 1954d90255
2 changed files with 45 additions and 31 deletions

View file

@ -459,43 +459,46 @@ function SectionComptables() {
}
const handleDownload = async (item: DocumentItem, period: string) => {
// Chercher l'URL dans les données chargées
// Vérifier si c'est un fichier .txt
const isTxtFile = item.title.toLowerCase().endsWith('.txt')
if (isTxtFile) {
// Forcer le téléchargement pour les fichiers .txt via notre API
try {
// Utiliser le storage_path depuis les métadonnées de l'item
const storagePath = item.meta?.storage_path
if (!storagePath) {
console.error('Storage path non disponible pour:', item)
alert('Chemin de stockage non disponible')
return
}
console.log('Téléchargement du fichier .txt:', item.title, 'Path:', storagePath)
const downloadUrl = `/api/documents/download?path=${encodeURIComponent(storagePath)}`
const link = document.createElement('a')
link.href = downloadUrl
link.download = item.title
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch (error) {
console.error('Erreur lors du téléchargement:', error)
alert('Erreur lors du téléchargement du fichier')
}
return
}
// Pour les autres fichiers (PDF, etc.), chercher l'URL pré-signée
const docsWithUrls = periodUrls[period]
if (docsWithUrls) {
const docWithUrl = docsWithUrls.find(d => d.id === item.id)
if (docWithUrl?.url) {
// Vérifier si c'est un fichier .txt
const isTxtFile = item.title.toLowerCase().endsWith('.txt')
if (isTxtFile) {
// Forcer le téléchargement pour les fichiers .txt via notre API
try {
// Utiliser le storage_path depuis les métadonnées
const storagePath = docWithUrl.meta?.storage_path || item.meta?.storage_path
if (!storagePath) {
alert('Chemin de stockage non disponible')
return
}
const downloadUrl = `/api/documents/download?path=${encodeURIComponent(storagePath)}`
const link = document.createElement('a')
link.href = downloadUrl
link.download = item.title
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch (error) {
console.error('Erreur lors du téléchargement:', error)
alert('Erreur lors du téléchargement du fichier')
}
} else {
// Ouvrir dans un nouvel onglet pour les autres fichiers (PDF, etc.)
window.open(docWithUrl.url, '_blank')
}
window.open(docWithUrl.url, '_blank')
return
}
}
alert('Document non disponible')
}

View file

@ -21,13 +21,17 @@ export async function GET(request: NextRequest) {
// Vérifier l'authentification
const { data: { session } } = await supabase.auth.getSession()
if (!session) {
console.error('📥 Download API - No session')
return new NextResponse('Unauthorized', { status: 401 })
}
const searchParams = request.nextUrl.searchParams
const path = searchParams.get('path')
console.log('📥 Download API - Request for path:', path)
if (!path) {
console.error('📥 Download API - Missing path parameter')
return new NextResponse('Missing path parameter', { status: 400 })
}
@ -37,9 +41,12 @@ export async function GET(request: NextRequest) {
Key: path,
})
console.log('📥 Download API - Fetching from S3, bucket:', process.env.AWS_S3_BUCKET_NAME, 'key:', path)
const response = await s3Client.send(command)
if (!response.Body) {
console.error('📥 Download API - File not found in S3')
return new NextResponse('File not found', { status: 404 })
}
@ -50,9 +57,13 @@ export async function GET(request: NextRequest) {
}
const buffer = Buffer.concat(chunks)
console.log('📥 Download API - File downloaded, size:', buffer.length, 'bytes')
// Extraire le nom du fichier depuis le path
const filename = path.split('/').pop() || 'document.txt'
console.log('📥 Download API - Sending file:', filename)
// Retourner le fichier avec les bons headers pour forcer le téléchargement
return new NextResponse(buffer, {
headers: {
@ -61,7 +72,7 @@ export async function GET(request: NextRequest) {
},
})
} catch (error) {
console.error('Error in download route:', error)
console.error('📥 Download API - Error:', error)
return new NextResponse('Internal Server Error', { status: 500 })
}
}