mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Several issues in the bulk move: - The GridFS source matcher required `meta.gridFsFileId` to be *absent*, but Meteor-Files always sets it, so selecting "MongoDB Meteor-Files" matched zero files and "nothing happened". The matcher now recognizes real GridFS files (`versions.*.storage === 'gridfs'` or a `meta.gridFsFileId` reference), and a "nothing to move" message is shown when a source is empty instead of silently doing nothing. - Moving files crashed the whole server with `FilesCollection#findOne() not available in server` — `ReactiveCache.getAttachment` used a synchronous `findOne()`; it now uses `findOneAsync()`. The background job is also hardened so a single failing file is skipped instead of crashing the server via an unhandled rejection. - The attachment **copy** API now honours the admin API upload limits (it previously skipped them). - **Fixed the "MongoDB Meteor-Files" file-count statistic** in Admin Panel / Attachments, which counted *every* attachment metadata document (so files on the Filesystem were wrongly reported as being in GridFS). It now counts only attachments actually stored in GridFS, consistent with the move tool. Also renamed the mislabeled "Mongo-Files" column to "Meteor-Files". Thanks to Claude. - **Unified attachment/avatar storage migration: move any → any.** Admin Panel / Attachments / Move Attachment can now move **Attachments, Avatars, or both**, from **any source to any destination** across Filesystem, Meteor-Files GridFS, Cloud (S3/Azure/GCS) and legacy CollectionFS GridFS. The default source is "All Read-enabled storages" (every backend whose Read flag is enabled and whose settings work), so everything can be consolidated into one destination in a single run. All metadata is preserved (board / swimlane / list / card / user / uploaded date / name / type / size), attachment cover references (`cards.coverId`) are remapped when an id changes, and the legacy source is deleted only after the new copy is verified. Thanks to Claude. - **Legacy CollectionFS GridFS is now a first-class storage backend** (read, migrate-from, and export-to) for both attachments and avatars, via the new `models/lib/collectionFsStore.js` (binary keyed by `copies.<coll>.key` in the `cfs_gridfs.<coll>` bucket, metadata in `cfs.<coll>.filerecord`). Thanks to Claude. - **The storage strategy layer is now collection-aware.** `moveToStorage` and the GridFS/Cloud strategies previously hard-coded the `Attachments` collection, so moving avatars to GridFS/cloud would have updated the wrong collection. The factory now carries its collection (`Attachments` or `Avatars`), so **avatars can be stored in Meteor-Files GridFS and cloud**, not only on the filesystem. Attachment behavior is unchanged. Thanks to Claude. - **Read legacy CollectionFS attachments and avatars in place** (without migrating). The backward-compatibility layer (`models/lib/attachmentBackwardCompatibility.js`) was broken — it looked up the GridFS binary by the filerecord `_id` and by filename instead of by `ObjectId(copies.<coll>.key)`, so legacy files were never found. It is fixed and generalized for attachments and avatars. Legacy attachments now appear in the card attachment gallery (new `legacyBoardAttachments` publication) and stream from the `cfs_gridfs.attachments` bucket, and legacy avatars (`/cfs/files/avatars/<id>` URLs) are served from the `cfs_gridfs.avatars` bucket instead of redirecting to a 404. Thanks to Claude.
349 lines
11 KiB
JavaScript
349 lines
11 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo, MongoInternals } from 'meteor/mongo';
|
|
// Import Attachments directly to avoid relying on an implicit global that
|
|
// does not exist in Meteor's ES-module scope.
|
|
import Attachments from '../attachments';
|
|
|
|
/**
|
|
* Backward compatibility layer for CollectionFS to Meteor-Files migration
|
|
* Handles reading attachments from old CollectionFS database structure
|
|
*/
|
|
|
|
// Old CollectionFS collections
|
|
const OldAttachmentsFiles = new Mongo.Collection('cfs_gridfs.attachments.files');
|
|
const OldAttachmentsFileRecord = new Mongo.Collection('cfs.attachments.filerecord');
|
|
const OldAvatarsFiles = new Mongo.Collection('cfs_gridfs.avatars.files');
|
|
const OldAvatarsFileRecord = new Mongo.Collection('cfs.avatars.filerecord');
|
|
|
|
function oldCollections(coll) {
|
|
return coll === 'avatars'
|
|
? { FileRecord: OldAvatarsFileRecord, Files: OldAvatarsFiles }
|
|
: { FileRecord: OldAttachmentsFileRecord, Files: OldAttachmentsFiles };
|
|
}
|
|
|
|
// The GridFS binary is keyed by copies.<coll>.key (an ObjectId hex), NOT by the
|
|
// filerecord _id. Resolve it safely to an ObjectId.
|
|
function gridFsObjectIdFromRecord(coll, fileRecord) {
|
|
const key = fileRecord && fileRecord.copies && fileRecord.copies[coll] && fileRecord.copies[coll].key;
|
|
if (!key) return null;
|
|
try {
|
|
return new MongoInternals.NpmModule.ObjectId(String(key));
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if an attachment exists in the new Meteor-Files structure
|
|
* @param {string} attachmentId - The attachment ID to check
|
|
* @returns {Promise<boolean>} - True if exists in new structure
|
|
*/
|
|
export async function isNewAttachmentStructure(attachmentId) {
|
|
if (Meteor.isServer) {
|
|
if (Attachments && Attachments.collection) {
|
|
const cursor = Attachments.collection;
|
|
return !!(typeof cursor.findOneAsync === 'function' ? await cursor.findOneAsync({ _id: attachmentId }) : cursor.findOne({ _id: attachmentId }));
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get attachment data from old CollectionFS structure
|
|
* @param {string} attachmentId - The attachment ID
|
|
* @returns {Promise<Object|null>} - Attachment data in new format or null if not found
|
|
*/
|
|
export async function getOldAttachmentData(attachmentId, coll = 'attachments') {
|
|
if (Meteor.isServer) {
|
|
try {
|
|
const { FileRecord, Files } = oldCollections(coll);
|
|
// Look up the filerecord (metadata) by its _id.
|
|
const fileRecord = await FileRecord.findOneAsync({ _id: attachmentId });
|
|
if (!fileRecord) {
|
|
return null;
|
|
}
|
|
|
|
// The binary lives in the cfs_gridfs.<coll> bucket keyed by
|
|
// copies.<coll>.key (an ObjectId), NOT by the filerecord _id.
|
|
const gridFsId = gridFsObjectIdFromRecord(coll, fileRecord);
|
|
let fileData = null;
|
|
if (gridFsId) {
|
|
fileData = await Files.findOneAsync({ _id: gridFsId });
|
|
}
|
|
|
|
const copy = (fileRecord.copies && fileRecord.copies[coll]) || {};
|
|
const name = fileRecord.original?.name || copy.name || fileData?.filename || 'Unknown';
|
|
const type = fileRecord.original?.type || copy.type || fileData?.contentType || 'application/octet-stream';
|
|
const size = fileRecord.original?.size || copy.size || fileData?.length || 0;
|
|
|
|
// Convert old structure to a Meteor-Files-shaped document. `meta.source`
|
|
// is 'legacy' so the file server streams it from the CollectionFS bucket,
|
|
// and `gridFsKey` lets the stream helper find the binary without a second
|
|
// filerecord lookup.
|
|
const convertedAttachment = {
|
|
_id: attachmentId,
|
|
name,
|
|
size,
|
|
type,
|
|
extension: getFileExtension(name),
|
|
extensionWithDot: getFileExtensionWithDot(name),
|
|
meta: {
|
|
boardId: fileRecord.boardId,
|
|
swimlaneId: fileRecord.swimlaneId,
|
|
listId: fileRecord.listId,
|
|
cardId: fileRecord.cardId,
|
|
userId: fileRecord.userId,
|
|
source: 'legacy',
|
|
},
|
|
userId: fileRecord.userId,
|
|
gridFsKey: gridFsId ? gridFsId.toString() : null,
|
|
collectionFsColl: coll,
|
|
uploadedAt: fileRecord.uploadedAt || fileData?.uploadDate || new Date(),
|
|
updatedAt: fileRecord.original?.updatedAt || fileData?.uploadDate || new Date(),
|
|
uploadedAtOstrio: fileRecord.uploadedAt || fileData?.uploadDate || new Date(),
|
|
isImage: isImageFile(type),
|
|
isVideo: isVideoFile(type),
|
|
isAudio: isAudioFile(type),
|
|
isText: isTextFile(type),
|
|
isJSON: isJSONFile(type),
|
|
isPDF: isPDFFile(type),
|
|
_downloadRoute: '/cdn/storage',
|
|
_collectionName: coll,
|
|
link(version = 'original') {
|
|
return `/cdn/storage/${coll}/${this._id}`;
|
|
},
|
|
versions: {
|
|
original: {
|
|
path: `/cdn/storage/${coll}/${attachmentId}`,
|
|
size,
|
|
type,
|
|
storage: 'gridfs',
|
|
},
|
|
},
|
|
};
|
|
|
|
return convertedAttachment;
|
|
} catch (error) {
|
|
console.error('Error reading old attachment data:', error);
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get file extension from filename
|
|
* @param {string} filename - The filename
|
|
* @returns {string} - File extension without dot
|
|
*/
|
|
function getFileExtension(filename) {
|
|
if (!filename) return '';
|
|
const lastDot = filename.lastIndexOf('.');
|
|
if (lastDot === -1) return '';
|
|
return filename.substring(lastDot + 1).toLowerCase();
|
|
}
|
|
|
|
/**
|
|
* Get file extension with dot
|
|
* @param {string} filename - The filename
|
|
* @returns {string} - File extension with dot
|
|
*/
|
|
function getFileExtensionWithDot(filename) {
|
|
const ext = getFileExtension(filename);
|
|
return ext ? `.${ext}` : '';
|
|
}
|
|
|
|
/**
|
|
* Check if file is an image
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if image
|
|
*/
|
|
function isImageFile(mimeType) {
|
|
return mimeType && mimeType.startsWith('image/');
|
|
}
|
|
|
|
/**
|
|
* Check if file is a video
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if video
|
|
*/
|
|
function isVideoFile(mimeType) {
|
|
return mimeType && mimeType.startsWith('video/');
|
|
}
|
|
|
|
/**
|
|
* Check if file is audio
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if audio
|
|
*/
|
|
function isAudioFile(mimeType) {
|
|
return mimeType && mimeType.startsWith('audio/');
|
|
}
|
|
|
|
/**
|
|
* Check if file is text
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if text
|
|
*/
|
|
function isTextFile(mimeType) {
|
|
return mimeType && mimeType.startsWith('text/');
|
|
}
|
|
|
|
/**
|
|
* Check if file is JSON
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if JSON
|
|
*/
|
|
function isJSONFile(mimeType) {
|
|
return mimeType === 'application/json';
|
|
}
|
|
|
|
/**
|
|
* Check if file is PDF
|
|
* @param {string} mimeType - MIME type
|
|
* @returns {boolean} - True if PDF
|
|
*/
|
|
function isPDFFile(mimeType) {
|
|
return mimeType === 'application/pdf';
|
|
}
|
|
|
|
/**
|
|
* Get attachment with backward compatibility
|
|
* @param {string} attachmentId - The attachment ID
|
|
* @returns {Promise<Object|null>} - Attachment data or null if not found
|
|
*/
|
|
export async function getAttachmentWithBackwardCompatibility(attachmentId) {
|
|
// First try new structure
|
|
if (Meteor.isServer) {
|
|
if (Attachments && Attachments.collection) {
|
|
const cursor = Attachments.collection;
|
|
const newAttachment = typeof cursor.findOneAsync === 'function' ? await cursor.findOneAsync({ _id: attachmentId }) : cursor.findOne({ _id: attachmentId });
|
|
if (newAttachment) {
|
|
return newAttachment;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fall back to old structure
|
|
const oldAttachment = await getOldAttachmentData(attachmentId);
|
|
return oldAttachment;
|
|
}
|
|
|
|
/**
|
|
* Get attachments for a card with backward compatibility
|
|
* @param {Object} query - Query object
|
|
* @returns {Promise<Array>} - Array of attachments
|
|
*/
|
|
export async function getAttachmentsWithBackwardCompatibility(query) {
|
|
let newAttachments = [];
|
|
|
|
// Get new attachments
|
|
if (Meteor.isServer) {
|
|
if (Attachments && Attachments.collection) {
|
|
const cursor = Attachments.collection.find(query);
|
|
newAttachments = typeof cursor.fetchAsync === 'function' ? await cursor.fetchAsync() : cursor.fetch();
|
|
}
|
|
}
|
|
|
|
const oldAttachments = [];
|
|
|
|
if (Meteor.isServer) {
|
|
try {
|
|
// Query old structure for the same card
|
|
const cardId = query['meta.cardId'];
|
|
if (cardId) {
|
|
const cursor = OldAttachmentsFileRecord.find({ cardId });
|
|
const oldFileRecords = typeof cursor.fetchAsync === 'function' ? await cursor.fetchAsync() : cursor.fetch();
|
|
for (const fileRecord of oldFileRecords) {
|
|
const oldAttachment = await getOldAttachmentData(fileRecord._id);
|
|
if (oldAttachment) {
|
|
oldAttachments.push(oldAttachment);
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error reading old attachments:', error);
|
|
}
|
|
}
|
|
|
|
// Combine and deduplicate
|
|
const allAttachments = [...newAttachments, ...oldAttachments];
|
|
const uniqueAttachments = allAttachments.filter((attachment, index, self) =>
|
|
index === self.findIndex(a => a._id === attachment._id)
|
|
);
|
|
|
|
return uniqueAttachments;
|
|
}
|
|
|
|
/**
|
|
* Get file stream from old GridFS structure
|
|
* @param {string} attachmentId - The attachment ID
|
|
* @returns {Object|null} - GridFS file stream or null if not found
|
|
*/
|
|
export async function getOldAttachmentStream(attachmentId, coll = 'attachments') {
|
|
if (Meteor.isServer) {
|
|
try {
|
|
const { FileRecord } = oldCollections(coll);
|
|
const fileRecord = await FileRecord.findOneAsync({ _id: attachmentId });
|
|
const gridFsId = gridFsObjectIdFromRecord(coll, fileRecord);
|
|
if (!gridFsId) {
|
|
return null;
|
|
}
|
|
const db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
|
|
const bucket = new MongoInternals.NpmModule.GridFSBucket(db, {
|
|
bucketName: `cfs_gridfs.${coll}`,
|
|
});
|
|
// The binary is addressed by the GridFS file ObjectId, not the filename.
|
|
return bucket.openDownloadStream(gridFsId);
|
|
} catch (error) {
|
|
console.error('Error creating GridFS stream:', error);
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get file data from old GridFS structure
|
|
* @param {string} attachmentId - The attachment ID
|
|
* @returns {Buffer|null} - File data buffer or null if not found
|
|
*/
|
|
export async function getOldAttachmentDataBuffer(attachmentId, coll = 'attachments') {
|
|
if (Meteor.isServer) {
|
|
try {
|
|
const { FileRecord } = oldCollections(coll);
|
|
const fileRecord = await FileRecord.findOneAsync({ _id: attachmentId });
|
|
const gridFsId = gridFsObjectIdFromRecord(coll, fileRecord);
|
|
if (!gridFsId) {
|
|
return null;
|
|
}
|
|
const db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
|
|
const bucket = new MongoInternals.NpmModule.GridFSBucket(db, {
|
|
bucketName: `cfs_gridfs.${coll}`,
|
|
});
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
// The binary is addressed by the GridFS file ObjectId, not the filename.
|
|
const downloadStream = bucket.openDownloadStream(gridFsId);
|
|
|
|
downloadStream.on('data', (chunk) => {
|
|
chunks.push(chunk);
|
|
});
|
|
|
|
downloadStream.on('end', () => {
|
|
resolve(Buffer.concat(chunks));
|
|
});
|
|
|
|
downloadStream.on('error', (error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.error('Error reading GridFS data:', error);
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|