1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00

Implementing embended image sending with email media messenger ##83

This commit is contained in:
Patrik J. Braun 2023-08-01 00:05:19 +02:00
parent 80853771f4
commit 819ca1b80d

View File

@ -1,7 +1,13 @@
import {createTransport, Transporter} from 'nodemailer'; 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 {Config} from '../../../common/config/private/Config';
import {EmailMessagingType} from '../../../common/config/private/MessagingConfig'; 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 { export class EmailMediaMessenger {
transporter: Transporter; 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: { public async sendMedia(mailSettings: {
from: string, from: string,
to: string, to: string,
@ -33,11 +48,47 @@ export class EmailMediaMessenger {
text: string text: string
}, media: MediaDTO[]) { }, media: MediaDTO[]) {
const attachments = [];
const htmlStart = '<h1 style="text-align: center; margin-bottom: 2em">' + Config.Server.applicationTitle + '</h1>\n' +
'<h3>' + mailSettings.text + '</h3>\n' +
'<table style="margin-left: auto; margin-right: auto;">\n' +
' <tbody>\n';
const htmlEnd = ' </tr>\n' +
' </tbody>\n' +
'</table>';
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 += '<tr>';
}
htmlMiddle += '<td>\n' +
' <a style="display: block;text-align: center;" href="' + linkUrl + '"><img alt="' + media[i].name + '" style="max-width: 200px; height: 150px" src="cid:img' + i + '"/></a>\n' +
caption +
' </td>\n';
if (i % 2 == 1 || i === media.length - 1) {
htmlMiddle += '</tr>';
}
}
return await this.transporter.sendMail({ return await this.transporter.sendMail({
from: mailSettings.from, from: mailSettings.from,
to: mailSettings.to, to: mailSettings.to,
subject: mailSettings.subject, subject: mailSettings.subject,
text: mailSettings.text + media.map(m => m.name).join(', ') html: htmlStart + htmlMiddle + htmlEnd,
attachments: attachments
}); });
} }
} }