@ingestkorea/client-sens
Version:
INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.
80 lines (79 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendMMSCommand = void 0;
const node_path_1 = require("node:path");
const node_crypto_1 = require("node:crypto");
const node_fs_1 = require("node:fs");
const index_js_1 = require("../models/index.js");
const SendMMS_js_1 = require("../protocols/SendMMS.js");
const constants_1 = require("./constants");
const SendSMSCommand_js_1 = require("./SendSMSCommand.js");
class SendMMSCommand extends index_js_1.SensCommand {
constructor(input) {
super(input);
const content = input.content ? (0, constants_1.trimText)(input.content) : "내용없음";
const subject = input.subject ? (0, constants_1.trimText)(input.subject) : "제목없음";
const messages = (0, SendSMSCommand_js_1.resolveInputMessages)(input.messages, { defaultContent: content, defaultSubject: subject });
this.input = {
from: (0, constants_1.prettyPhoneNum)(input.from),
content,
messages,
type: "MMS",
contentType: input.contentType ? input.contentType : "COMM",
countryCode: input.countryCode ? input.countryCode : "82",
subject,
files: (input.files || []).map((file) => {
return resolveSendFile(file, { maxKiB: index_js_1.MMS_MAX_SUPPORT_FILE_SIZE, supportExt: index_js_1.MMS_SUPPORT_FILE_EXTENSION });
}),
};
this.serializer = SendMMS_js_1.se_SendMMSCommand;
this.deserializer = SendMMS_js_1.de_SendMMSCommand;
}
}
exports.SendMMSCommand = SendMMSCommand;
const resolveSendFile = (file, options) => {
const parsed = (0, node_path_1.parse)(file.name); // { dir, name, ext, base }
const ext = parsed.ext.toLocaleLowerCase();
if (!options.supportExt.includes(ext)) {
throw new index_js_1.SensError({
code: -1,
type: "SDK.GENERAL_ERROR",
message: `MMS는 ${options.supportExt.join(", ")} 형식의 첨부파일만 보낼 수 있습니다.`,
});
}
if (file.body) {
const buffer = Buffer.from(file.body, "base64");
checkSizeLimit(buffer, options.maxKiB);
const hash = (0, node_crypto_1.createHash)("md5").update(buffer).digest("hex");
return {
name: (0, node_path_1.format)({ name: hash, ext }),
body: file.body,
};
}
if (!(0, node_fs_1.existsSync)(file.name)) {
throw new index_js_1.SensError({
code: -1,
type: "SDK.GENERAL_ERROR",
message: `${file.name} 첨부파일이 존재하지 않습니다.`,
});
}
const buffer = (0, node_fs_1.readFileSync)(file.name);
checkSizeLimit(buffer, options.maxKiB);
const hash = (0, node_crypto_1.createHash)("md5").update(buffer).digest("hex");
return {
name: (0, node_path_1.format)({ name: hash, ext }),
body: buffer.toString("base64"),
};
};
const checkSizeLimit = (buffer, maxKiB) => {
const byteSize = buffer.length;
const kibSize = Math.ceil(byteSize / 1024);
if (kibSize > maxKiB) {
throw new index_js_1.SensError({
code: -1,
type: "SDK.GENERAL_ERROR",
message: `첨부파일은 ${maxKiB}KiB까지 전송 가능합니다. 현재 ${kibSize}KiB`,
});
}
return;
};