forked from FoundKeyGang/FoundKey
refactor
This commit is contained in:
parent
6d918f195a
commit
c599a304c8
2 changed files with 37 additions and 64 deletions
|
@ -39,8 +39,9 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi
|
|||
|
||||
const metaStream = fs.createWriteStream(metaPath, { flags: 'a' });
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
metaStream.write('[', err => {
|
||||
const writeMeta = (text: string): Promise<void> => {
|
||||
return new Promise<void>((res, rej) => {
|
||||
metaStream.write(text, err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
|
@ -49,6 +50,9 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi
|
|||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
await writeMeta('[');
|
||||
|
||||
const customEmojis = await Emojis.find({
|
||||
where: {
|
||||
|
@ -72,34 +76,17 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi
|
|||
logger.error(e);
|
||||
}
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
const content = JSON.stringify({
|
||||
id: exportId,
|
||||
downloaded: downloaded,
|
||||
emoji: emoji,
|
||||
});
|
||||
const isFirst = customEmojis.indexOf(emoji) === 0;
|
||||
metaStream.write(isFirst ? content : ',\n' + content, err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
await writeMeta(isFirst ? content : ',\n' + content);
|
||||
}
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
metaStream.write(']', err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
});
|
||||
});
|
||||
await writeMeta(']');
|
||||
|
||||
metaStream.end();
|
||||
|
||||
|
|
|
@ -34,8 +34,9 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Prom
|
|||
|
||||
const stream = fs.createWriteStream(path, { flags: 'a' });
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
stream.write('[', err => {
|
||||
const write = (text: string): Promise<void> => {
|
||||
return new Promise<void>((res, rej) => {
|
||||
stream.write(text, err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
|
@ -44,6 +45,9 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Prom
|
|||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
await write('[');
|
||||
|
||||
let exportedNotesCount = 0;
|
||||
let cursor: Note['id'] | null = null;
|
||||
|
@ -73,17 +77,8 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Prom
|
|||
poll = await Polls.findOneOrFail({ noteId: note.id });
|
||||
}
|
||||
const content = JSON.stringify(serialize(note, poll));
|
||||
await new Promise<void>((res, rej) => {
|
||||
const isFirst = exportedNotesCount === 0;
|
||||
stream.write(isFirst ? content : ',\n' + content, err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
});
|
||||
});
|
||||
await write(isFirst ? content : ',\n' + content);
|
||||
exportedNotesCount++;
|
||||
}
|
||||
|
||||
|
@ -94,16 +89,7 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Prom
|
|||
job.progress(exportedNotesCount / total);
|
||||
}
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
stream.write(']', err => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
rej(err);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
});
|
||||
});
|
||||
await write(']');
|
||||
|
||||
stream.end();
|
||||
logger.succ(`Exported to: ${path}`);
|
||||
|
|
Loading…
Reference in a new issue