legacyAttachments.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Meteor } from 'meteor/meteor';
  2. import { WebApp } from 'meteor/webapp';
  3. import { ReactiveCache } from '/imports/reactiveCache';
  4. import { getAttachmentWithBackwardCompatibility, getOldAttachmentStream } from '/models/lib/attachmentBackwardCompatibility';
  5. // Ensure this file is loaded
  6. console.log('Legacy attachments route loaded');
  7. /**
  8. * Legacy attachment download route for CollectionFS compatibility
  9. * Handles downloads from old CollectionFS structure
  10. */
  11. if (Meteor.isServer) {
  12. // Handle legacy attachment downloads
  13. WebApp.connectHandlers.use('/cfs/files/attachments', (req, res, next) => {
  14. const attachmentId = req.url.split('/').pop();
  15. if (!attachmentId) {
  16. res.writeHead(404);
  17. res.end('Attachment not found');
  18. return;
  19. }
  20. try {
  21. // Try to get attachment with backward compatibility
  22. const attachment = getAttachmentWithBackwardCompatibility(attachmentId);
  23. if (!attachment) {
  24. res.writeHead(404);
  25. res.end('Attachment not found');
  26. return;
  27. }
  28. // Check permissions
  29. const board = ReactiveCache.getBoard(attachment.meta.boardId);
  30. if (!board) {
  31. res.writeHead(404);
  32. res.end('Board not found');
  33. return;
  34. }
  35. // Check if user has permission to download
  36. const userId = Meteor.userId();
  37. if (!board.isPublic() && (!userId || !board.hasMember(userId))) {
  38. res.writeHead(403);
  39. res.end('Access denied');
  40. return;
  41. }
  42. // Set appropriate headers
  43. res.setHeader('Content-Type', attachment.type || 'application/octet-stream');
  44. res.setHeader('Content-Length', attachment.size || 0);
  45. res.setHeader('Content-Disposition', `attachment; filename="${attachment.name}"`);
  46. // Get GridFS stream for legacy attachment
  47. const fileStream = getOldAttachmentStream(attachmentId);
  48. if (fileStream) {
  49. res.writeHead(200);
  50. fileStream.pipe(res);
  51. } else {
  52. res.writeHead(404);
  53. res.end('File not found in GridFS');
  54. }
  55. } catch (error) {
  56. console.error('Error serving legacy attachment:', error);
  57. res.writeHead(500);
  58. res.end('Internal server error');
  59. }
  60. });
  61. }