mirror of
https://github.com/wekan/wekan.git
synced 2025-12-22 18:30:13 +01:00
Use attachments from old CollectionFS database structure, when not yet migrated to Meteor-Files/ostrio-files, without needing to migrate database structure.
Thanks to xet7 !
This commit is contained in:
parent
dda013844c
commit
a8de2f224f
6 changed files with 698 additions and 2 deletions
72
server/routes/legacyAttachments.js
Normal file
72
server/routes/legacyAttachments.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import { Meteor } from 'meteor/meteor';
|
||||
import { WebApp } from 'meteor/webapp';
|
||||
import { ReactiveCache } from '/imports/reactiveCache';
|
||||
import { getAttachmentWithBackwardCompatibility, getOldAttachmentStream } from '/models/lib/attachmentBackwardCompatibility';
|
||||
|
||||
// Ensure this file is loaded
|
||||
console.log('Legacy attachments route loaded');
|
||||
|
||||
/**
|
||||
* Legacy attachment download route for CollectionFS compatibility
|
||||
* Handles downloads from old CollectionFS structure
|
||||
*/
|
||||
|
||||
if (Meteor.isServer) {
|
||||
// Handle legacy attachment downloads
|
||||
WebApp.connectHandlers.use('/cfs/files/attachments', (req, res, next) => {
|
||||
const attachmentId = req.url.split('/').pop();
|
||||
|
||||
if (!attachmentId) {
|
||||
res.writeHead(404);
|
||||
res.end('Attachment not found');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Try to get attachment with backward compatibility
|
||||
const attachment = getAttachmentWithBackwardCompatibility(attachmentId);
|
||||
|
||||
if (!attachment) {
|
||||
res.writeHead(404);
|
||||
res.end('Attachment not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check permissions
|
||||
const board = ReactiveCache.getBoard(attachment.meta.boardId);
|
||||
if (!board) {
|
||||
res.writeHead(404);
|
||||
res.end('Board not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if user has permission to download
|
||||
const userId = Meteor.userId();
|
||||
if (!board.isPublic() && (!userId || !board.hasMember(userId))) {
|
||||
res.writeHead(403);
|
||||
res.end('Access denied');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set appropriate headers
|
||||
res.setHeader('Content-Type', attachment.type || 'application/octet-stream');
|
||||
res.setHeader('Content-Length', attachment.size || 0);
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${attachment.name}"`);
|
||||
|
||||
// Get GridFS stream for legacy attachment
|
||||
const fileStream = getOldAttachmentStream(attachmentId);
|
||||
if (fileStream) {
|
||||
res.writeHead(200);
|
||||
fileStream.pipe(res);
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end('File not found in GridFS');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error serving legacy attachment:', error);
|
||||
res.writeHead(500);
|
||||
res.end('Internal server error');
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue