Skip to content

Commit

Permalink
fix(backup): 🩹 prevent HTTP 404 Error when downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed May 8, 2023
1 parent ee3bca0 commit 1ab6925
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scolengo-cli",
"version": "0.9.0",
"version": "0.9.1",
"types": "types/index.d.ts",
"bin": {
"scolengo": "./dist/src/index.js"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Backup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async function backup (filePath: string, {
const communications = await getCommunications(user, mailFolder)
await communicationsToZip(user, folder.folder('eml') as JSZip, communications, attachments)
folder.file('communications.json', JSON.stringify(communications, null, 2))
console.log(chalk.green(`✔ MAIL / ${mailFolder.type} (${mailFolder.name})`))
console.log(chalk.green(`✔ MAIL > ${mailFolder.type} (${mailFolder.name})`))
}
}
if (exportableData !== ExportableData.MAIL) console.log(chalk.green('✔ ' + ExportableData[exportableData]))
Expand Down
26 changes: 16 additions & 10 deletions src/functions/participationToMIME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Participation } from 'scolengo-api/types/models/Messaging'
import { Skolengo } from 'scolengo-api'
import { ReadStream } from 'fs'
import { Stream } from 'stream'
import chalk from 'chalk'

async function stream2buffer (stream: Stream): Promise<Buffer> {
return await new Promise < Buffer >((resolve, reject) => {
const _buf = Array < any >()
return await new Promise<Buffer>((resolve, reject) => {
const _buf = Array<any>()

stream.on('data', chunk => _buf.push(chunk))
stream.on('end', () => resolve(Buffer.concat(_buf)))
Expand Down Expand Up @@ -35,15 +36,20 @@ export async function participationToMIME (user: Skolengo, participation: Partic
})
if (withAttachments) {
for (const attachment of participation.attachments) {
const data = (await user.downloadAttachment(attachment)) as ReadStream
try {
const data = (await user.downloadAttachment(attachment)) as ReadStream

msg.addAttachment({
contentType: attachment.mimeType,
filename: attachment.name,
inline: false,
encoding: 'base64',
data: (await stream2buffer(data)).toString('base64')
})
msg.addAttachment({
contentType: attachment.mimeType,
filename: attachment.name,
inline: false,
encoding: 'base64',
data: (await stream2buffer(data)).toString('base64')
})
} catch (e) {
const err = e as Error
console.error(chalk.redBright(`✘ ${err.name} : ${err.message}`))
}
}
}
return msg.asRaw()
Expand Down

0 comments on commit 1ab6925

Please sign in to comment.