-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from formio/feat/FIO-7615_cli_tool_migrate_pdfs
FIO-7615: Add option to migrate pdf files to copy and migrate commands
- Loading branch information
Showing
7 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
const axios = require('axios'); | ||
const crypto = require('crypto'); | ||
|
||
async function getPdfFile(url) { | ||
const response = await axios.get(url, {responseType: 'arraybuffer'}); | ||
const buffer = Buffer.from(response.data, 'binary'); // Get the file buffer | ||
const fileName = `${crypto.randomUUID()}.pdf`; | ||
|
||
return { | ||
name: fileName, | ||
buffer | ||
}; | ||
} | ||
|
||
async function migratePdfFileForForm(form, options) { | ||
if (form.display !== 'pdf' || !form.settings.pdf) { | ||
return; | ||
} | ||
|
||
const destUploadUrl = `${options.dstOptions.server}/${options.dstOptions.projectName}/upload`; | ||
const file = await getPdfFile(form.settings.pdf.src + '.pdf'); | ||
const formData = new FormData(); | ||
const fileBlob = new Blob([file.buffer], {type: 'application/pdf'}); | ||
|
||
formData.set('file', fileBlob, file.name); | ||
|
||
const result = await axios.post(destUploadUrl, formData, { | ||
headers: { | ||
'x-token': options.dstOptions.key, | ||
'Content-Type': 'multipart/form-data', | ||
} | ||
}); | ||
|
||
// Assign new pdf file info to the form | ||
form.settings.pdf = { | ||
src: `${options.dstOptions.server}/pdf-proxy${result.data.path}`, | ||
id: result.data.file | ||
}; | ||
} | ||
|
||
module.exports = { | ||
migratePdfFileForForm | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters