-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathdbjob.ts
461 lines (415 loc) · 13.8 KB
/
dbjob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { CronTime } from "cron";
import path from 'path';
import fs from 'fs';
import { writeFile } from 'fs/promises'
import Package from '../../../package.json';
import { $ } from 'execa';
import AdmZip from 'adm-zip'
import { DBBAK_TASK_NAME, DBBAKUP_PATH, ROOT_PATH, TEMP_PATH, UPLOAD_FILE_PATH } from "@/lib/constant";
import { prisma } from "../prisma";
import { unlink } from "fs/promises";
import { createCaller } from "../routers/_app";
import { Context } from "../context";
import { BaseScheduleJob } from "./baseScheduleJob";
import { CreateNotification } from "../routers/notification";
import { NotificationType } from "@/lib/prismaZodType";
import archiver from 'archiver';
import { createWriteStream } from 'fs';
import yauzl from 'yauzl-promise';
import { FileService } from "./files";
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { getGlobalConfig } from "../routers/config";
export type RestoreResult = {
type: 'success' | 'skip' | 'error';
content?: string;
error?: unknown;
progress?: { current: number; total: number };
}
export type ExportTimeRange = 'day' | 'week' | 'month' | 'quarter';
export class DBJob extends BaseScheduleJob {
protected static taskName = DBBAK_TASK_NAME;
protected static job = this.createJob();
static {
this.initializeJob();
}
protected static async RunTask() {
try {
const config = await getGlobalConfig({ useAdmin: true });
const notes = await prisma.notes.findMany({
select: {
id: true,
account: true,
content: true,
isArchived: true,
isShare: true,
isTop: true,
createdAt: true,
updatedAt: true,
type: true,
attachments: true,
tags: true,
references: true,
referencedBy: true
}
});
const exportData = {
notes,
exportTime: new Date(),
version: Package.version
};
fs.writeFileSync(
`${DBBAKUP_PATH}/bak.json`,
JSON.stringify(exportData, null, 2)
);
const targetFile = UPLOAD_FILE_PATH + `/blinko_export.bko`;
try {
await unlink(targetFile);
} catch (error) { }
const output = createWriteStream(targetFile);
const archive = archiver('zip', {
zlib: { level: 9 }
});
archive.on('error', (err) => {
throw err;
});
const archiveComplete = new Promise((resolve, reject) => {
output.on('close', resolve);
output.on('error', reject);
archive.on('error', reject);
});
archive.pipe(output);
const addFilesRecursively = async (dirPath: string, basePath: string = '') => {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const fullPath = path.join(dirPath, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
await addFilesRecursively(fullPath, path.join(basePath, file));
} else {
archive.file(fullPath, {
name: path.join(basePath, file)
});
}
}
};
await addFilesRecursively(ROOT_PATH, '');
let lastUpdateTime = 0;
const updateInterval = 1000;
let finalProgress: any = null;
const task = await prisma.scheduledTask.findFirst({
where: { name: this.taskName }
})
if (!task) {
await prisma.scheduledTask.create({
data: {
name: this.taskName,
isRunning: true,
isSuccess: false,
lastRun: new Date(),
schedule: '0 0 * * *'
}
});
}
archive.on('progress', async (progress) => {
finalProgress = {
processed: progress.entries.processed,
total: progress.entries.total,
processedBytes: progress.fs.processedBytes,
percent: Math.floor((progress.entries.processed / progress.entries.total) * 100)
};
const now = Date.now();
if (now - lastUpdateTime >= updateInterval) {
lastUpdateTime = now;
await prisma.scheduledTask.update({
where: { name: this.taskName },
data: {
output: {
progress: finalProgress
}
}
});
}
});
archive.finalize();
await archiveComplete;
await CreateNotification({
type: NotificationType.SYSTEM,
title: 'system-notification',
content: 'backup-success',
useAdmin: true,
});
if (config.objectStorage === 's3') {
const { s3ClientInstance } = await FileService.getS3Client();
const fileStream = fs.createReadStream(targetFile);
await s3ClientInstance.send(new PutObjectCommand({
Bucket: config.s3Bucket,
Key: `/BLINKO_BACKUP/blinko_export.bko`,
Body: fileStream
}));
return {
filePath: `/api/s3file/BLINKO_BACKUP/blinko_export.bko`,
progress: finalProgress
};
}
return {
filePath: `/api/file/blinko_export.bko`,
progress: finalProgress
};
} catch (error) {
throw new Error(error);
}
}
static async *RestoreDB(filePath: string, ctx: Context): AsyncGenerator<RestoreResult & { progress: { current: number; total: number } }, void, unknown> {
try {
const zipFile = await yauzl.open(filePath);
let processedBytes = 0;
let entryCount = 0;
const totalEntries = await (async () => {
let count = 0;
for await (const _ of zipFile) {
count++;
}
await zipFile.close();
return count;
})();
const zipFileForExtract = await yauzl.open(filePath);
for await (const entry of zipFileForExtract) {
if (entry.filename.endsWith('/')) {
await fs.promises.mkdir(path.join(ROOT_PATH, entry.filename), { recursive: true });
continue;
}
const targetPath = path.join(ROOT_PATH, entry.filename);
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
const readStream = await entry.openReadStream();
const writeStream = fs.createWriteStream(targetPath);
await new Promise((resolve, reject) => {
readStream
.pipe(writeStream)
.on('finish', resolve)
.on('error', reject);
});
processedBytes += entry.uncompressedSize;
entryCount++;
yield {
type: 'success',
content: `extract: ${entry.filename}`,
progress: { current: entryCount, total: totalEntries }
};
}
await zipFileForExtract.close();
const backupData = JSON.parse(
fs.readFileSync(`${DBBAKUP_PATH}/bak.json`, 'utf-8')
);
const attachmentsCount = backupData.notes.reduce((acc, note) =>
acc + (note.attachments?.length || 0), 0);
const total = backupData.notes.length + attachmentsCount;
let current = 0;
const accountMap = new Map();
for (const note of backupData.notes) {
if (!note.account?.name) {
yield {
type: 'error',
content: 'Note missing account information',
error: new Error('Missing account information'),
progress: { current: 0, total }
};
continue;
}
if (!accountMap.has(note.account.name)) {
const account = await prisma.accounts.findFirst({
where: { name: note.account.name }
});
if (!account) {
const newAccount = await prisma.accounts.create({
data: {
name: note.account.name,
password: note.account.password,
role: note.account.role
}
});
accountMap.set(note.account.name, newAccount);
} else {
accountMap.set(note.account.name, account);
}
}
}
for (const note of backupData.notes) {
current++;
if (!note.account?.name) {
yield {
type: 'error',
content: 'Note missing account information',
error: new Error('Missing account information'),
progress: { current, total }
};
continue;
}
const accountInfo = accountMap.get(note.account.name);
if (!accountInfo) {
yield {
type: 'error',
content: `Account not found: ${note.account.name}`,
error: new Error('Account not found'),
progress: { current, total }
};
continue;
}
try {
await prisma.$transaction(async (tx) => {
let ctx = {
name: accountInfo.name,
sub: accountInfo.id,
role: accountInfo.role,
id: accountInfo.id,
exp: 0,
iat: 0,
}
const userCaller = createCaller(ctx);
const createdNote = await userCaller.notes.upsert({
content: note.content,
isArchived: note.isArchived,
type: note.type,
isTop: note.isTop,
isShare: note.isShare,
});
if (createdNote.id) {
const account = accountMap.get(note.account.name);
await tx.notes.update({
where: { id: createdNote.id },
data: {
accountId: account.id,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}
});
if (note.attachments?.length) {
const attachmentData = note.attachments.map(attachment => ({
...attachment,
noteId: createdNote.id
}));
await tx.attachments.createMany({
data: attachmentData,
skipDuplicates: true
});
current += note.attachments.length;
}
}
});
yield {
type: 'success',
content: note.account.name + ' - ' + note.content.slice(0, 30),
progress: { current, total }
};
} catch (error) {
yield {
type: 'error',
content: note.content.slice(0, 30),
error,
progress: { current, total }
};
continue;
}
}
} catch (error) {
yield {
type: 'error',
error,
content: `extract failed: ${error.message}`,
progress: { current: 0, total: 0 }
};
}
}
static async ExporMDFiles(params: {
baseURL: string;
startDate?: Date;
endDate?: Date;
ctx: Context;
format: 'markdown' | 'csv' | 'json';
}) {
const { baseURL, startDate, endDate, ctx, format } = params;
const notes = await prisma.notes.findMany({
where: {
createdAt: {
...(startDate && { gte: startDate }),
...(endDate && { lte: endDate })
},
accountId: Number(ctx.id)
},
select: {
id: true,
content: true,
attachments: true,
createdAt: true,
}
});
if (notes.length === 0) {
throw new Error('No notes found');
}
const exportDir = path.join(TEMP_PATH, 'exports');
const attachmentsDir = path.join(exportDir, 'files');
const zipFilePath = TEMP_PATH + `/notes_export_${Date.now()}.zip`;
try {
if (!fs.existsSync(exportDir)) {
fs.mkdirSync(exportDir, { recursive: true });
}
if (!fs.existsSync(attachmentsDir)) {
fs.mkdirSync(attachmentsDir, { recursive: true });
}
if (format === 'csv') {
const csvContent = ['ID,Content,Created At'].concat(
notes.map(note => `${note.id},"${note.content.replace(/"/g, '""')}",${note.createdAt.toISOString()}`)
).join('\n');
await writeFile(path.join(exportDir, 'notes.csv'), csvContent);
} else if (format === 'json') {
await writeFile(
path.join(exportDir, 'notes.json'),
JSON.stringify(notes, null, 2)
);
} else {
await Promise.all(notes.map(async (note) => {
let mdContent = note.content;
if (note.attachments?.length) {
await Promise.all(note.attachments.map(async (attachment) => {
try {
const response = await fetch(`${baseURL}${attachment.path}`);
const buffer = await response.arrayBuffer();
const attachmentPath = path.join(attachmentsDir, attachment.name);
//@ts-ignore
await writeFile(attachmentPath, Buffer.from(buffer));
const isImage = /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(attachment.name);
if (isImage) {
mdContent += `\n![${attachment.name}](./files/${attachment.name})`;
} else {
mdContent += `\n[${attachment.name}](./files/${attachment.name})`;
}
} catch (error) {
console.error(`Failed to download attachment: ${attachment.name}`, error);
}
}));
}
const fileName = `note-${note.id}-${note.createdAt.getTime()}.md`;
await writeFile(path.join(exportDir, fileName), mdContent);
}));
}
const zip = new AdmZip();
zip.addLocalFolder(exportDir);
zip.writeZip(zipFilePath);
fs.rmSync(exportDir, { recursive: true, force: true });
return {
success: true,
path: zipFilePath.replace(UPLOAD_FILE_PATH, ''),
fileCount: notes.length
};
} catch (error) {
try {
if (fs.existsSync(exportDir)) {
fs.rmSync(exportDir, { recursive: true, force: true });
}
if (fs.existsSync(zipFilePath)) {
fs.unlinkSync(zipFilePath);
}
} catch { }
throw error;
}
}
}