Skip to content

Commit

Permalink
feat: check ebook size before sending and show warning
Browse files Browse the repository at this point in the history
  • Loading branch information
agrmohit committed Mar 2, 2024
1 parent 50d24da commit b284566
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,30 @@ List of eReaders that support sending ebook using email:
Configuration options available in the [config file](config.json)

<!-- deno-fmt-ignore-start -->
| Option | Type | Description |
| ----------------------- | -------- | --------------------------------------------------------- |
| token | string | Omnivore API Token |
| endpoint | string | Omnivore GraphQL API endpoint |
| title | string | Title of the ebook |
| author | string | Author of the ebook |
| cover | string | URL for fetching cover image for the ebook |
| description | string | Description of the ebook |
| addLabelsInContent | boolean | Whether to add the labels for the article below title |
| addArticleLinkInContent | boolean | Whether to add the link for the article below title |
| allowImages | boolean | Whether to add images linked in article in the ebook |
| outputFileName | string | ebook file name |
| maxArticleCount | number | Number of articles to fetch |
| searchQuery | string | Valid query for article search, default: "sort:saved-desc". Change it to "sort:saved-asc" for fetching oldest articles first |
| ignoredLabels | string[] | List of labels to exclude from the ebook |
| ignoredLinks | string[] | List of urls to exclude from the ebook |
| emailSupport | boolean | Whether to send the ebook via email (to your eReader) |
| emailHost | string | SMTP Hostname of your email provider |
| emailPort | number | Usually one of 587, 465 or 25. Prefer 587 when available |
| emailUser | string | Username/Email address of your email account |
| emailPassword | string | Password of your email account. Prefer app password |
| emailRecipient | string | Email address that should receive your ebook |
| emailFrom | string | Sender name that appears to the email receiver |
| emailAllowSTARTTLS | boolean | Allow connecting to the SMTP server using STARTTLS |
| Option | Type | Description |
| ------------------------ | -------- | -------------------------------------------------------- |
| token | string | Omnivore API Token |
| endpoint | string | Omnivore GraphQL API endpoint |
| title | string | Title of the ebook |
| author | string | Author of the ebook |
| cover | string | URL for fetching cover image for the ebook |
| description | string | Description of the ebook |
| addLabelsInContent | boolean | Whether to add the labels for the article below title |
| addArticleLinkInContent | boolean | Whether to add the link for the article below title |
| allowImages | boolean | Whether to add images linked in article in the ebook |
| outputFileName | string | ebook file name |
| maxArticleCount | number | Number of articles to fetch |
| searchQuery | string | Valid query for article search, default: "sort:saved-desc". Change it to "sort:saved-asc" for fetching oldest articles first |
| ignoredLabels | string[] | List of labels to exclude from the ebook |
| ignoredLinks | string[] | List of urls to exclude from the ebook |
| emailSupport | boolean | Whether to send the ebook via email (to your eReader) |
| emailHost | string | SMTP Hostname of your email provider |
| emailPort | number | Usually one of 587, 465 or 25. Prefer 587 when available |
| emailUser | string | Username/Email address of your email account |
| emailPassword | string | Password of your email account. Prefer app password |
| emailRecipient | string | Email address that should receive your ebook |
| emailFrom | string | Sender name that appears to the email receiver |
| emailAllowSTARTTLS | boolean | Allow connecting to the SMTP server using STARTTLS |
| emailSizeWarningSuppress | boolean | Show a warning if ebook is over emailSizeWarningMinSize |
| emailSizeWarningMinSize | number | Min ebook size to show warning while sending email in MB |
<!-- deno-fmt-ignore-end -->
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
"emailPassword": "",
"emailRecipient": "my-send-to-kindle-email@kindle.com",
"emailFrom": "Omnivore EPUB Mailer",
"emailAllowSTARTTLS": true
"emailAllowSTARTTLS": true,
"emailSizeWarningSuppress": false,
"emailSizeWarningMinSize": 25
}
34 changes: 28 additions & 6 deletions email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ export async function sendEmail() {
],
};

if (
await Deno.stat(config.outputFileName).catch((_err) => {
console.error(`🚫 ebook file '${config.outputFileName}' is missing`);
Deno.exit(1);
})
) {
if (await verifyEpub()) {
try {
console.log(`📧 Sending email from '${config.emailFrom} <${config.emailUser}>' to '${config.emailRecipient}'`);
const info = await transporter.sendMail(mailOptions);
Expand All @@ -39,3 +34,30 @@ export async function sendEmail() {
}
}
}

async function verifyEpub(): Promise<boolean> {
try {
const file = await Deno.stat(config.outputFileName);

// Check if it is indeed a file
if (!file.isFile) {
console.error(`🚫 ${config.outputFileName} is not a file`);
Deno.exit(1);
}

// Convert from bytes to MB (not MiB) rounded off to 2 digits after decimal
const ebookSize = (file.size / 1_000_000).toFixed(2);

// Show a warning if ebook is over a specified size
if (!config.emailSizeWarningSuppress && Number(ebookSize) >= config.emailSizeWarningMinSize) {
console.warn(`⚠️ ebook size is too large at ${ebookSize} MB (limit: ${config.emailSizeWarningMinSize} MB)`);
console.warn("⚠️ Many email providers and eReader emailing services may reject this email");
console.warn("⚠️ To suppress this warning, set 'emailSuppressSizeWarning' to true in 'config.json'");
}
} catch (_err) {
console.error(`🚫 ebook file '${config.outputFileName}' is missing`);
Deno.exit(1);
}

return true;
}

0 comments on commit b284566

Please sign in to comment.