You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.0 KiB
82 lines
2.0 KiB
import { axiosWrapper } from './axios.js'; |
|
import { exportDatabaseValidation } from './validation.js'; |
|
import fs from 'fs'; |
|
import path from 'path'; |
|
|
|
const getBackups = async () => { |
|
try { |
|
const { data } = await axiosWrapper({ |
|
method: 'get', |
|
url: '/backups/available', |
|
}); |
|
return data; |
|
} catch (error) { |
|
console.error(error); |
|
return false; |
|
} |
|
}; |
|
|
|
const exportDatabase = async (tag) => { |
|
try { |
|
const { value: validValue, error } = exportDatabaseValidation({ tag }); |
|
if (error) throw error?.details[0]?.message; |
|
|
|
const { data } = await axiosWrapper({ |
|
method: 'post', |
|
url: '/backups/export/database', |
|
payload: { |
|
tag: validValue.tag, // 'Julyyy 24th 2021' |
|
options: { |
|
recipes: true, |
|
settings: true, |
|
pages: true, |
|
themes: true, |
|
groups: true, |
|
users: true, |
|
notifications: true, |
|
}, |
|
templates: ['recipes.md'], |
|
}, |
|
}); |
|
return data; |
|
} catch (error) { |
|
console.error(error); |
|
return false; |
|
} |
|
}; |
|
|
|
const getFileToken = async (filename) => { |
|
try { |
|
const { data } = await axiosWrapper({ |
|
method: 'get', |
|
url: `/backups/${filename}/download`, |
|
}); |
|
return data; |
|
} catch (error) { |
|
console.error(error); |
|
return false; |
|
} |
|
}; |
|
|
|
const downloadBackup = async (fileToken, fileName) => { |
|
try { |
|
const target_path = path.resolve(`backups/${fileName}`); |
|
const writer = fs.createWriteStream(target_path, 'binary'); |
|
const streamResponse = await axiosWrapper({ |
|
method: 'get', |
|
url: `/utils/download?token=${fileToken}`, |
|
responseType: 'stream', |
|
}); |
|
streamResponse.data.pipe(writer); |
|
writer.on('finish', () => console.log(`Downloaded: ${fileName}`)); |
|
writer.on('error', () => |
|
console.error(`[ERROR] while dowloading ${fileName}`) |
|
); |
|
return true; |
|
} catch (error) { |
|
console.error(error); |
|
return false; |
|
} |
|
}; |
|
|
|
export { getBackups, exportDatabase, getFileToken, downloadBackup };
|
|
|