I am attempting to write a simple ipfs backup script, which given a directory and a time period will sync that folder to ipfs. I also plan to add some additional logic so that the system wont backup something that it already has.
I am able to zip and read the folder passed in, but when it attempts to instantiate the Moralis.File I get the following error:
(node:12488) UnhandledPromiseRejectionWarning: TypeError: Cannot create a Parse.File with that data.
at new ParseFile (D:\Software Projects\Repos\ipfs-backup\node_modules\moralis\lib\node\ParseFile.js:203:15)
at backup (D:\Software Projects\Repos\ipfs-backup\backup-service.js:33:28)
at Object. (D:\Software Projects\Repos\ipfs-backup\backup-script.js:14:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
This is my code:
const compressDir = (directory) => {
const dirPath = directory[0] === '/' ? directory.substring(1) : directory;
const splitDirPath = dirPath.split('/');
const lastDir = splitDirPath[splitDirPath.length - 1];
const tempStoragePath = `./temp/${lastDir}.zip`;
compressing.zip.compressDir(directory, tempStoragePath).then((x) => console.log(x))
.catch((err) => console.error(err));
return tempStoragePath;
};
const backup = async (directory) => {
const compressedDir = compressDir(directory);
const backupDate = new Date();
const zipFile = fs.readFileSync(compressedDir);
console.log('zip', zipFile)
// Perform encryption
const moralisZipFile = new Moralis.File(`Backup ${backupDate}`, zipFile, 'application/zip');
await moralisZipFile.saveIPFS();
const backupLink = moralisZipFile.ipfs();
const mailOptions = {
from: email,
to: email,
subject: `Backup completed - ${compressedDir} - ${backupDate}`,
text: `Backup - ${compressedDir} - ${backupDate}\n${backupLink}`
};
transporter.sendMail(mailOptions, (err, info) => console.log(err ? err : `Backup email sent: ${info.response}`));
fs.unlink(compressedDir, (err) => {
if (err) throw err;
console.log(`${compressedDir} was deleted`)
});
};
This is how I am importing Moralis, as I am using the node environment:
const Moralis = require('moralis/node');