[IMPROVE] Create thumbnails from uploaded images (#20907)
Co-authored-by: Diego Sampaio <chinello@gmail.com> Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat> Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>pull/21779/head^2
parent
3c362a0d60
commit
3e68e780bf
@ -1,91 +0,0 @@ |
|||||||
import { Meteor } from 'meteor/meteor'; |
|
||||||
import { Match, check } from 'meteor/check'; |
|
||||||
import { Random } from 'meteor/random'; |
|
||||||
import _ from 'underscore'; |
|
||||||
|
|
||||||
import { Uploads } from '../../../models'; |
|
||||||
import { Rooms } from '../../../models/server/raw'; |
|
||||||
import { callbacks } from '../../../callbacks'; |
|
||||||
import { FileUpload } from '../lib/FileUpload'; |
|
||||||
import { canAccessRoom } from '../../../authorization/server/functions/canAccessRoom'; |
|
||||||
|
|
||||||
Meteor.methods({ |
|
||||||
async sendFileMessage(roomId, store, file, msgData = {}) { |
|
||||||
if (!Meteor.userId()) { |
|
||||||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendFileMessage' }); |
|
||||||
} |
|
||||||
|
|
||||||
const room = await Rooms.findOneById(roomId); |
|
||||||
const user = Meteor.user(); |
|
||||||
|
|
||||||
if (user?.type !== 'app' && canAccessRoom(room, user) !== true) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
check(msgData, { |
|
||||||
avatar: Match.Optional(String), |
|
||||||
emoji: Match.Optional(String), |
|
||||||
alias: Match.Optional(String), |
|
||||||
groupable: Match.Optional(Boolean), |
|
||||||
msg: Match.Optional(String), |
|
||||||
tmid: Match.Optional(String), |
|
||||||
}); |
|
||||||
|
|
||||||
Uploads.updateFileComplete(file._id, Meteor.userId(), _.omit(file, '_id')); |
|
||||||
|
|
||||||
const fileUrl = FileUpload.getPath(`${ file._id }/${ encodeURI(file.name) }`); |
|
||||||
|
|
||||||
const attachment = { |
|
||||||
title: file.name, |
|
||||||
type: 'file', |
|
||||||
description: file.description, |
|
||||||
title_link: fileUrl, |
|
||||||
title_link_download: true, |
|
||||||
}; |
|
||||||
|
|
||||||
if (/^image\/.+/.test(file.type)) { |
|
||||||
attachment.image_url = fileUrl; |
|
||||||
attachment.image_type = file.type; |
|
||||||
attachment.image_size = file.size; |
|
||||||
if (file.identify && file.identify.size) { |
|
||||||
attachment.image_dimensions = file.identify.size; |
|
||||||
} |
|
||||||
try { |
|
||||||
attachment.image_preview = await FileUpload.resizeImagePreview(file); |
|
||||||
} catch (e) { |
|
||||||
delete attachment.image_url; |
|
||||||
delete attachment.image_type; |
|
||||||
delete attachment.image_size; |
|
||||||
delete attachment.image_dimensions; |
|
||||||
} |
|
||||||
} else if (/^audio\/.+/.test(file.type)) { |
|
||||||
attachment.audio_url = fileUrl; |
|
||||||
attachment.audio_type = file.type; |
|
||||||
attachment.audio_size = file.size; |
|
||||||
} else if (/^video\/.+/.test(file.type)) { |
|
||||||
attachment.video_url = fileUrl; |
|
||||||
attachment.video_type = file.type; |
|
||||||
attachment.video_size = file.size; |
|
||||||
} |
|
||||||
|
|
||||||
let msg = Object.assign({ |
|
||||||
_id: Random.id(), |
|
||||||
rid: roomId, |
|
||||||
ts: new Date(), |
|
||||||
msg: '', |
|
||||||
file: { |
|
||||||
_id: file._id, |
|
||||||
name: file.name, |
|
||||||
type: file.type, |
|
||||||
}, |
|
||||||
groupable: false, |
|
||||||
attachments: [attachment], |
|
||||||
}, msgData); |
|
||||||
|
|
||||||
msg = Meteor.call('sendMessage', msg); |
|
||||||
|
|
||||||
Meteor.defer(() => callbacks.run('afterFileUpload', { user, room, message: msg })); |
|
||||||
|
|
||||||
return msg; |
|
||||||
}, |
|
||||||
}); |
|
@ -0,0 +1,130 @@ |
|||||||
|
/* eslint-disable @typescript-eslint/camelcase */ |
||||||
|
import { Meteor } from 'meteor/meteor'; |
||||||
|
import { Match, check } from 'meteor/check'; |
||||||
|
import _ from 'underscore'; |
||||||
|
|
||||||
|
import { Uploads } from '../../../models/server'; |
||||||
|
import { Rooms } from '../../../models/server/raw'; |
||||||
|
import { callbacks } from '../../../callbacks/server'; |
||||||
|
import { FileUpload } from '../lib/FileUpload'; |
||||||
|
import { canAccessRoom } from '../../../authorization/server/functions/canAccessRoom'; |
||||||
|
import { MessageAttachment } from '../../../../definition/IMessage/MessageAttachment/MessageAttachment'; |
||||||
|
import { FileAttachmentProps } from '../../../../definition/IMessage/MessageAttachment/Files/FileAttachmentProps'; |
||||||
|
import { IUser } from '../../../../definition/IUser'; |
||||||
|
|
||||||
|
Meteor.methods({ |
||||||
|
async sendFileMessage(roomId, _store, file, msgData = {}) { |
||||||
|
const user = Meteor.user() as IUser | undefined; |
||||||
|
if (!user) { |
||||||
|
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendFileMessage' } as any); |
||||||
|
} |
||||||
|
|
||||||
|
const room = await Rooms.findOneById(roomId); |
||||||
|
|
||||||
|
if (user?.type !== 'app' && !canAccessRoom(room, user)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
check(msgData, { |
||||||
|
avatar: Match.Optional(String), |
||||||
|
emoji: Match.Optional(String), |
||||||
|
alias: Match.Optional(String), |
||||||
|
groupable: Match.Optional(Boolean), |
||||||
|
msg: Match.Optional(String), |
||||||
|
tmid: Match.Optional(String), |
||||||
|
}); |
||||||
|
|
||||||
|
Uploads.updateFileComplete(file._id, user._id, _.omit(file, '_id')); |
||||||
|
|
||||||
|
const fileUrl = FileUpload.getPath(`${ file._id }/${ encodeURI(file.name) }`); |
||||||
|
|
||||||
|
const attachments: MessageAttachment[] = []; |
||||||
|
|
||||||
|
const files = [{ |
||||||
|
_id: file._id, |
||||||
|
name: file.name, |
||||||
|
type: file.type, |
||||||
|
}]; |
||||||
|
|
||||||
|
if (/^image\/.+/.test(file.type)) { |
||||||
|
const attachment: FileAttachmentProps = { |
||||||
|
title: file.name, |
||||||
|
type: 'file', |
||||||
|
description: file.description, |
||||||
|
title_link: fileUrl, |
||||||
|
title_link_download: true, |
||||||
|
image_url: fileUrl, |
||||||
|
image_type: file.type, |
||||||
|
image_size: file.size, |
||||||
|
}; |
||||||
|
|
||||||
|
if (file.identify && file.identify.size) { |
||||||
|
attachment.image_dimensions = file.identify.size; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
attachment.image_preview = await FileUpload.resizeImagePreview(file); |
||||||
|
const thumbResult = await FileUpload.createImageThumbnail(file); |
||||||
|
if (thumbResult) { |
||||||
|
const { data: thumbBuffer, width, height } = thumbResult; |
||||||
|
const thumbnail = FileUpload.uploadImageThumbnail(file, thumbBuffer, roomId, user._id); |
||||||
|
const thumbUrl = FileUpload.getPath(`${ thumbnail._id }/${ encodeURI(file.name) }`); |
||||||
|
attachment.image_url = thumbUrl; |
||||||
|
attachment.image_type = thumbnail.type; |
||||||
|
attachment.image_dimensions = { |
||||||
|
width, |
||||||
|
height, |
||||||
|
}; |
||||||
|
files.push({ |
||||||
|
_id: thumbnail._id, |
||||||
|
name: file.name, |
||||||
|
type: thumbnail.type, |
||||||
|
}); |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
console.error(e); |
||||||
|
} |
||||||
|
attachments.push(attachment); |
||||||
|
} else if (/^audio\/.+/.test(file.type)) { |
||||||
|
const attachment: FileAttachmentProps = { |
||||||
|
title: file.name, |
||||||
|
type: 'file', |
||||||
|
description: file.description, |
||||||
|
title_link: fileUrl, |
||||||
|
title_link_download: true, |
||||||
|
audio_url: fileUrl, |
||||||
|
audio_type: file.type, |
||||||
|
audio_size: file.size, |
||||||
|
}; |
||||||
|
attachments.push(attachment); |
||||||
|
} else if (/^video\/.+/.test(file.type)) { |
||||||
|
const attachment: FileAttachmentProps = { |
||||||
|
title: file.name, |
||||||
|
type: 'file', |
||||||
|
description: file.description, |
||||||
|
title_link: fileUrl, |
||||||
|
title_link_download: true, |
||||||
|
video_url: fileUrl, |
||||||
|
video_type: file.type, |
||||||
|
video_size: file.size, |
||||||
|
}; |
||||||
|
attachments.push(attachment); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
const msg = Meteor.call('sendMessage', { |
||||||
|
rid: roomId, |
||||||
|
ts: new Date(), |
||||||
|
msg: '', |
||||||
|
file: files[0], |
||||||
|
files, |
||||||
|
groupable: false, |
||||||
|
attachments, |
||||||
|
...msgData, |
||||||
|
}); |
||||||
|
|
||||||
|
callbacks.runAsync('afterFileUpload', { user, room, message: msg }); |
||||||
|
|
||||||
|
return msg; |
||||||
|
}, |
||||||
|
}); |
Loading…
Reference in new issue