diff --git a/src/backend/model/mediamessengers/EmailMediaMessenger.ts b/src/backend/model/mediamessengers/EmailMediaMessenger.ts index 7aae180b..a951750e 100644 --- a/src/backend/model/mediamessengers/EmailMediaMessenger.ts +++ b/src/backend/model/mediamessengers/EmailMediaMessenger.ts @@ -1,7 +1,13 @@ import {createTransport, Transporter} from 'nodemailer'; -import {MediaDTO} from '../../../common/entities/MediaDTO'; +import {MediaDTO, MediaDTOUtils} from '../../../common/entities/MediaDTO'; import {Config} from '../../../common/config/private/Config'; import {EmailMessagingType} from '../../../common/config/private/MessagingConfig'; +import {PhotoProcessing} from '../fileprocessing/PhotoProcessing'; +import {ThumbnailSourceType} from '../threading/PhotoWorker'; +import {ProjectPath} from '../../ProjectPath'; +import * as path from 'path'; +import {PhotoMetadata} from '../../../common/entities/PhotoDTO'; +import {Utils} from '../../../common/Utils'; export class EmailMediaMessenger { transporter: Transporter; @@ -26,6 +32,15 @@ export class EmailMediaMessenger { } + private async getThumbnail(m: MediaDTO) { + return await PhotoProcessing.generateThumbnail( + path.join(ProjectPath.ImageFolder, m.directory.path, m.directory.name, m.name), + Config.Media.Thumbnail.thumbnailSizes[0], + MediaDTOUtils.isPhoto(m) ? ThumbnailSourceType.Photo : ThumbnailSourceType.Video, + false + ); + } + public async sendMedia(mailSettings: { from: string, to: string, @@ -33,11 +48,47 @@ export class EmailMediaMessenger { text: string }, media: MediaDTO[]) { + const attachments = []; + const htmlStart = '

' + Config.Server.applicationTitle + '

\n' + + '

' + mailSettings.text + '

\n' + + '\n' + + ' \n'; + const htmlEnd = ' \n' + + ' \n' + + '
'; + let htmlMiddle = ''; + for (let i = 0; i < media.length; ++i) { + const thPath = await this.getThumbnail(media[i]); + const linkUrl = Utils.concatUrls(Config.Server.publicUrl, '/gallery/', path.join(media[i].directory.path, media[i].directory.name)); + const location = (media[0].metadata as PhotoMetadata).positionData?.country ? + (media[0].metadata as PhotoMetadata).positionData?.country : + ((media[0].metadata as PhotoMetadata).positionData?.city ? + (media[0].metadata as PhotoMetadata).positionData?.city : ''); + const caption = (new Date(media[0].metadata.creationDate)).getFullYear() + (location ? ', ' + location : ''); + attachments.push({ + filename: media[i].name, + path: thPath, + cid: 'img' + i + }); + if (i % 2 == 0) { + htmlMiddle += ''; + } + htmlMiddle += '\n' + + ' ' + media[i].name + '\n' + + caption + + ' \n'; + + if (i % 2 == 1 || i === media.length - 1) { + htmlMiddle += ''; + } + } + return await this.transporter.sendMail({ from: mailSettings.from, to: mailSettings.to, subject: mailSettings.subject, - text: mailSettings.text + media.map(m => m.name).join(', ') + html: htmlStart + htmlMiddle + htmlEnd, + attachments: attachments }); } }