Skip to content

Commit

Permalink
fix file downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
RealFascinated committed Mar 7, 2025
1 parent 88388bb commit e2fbed4
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/app/(pages)/(app)/(files)/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,30 @@ async function getFileMetadata(
"Content-Type": mimeType,
});

// Add specific headers based on file type
if (isVideo) {
headers.set("Accept-Ranges", "bytes");
} else if (isImage) {
headers.set("Cache-Control", "public, max-age=43200"); // 12 hours
}

// Download file, or is not image and is not video
if (download || (!isVideo && !isImage)) {
// Download file
if (download) {
headers.set(
"Content-Disposition",
`inline; filename="${getFileName(fileMeta)}"`
`attachment; filename="${getFileName(fileMeta)}"`
);
// Prevent caching for downloads
headers.set("Cache-Control", "no-store, no-cache, must-revalidate");
// Stream file
} else {
// Add specific headers based on file type
if (isVideo) {
headers.set("Accept-Ranges", "bytes");
} else if (isImage) {
headers.set("Cache-Control", "public, max-age=43200"); // 12 hours
}

// Download file, or is not image and is not video
if (!isVideo && !isImage) {
headers.set(
"Content-Disposition",
`inline; filename="${getFileName(fileMeta)}"`
);
// Prevent caching for downloads
headers.set("Cache-Control", "no-store, no-cache, must-revalidate");
}
}

return { fileMeta, user, isVideo, isImage, headers };
Expand Down

0 comments on commit e2fbed4

Please sign in to comment.