{"record":{"id":"3faac475d2e21e86","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-file-3faac4","errorCode":"error-invalid-file","errorMessage":"Invalid file","messagePattern":"Invalid file","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/messages/sendFileMessage.ts","lineNumber":29,"sourceCode":"import type { ServerMethods } from '@rocket.chat/ddp-client';\nimport { Rooms, Uploads, Users } from '@rocket.chat/models';\nimport { Match, check } from 'meteor/check';\nimport { Meteor } from 'meteor/meteor';\n\nimport { executeSendMessage } from './sendMessage';\nimport { getFileExtension } from '../../../lib/utils/getFileExtension';\nimport { canAccessRoomAsync } from '../../lib/authorization/canAccessRoom';\nimport { callbacks } from '../../lib/callbacks';\nimport { methodDeprecationLogger } from '../../lib/deprecationWarningLogger';\nimport { SystemLogger } from '../../lib/logger/system';\nimport { isImagePreviewSupported } from '../../lib/media/file-upload/isImagePreviewSupported';\nimport { FileUpload } from '../../lib/media/file-upload/lib/FileUpload';\n\nfunction validateFileRequiredFields(file: Partial<IUpload>): asserts file is AtLeast<IUpload, '_id' | 'name' | 'type' | 'size'> {\n\tconst requiredFields = ['_id', 'name', 'type', 'size'];\n\trequiredFields.forEach((field) => {\n\t\tif (!Object.keys(file).includes(field)) {\n\t\t\tthrow new Meteor.Error('error-invalid-file', 'Invalid file');\n\t\t}\n\t});\n}\n\nexport const parseFileIntoMessageAttachments = async (\n\tfile: Partial<IUpload>,\n\troomId: string,\n\tuser: IUser,\n): Promise<FilesAndAttachments> => {\n\tvalidateFileRequiredFields(file);\n\n\tconst upload = await Uploads.findOneByIdAndUserIdAndRoomId(file._id, user._id, roomId, { projection: { _id: 1 } });\n\tif (!upload) {\n\t\tthrow new Meteor.Error('error-invalid-file', 'Invalid file', {\n\t\t\tmethod: 'sendFileMessage',\n\t\t});\n\t}\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/messages/sendFileMessage.ts#L11-L47","documentation":"sendFileMessage first validates the client-supplied file object with validateFileRequiredFields: each of _id, name, type and size must be present as a key. If any is missing it throws error-invalid-file before any database access, so this failure never depends on server state.","triggerScenarios":"Meteor.call('sendFileMessage', rid, store, file) with a partial object, e.g. omitting type or size; passing a raw File/Blob handle or a picked subset of the upload record; a refactor or serializer that drops falsy fields (size 0, empty name) from the payload.","commonSituations":"Client sends a hand-built object instead of the record returned by the completed upload; form data mangled by a middleware; falsy-value filtering (size === 0) accidentally removing required keys.","solutions":["Pass the complete upload record produced by the upload flow - it contains _id, name, type and size","Validate the object client-side before the method call (see type guard)","Prefer the REST flow POST /v1/rooms.media/:rid followed by POST /v1/rooms.mediaConfirm/:rid/:fileId; the Meteor method is deprecated since 9.0.0"],"exampleFix":"// before: partial object\nMeteor.call('sendFileMessage', rid, store, { _id: fileId, name: 'log.txt' });\n\n// after: full record from the completed upload\nMeteor.call('sendFileMessage', rid, store, {\n\t_id: upload.fileId,\n\tname: upload.name,\n\ttype: upload.type,\n\tsize: upload.size,\n});","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import type { IUpload } from '@rocket.chat/core-typings';\n\ntype CompleteUpload = Pick<IUpload, '_id' | 'name' | 'type' | 'size'>;\n\nconst isCompleteUpload = (\n\tfile: Partial<IUpload>,\n): file is CompleteUpload =>\n\ttypeof file._id === 'string' &&\n\ttypeof file.name === 'string' &&\n\ttypeof file.type === 'string' &&\n\ttypeof file.size === 'number';\n\nif (!isCompleteUpload(file)) {\n\tthrow new Error('file payload incomplete');\n}\nawait Meteor.callAsync('sendFileMessage', rid, store, file);","tryCatchPattern":"try {\n\tawait Meteor.callAsync('sendFileMessage', rid, store, file);\n} catch (e: any) {\n\tif (e?.error === 'error-invalid-file' && !isCompleteUpload(file)) {\n\t\t// client-side payload bug: fix the object, do not retry as-is\n\t}\n\tthrow e;\n}","preventionTips":["Always send the upload record returned by the completed upload, never a hand-built subset","Type the payload as the required subset so the compiler catches missing fields","Prefer the REST rooms.media/rooms.mediaConfirm pair over the deprecated method"],"tags":["meteor-method","file-upload","validation","deprecated"],"backgroundTag":"missing-required-fields","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}