legacyAttachments.js 2.2 KB

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