@@ -41,34 +41,50 @@ export default function register($app) {
4141 ) ;
4242 } )
4343 . post ( ( req , res ) => {
44- let { content } = req . body ;
4544 try {
46- content = JSON . parse ( Base64 . decode ( content ) ) ;
47- if ( ! ( Object . keys ( content . settings ) . length >= 0 ) ) {
48- throw new Error ( '备份文件应该至少包含 settings 字段' ) ;
49- }
50- } catch ( err ) {
45+ let { content } = req . body ;
5146 try {
52- content = JSON . parse ( content ) ;
47+ content = JSON . parse ( Base64 . decode ( content ) ) ;
5348 if ( ! ( Object . keys ( content . settings ) . length >= 0 ) ) {
5449 throw new Error ( '备份文件应该至少包含 settings 字段' ) ;
5550 }
5651 } catch ( err ) {
57- $ . error (
58- `备份文件校验失败, 无法还原\nReason: ${
59- err . message ?? err
60- } `,
61- ) ;
62- throw new Error ( '备份文件校验失败, 无法还原' ) ;
52+ try {
53+ content = JSON . parse ( content ) ;
54+ if ( ! ( Object . keys ( content . settings ) . length >= 0 ) ) {
55+ throw new Error (
56+ '备份文件应该至少包含 settings 字段' ,
57+ ) ;
58+ }
59+ } catch ( err ) {
60+ $ . error (
61+ `备份文件校验失败, 无法还原\nReason: ${
62+ err . message ?? err
63+ } `,
64+ ) ;
65+ throw new Error ( '备份文件校验失败, 无法还原' ) ;
66+ }
6367 }
68+ $ . write ( JSON . stringify ( content , null , ` ` ) , '#sub-store' ) ;
69+ if ( $ . env . isNode ) {
70+ $ . cache = content ;
71+ $ . persistCache ( ) ;
72+ }
73+ migrate ( ) ;
74+ success ( res ) ;
75+ } catch ( e ) {
76+ $ . error (
77+ `Failed to restore backup data.\nReason: ${ e . message ?? e } ` ,
78+ ) ;
79+ failed (
80+ res ,
81+ new RequestInvalidError (
82+ 'INVALID_BACKUP_DATA' ,
83+ 'Invalid backup data, failed to restore!' ,
84+ `Reason: ${ e . message ?? e } ` ,
85+ ) ,
86+ ) ;
6487 }
65- $ . write ( JSON . stringify ( content , null , ` ` ) , '#sub-store' ) ;
66- if ( $ . env . isNode ) {
67- $ . cache = content ;
68- $ . persistCache ( ) ;
69- }
70- migrate ( ) ;
71- success ( res ) ;
7288 } ) ;
7389
7490 if ( ENV ( ) . isNode ) {
0 commit comments