Compare commits

..

No commits in common. "4bc517ca89a137f81d94fa8d89a97b62afdbb4be" and "1bb5021c5479ad4a6bfad2714a4a4f2b6ba02493" have entirely different histories.

View file

@ -1,5 +1,4 @@
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import * as vm from 'node:vm';
import * as crypto from 'node:crypto';
import { Inject, Injectable } from '@nestjs/common';
@ -52,7 +51,7 @@ export class ImportNotesProcessorService {
@bindThis
private async uploadFiles(dir: string, user: MiUser, folder?: MiDriveFolder['id']) {
const fileList = await fsp.readdir(dir);
const fileList = await fs.promises.readdir(dir);
for await (const file of fileList) {
const name = `${dir}/${file}`;
if (fs.statSync(name).isDirectory()) {
@ -132,7 +131,7 @@ export class ImportNotesProcessorService {
}
@bindThis
private parseTwitterFile(str : string) : { tweet: object }[] {
private parseTwitterFile(str : string) : null | { tweet: object }[] {
const jsonStr = str.replace(/^\s*window\.YTD\.tweets\.part0\s*=\s*/, '');
try {
@ -176,7 +175,7 @@ export class ImportNotesProcessorService {
const destPath = path + '/twitter.zip';
try {
await fsp.writeFile(destPath, '', 'binary');
await fs.promises.writeFile(destPath, '', 'binary');
await this.downloadService.downloadUrl(file.url, destPath);
} catch (e) { // TODO: 何度か再試行
if (e instanceof Error || typeof e === 'string') {
@ -188,11 +187,11 @@ export class ImportNotesProcessorService {
const outputPath = path + '/twitter';
try {
this.logger.succ(`Unzipping to ${outputPath}`);
ZipReader.withDestinationPath(outputPath).viaBuffer(await fsp.readFile(destPath));
ZipReader.withDestinationPath(outputPath).viaBuffer(await fs.promises.readFile(destPath));
const unprocessedTweets = this.parseTwitterFile(await fsp.readFile(outputPath + '/data/tweets.js', 'utf-8'));
const unprocessedTweets = this.parseTwitterFile(await fs.promises.readFile(outputPath + '/data/tweets.js', 'utf-8'));
const tweets = unprocessedTweets.map(e => e.tweet);
const tweets = unprocessedTweets.map(e=>e.tweet);
const processedTweets = await this.recreateChain(['id_str'], ['in_reply_to_status_id_str'], tweets, false);
this.queueService.createImportTweetsToDbJob(job.data.user, processedTweets, null);
} finally {
@ -206,7 +205,7 @@ export class ImportNotesProcessorService {
const destPath = path + '/facebook.zip';
try {
await fsp.writeFile(destPath, '', 'binary');
await fs.promises.writeFile(destPath, '', 'binary');
await this.downloadService.downloadUrl(file.url, destPath);
} catch (e) { // TODO: 何度か再試行
if (e instanceof Error || typeof e === 'string') {
@ -218,8 +217,8 @@ export class ImportNotesProcessorService {
const outputPath = path + '/facebook';
try {
this.logger.succ(`Unzipping to ${outputPath}`);
ZipReader.withDestinationPath(outputPath).viaBuffer(await fsp.readFile(destPath));
const postsJson = await fsp.readFile(outputPath + '/your_activity_across_facebook/posts/your_posts__check_ins__photos_and_videos_1.json', 'utf-8');
ZipReader.withDestinationPath(outputPath).viaBuffer(await fs.promises.readFile(destPath));
const postsJson = await fs.promises.readFile(outputPath + '/your_activity_across_facebook/posts/your_posts__check_ins__photos_and_videos_1.json', 'utf-8');
const posts = JSON.parse(postsJson);
const facebookFolder = await this.driveFoldersRepository.findOneBy({ name: 'Facebook', userId: job.data.user.id, parentId: folder?.id });
if (facebookFolder == null && folder) {
@ -239,7 +238,7 @@ export class ImportNotesProcessorService {
const destPath = path + '/unknown.zip';
try {
await fsp.writeFile(destPath, '', 'binary');
await fs.promises.writeFile(destPath, '', 'binary');
await this.downloadService.downloadUrl(file.url, destPath);
} catch (e) { // TODO: 何度か再試行
if (e instanceof Error || typeof e === 'string') {
@ -251,11 +250,11 @@ export class ImportNotesProcessorService {
const outputPath = path + '/unknown';
try {
this.logger.succ(`Unzipping to ${outputPath}`);
ZipReader.withDestinationPath(outputPath).viaBuffer(await fsp.readFile(destPath));
ZipReader.withDestinationPath(outputPath).viaBuffer(await fs.promises.readFile(destPath));
const isInstagram = type === 'Instagram' || fs.existsSync(outputPath + '/instagram_live') || fs.existsSync(outputPath + '/instagram_ads_and_businesses');
const isOutbox = type === 'Mastodon' || fs.existsSync(outputPath + '/outbox.json');
if (isInstagram) {
const postsJson = await fsp.readFile(outputPath + '/content/posts_1.json', 'utf-8');
const postsJson = await fs.promises.readFile(outputPath + '/content/posts_1.json', 'utf-8');
const posts = JSON.parse(postsJson);
const igFolder = await this.driveFoldersRepository.findOneBy({ name: 'Instagram', userId: job.data.user.id, parentId: folder?.id });
if (igFolder == null && folder) {
@ -265,16 +264,16 @@ export class ImportNotesProcessorService {
}
this.queueService.createImportIGToDbJob(job.data.user, posts);
} else if (isOutbox) {
const actorJson = await fsp.readFile(outputPath + '/actor.json', 'utf-8');
const actorJson = await fs.promises.readFile(outputPath + '/actor.json', 'utf-8');
const actor = JSON.parse(actorJson);
const isPleroma = actor['@context'].some((v: any) => typeof v === 'string' && v.match(/litepub(.*)/));
if (isPleroma) {
const outboxJson = await fsp.readFile(outputPath + '/outbox.json', 'utf-8');
const outboxJson = await fs.promises.readFile(outputPath + '/outbox.json', 'utf-8');
const outbox = JSON.parse(outboxJson);
const processedToots = await this.recreateChain(['object', 'id'], ['object', 'inReplyTo'], outbox.orderedItems.filter((x: any) => x.type === 'Create' && x.object.type === 'Note'), true);
this.queueService.createImportPleroToDbJob(job.data.user, processedToots, null);
} else {
const outboxJson = await fsp.readFile(outputPath + '/outbox.json', 'utf-8');
const outboxJson = await fs.promises.readFile(outputPath + '/outbox.json', 'utf-8');
const outbox = JSON.parse(outboxJson);
let mastoFolder = await this.driveFoldersRepository.findOneBy({ name: 'Mastodon', userId: job.data.user.id, parentId: folder?.id });
if (mastoFolder == null && folder) {
@ -297,7 +296,7 @@ export class ImportNotesProcessorService {
this.logger.info(`Temp dir is ${path}`);
try {
await fsp.writeFile(path, '', 'utf-8');
await fs.promises.writeFile(path, '', 'utf-8');
await this.downloadService.downloadUrl(file.url, path);
} catch (e) { // TODO: 何度か再試行
if (e instanceof Error || typeof e === 'string') {
@ -306,7 +305,7 @@ export class ImportNotesProcessorService {
throw e;
}
const notesJson = await fsp.readFile(path, 'utf-8');
const notesJson = await fs.promises.readFile(path, 'utf-8');
const notes = JSON.parse(notesJson);
const processedNotes = await this.recreateChain(['id'], ['replyId'], notes, false);
this.queueService.createImportKeyNotesToDbJob(job.data.user, processedNotes, null);
@ -585,7 +584,7 @@ export class ImportNotesProcessorService {
try {
const date = new Date(tweet.created_at);
const decodedText = tweet.full_text.replaceAll('&gt;', '>').replaceAll('&lt;', '<').replaceAll('&amp;', '&');
const decodedText = tweet.full_text.replaceAll('&gt;','>').replaceAll('&lt;','<').replaceAll('&amp;','&');
const textReplaceURLs = tweet.entities.urls && tweet.entities.urls.length > 0 ? await replaceTwitterUrls(decodedText, tweet.entities.urls) : decodedText;
const text = tweet.entities.user_mentions && tweet.entities.user_mentions.length > 0 ? await replaceTwitterMentions(textReplaceURLs, tweet.entities.user_mentions) : textReplaceURLs;
const files: MiDriveFile[] = [];