2019-04-07 07:50:36 -05:00
|
|
|
import { DriveFile } from '../../models/entities/drive-file';
|
|
|
|
import { InternalStorage } from './internal-storage';
|
2020-02-13 08:08:33 -06:00
|
|
|
import { DriveFiles, Instances, Notes, Users } from '../../models';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { driveChart, perUserDriveChart, instanceChart } from '../chart';
|
2019-05-27 02:54:47 -05:00
|
|
|
import { createDeleteObjectStorageFileJob } from '../../queue';
|
2019-07-01 07:12:14 -05:00
|
|
|
import { fetchMeta } from '../../misc/fetch-meta';
|
2019-07-27 19:49:02 -05:00
|
|
|
import { getS3 } from './s3';
|
2019-12-31 02:23:47 -06:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2020-02-13 08:08:33 -06:00
|
|
|
import { Note } from '../../models/entities/note';
|
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
|
|
|
import renderDelete from '../../remote/activitypub/renderer/delete';
|
|
|
|
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
|
|
|
|
import config from '../../config';
|
|
|
|
import { deliverToFollowers } from '../../remote/activitypub/deliver-manager';
|
2018-06-14 19:53:30 -05:00
|
|
|
|
2019-05-27 02:54:47 -05:00
|
|
|
export async function deleteFile(file: DriveFile, isExpired = false) {
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.storedInternal) {
|
2019-04-12 11:43:22 -05:00
|
|
|
InternalStorage.del(file.accessKey!);
|
2018-08-15 17:17:04 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.thumbnailUrl) {
|
2019-04-12 11:43:22 -05:00
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
2018-08-15 17:17:04 -05:00
|
|
|
}
|
2018-11-25 13:25:48 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.webpublicUrl) {
|
2019-04-12 11:43:22 -05:00
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
2018-11-25 13:25:48 -06:00
|
|
|
}
|
2019-04-09 09:07:08 -05:00
|
|
|
} else if (!file.isLink) {
|
2019-05-27 02:54:47 -05:00
|
|
|
createDeleteObjectStorageFileJob(file.accessKey!);
|
2018-07-24 18:01:12 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.thumbnailUrl) {
|
2019-05-27 02:54:47 -05:00
|
|
|
createDeleteObjectStorageFileJob(file.thumbnailAccessKey!);
|
2018-06-14 19:53:30 -05:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.webpublicUrl) {
|
2019-05-27 02:54:47 -05:00
|
|
|
createDeleteObjectStorageFileJob(file.webpublicAccessKey!);
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2018-06-14 19:53:30 -05:00
|
|
|
}
|
2018-08-18 09:56:44 -05:00
|
|
|
|
2019-07-01 07:12:14 -05:00
|
|
|
postProcess(file, isExpired);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteFileSync(file: DriveFile, isExpired = false) {
|
|
|
|
if (file.storedInternal) {
|
|
|
|
InternalStorage.del(file.accessKey!);
|
|
|
|
|
|
|
|
if (file.thumbnailUrl) {
|
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.webpublicUrl) {
|
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
|
|
|
}
|
|
|
|
} else if (!file.isLink) {
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
promises.push(deleteObjectStorageFile(file.accessKey!));
|
|
|
|
|
|
|
|
if (file.thumbnailUrl) {
|
|
|
|
promises.push(deleteObjectStorageFile(file.thumbnailAccessKey!));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.webpublicUrl) {
|
|
|
|
promises.push(deleteObjectStorageFile(file.webpublicAccessKey!));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
postProcess(file, isExpired);
|
|
|
|
}
|
|
|
|
|
2020-02-13 08:08:33 -06:00
|
|
|
async function postProcess(file: DriveFile, isExpired = false) {
|
2019-04-07 07:50:36 -05:00
|
|
|
// リモートファイル期限切れ削除後は直リンクにする
|
2019-04-12 11:43:22 -05:00
|
|
|
if (isExpired && file.userHost !== null && file.uri != null) {
|
2019-04-07 07:50:36 -05:00
|
|
|
DriveFiles.update(file.id, {
|
2019-04-09 09:07:08 -05:00
|
|
|
isLink: true,
|
2019-04-07 07:50:36 -05:00
|
|
|
url: file.uri,
|
2020-01-01 11:45:05 -06:00
|
|
|
thumbnailUrl: null,
|
|
|
|
webpublicUrl: null,
|
2019-11-05 07:11:47 -06:00
|
|
|
size: 0,
|
2019-12-31 02:23:47 -06:00
|
|
|
// ローカルプロキシ用
|
|
|
|
accessKey: uuid(),
|
|
|
|
thumbnailAccessKey: 'thumbnail-' + uuid(),
|
|
|
|
webpublicAccessKey: 'webpublic-' + uuid(),
|
2018-11-25 13:25:48 -06:00
|
|
|
});
|
2019-04-07 07:50:36 -05:00
|
|
|
} else {
|
|
|
|
DriveFiles.delete(file.id);
|
2019-04-16 12:11:22 -05:00
|
|
|
|
|
|
|
// TODO: トランザクション
|
2020-02-13 08:08:33 -06:00
|
|
|
const relatedNotes = await findRelatedNotes(file.id);
|
|
|
|
for (const relatedNote of relatedNotes) { // for each note with deleted driveFile
|
|
|
|
const cascadingNotes = (await findCascadingNotes(relatedNote)).filter(note => !note.localOnly);
|
|
|
|
for (const cascadingNote of cascadingNotes) { // for each notes subject to cascade deletion
|
|
|
|
if (!cascadingNote.user) continue;
|
|
|
|
if (!Users.isLocalUser(cascadingNote.user)) continue;
|
|
|
|
const content = renderActivity(renderDelete(renderTombstone(`${config.url}/notes/${cascadingNote.id}`), cascadingNote.user));
|
|
|
|
deliverToFollowers(cascadingNote.user, content); // federate delete msg
|
|
|
|
}
|
|
|
|
if (!relatedNote.user) continue;
|
|
|
|
if (Users.isLocalUser(relatedNote.user)) {
|
|
|
|
const content = renderActivity(renderDelete(renderTombstone(`${config.url}/notes/${relatedNote.id}`), relatedNote.user));
|
|
|
|
deliverToFollowers(relatedNote.user, content);
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 12:37:37 -05:00
|
|
|
Notes.createQueryBuilder().delete()
|
2020-02-13 08:08:33 -06:00
|
|
|
.where(':id = ANY("fileIds")', { id: file.id })
|
2019-04-16 12:11:22 -05:00
|
|
|
.execute();
|
2018-11-25 13:25:48 -06:00
|
|
|
}
|
|
|
|
|
2018-08-18 09:56:44 -05:00
|
|
|
// 統計を更新
|
2018-10-22 15:36:35 -05:00
|
|
|
driveChart.update(file, false);
|
|
|
|
perUserDriveChart.update(file, false);
|
2019-04-07 07:50:36 -05:00
|
|
|
if (file.userHost !== null) {
|
2019-02-08 01:58:57 -06:00
|
|
|
instanceChart.updateDrive(file, false);
|
2019-04-07 07:50:36 -05:00
|
|
|
Instances.decrement({ host: file.userHost }, 'driveUsage', file.size);
|
|
|
|
Instances.decrement({ host: file.userHost }, 'driveFiles', 1);
|
2019-02-08 01:58:57 -06:00
|
|
|
}
|
2018-06-14 19:53:30 -05:00
|
|
|
}
|
2019-07-01 07:12:14 -05:00
|
|
|
|
|
|
|
export async function deleteObjectStorageFile(key: string) {
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-07-27 19:49:02 -05:00
|
|
|
const s3 = getS3(meta);
|
2019-07-01 07:12:14 -05:00
|
|
|
|
2019-07-27 19:49:02 -05:00
|
|
|
await s3.deleteObject({
|
|
|
|
Bucket: meta.objectStorageBucket!,
|
|
|
|
Key: key
|
|
|
|
}).promise();
|
2019-07-01 07:12:14 -05:00
|
|
|
}
|
2020-02-13 08:08:33 -06:00
|
|
|
|
|
|
|
async function findRelatedNotes(fileId: string) {
|
|
|
|
// NOTE: When running raw query, TypeORM converts field name to lowercase. Wrap in quotes to prevent conversion.
|
|
|
|
const relatedNotes = await Notes.createQueryBuilder('note').where(':id = ANY("fileIds")', { id: fileId }).getMany();
|
|
|
|
for (const relatedNote of relatedNotes) {
|
|
|
|
const user = await Users.findOne({ id: relatedNote.userId });
|
|
|
|
if (user)
|
|
|
|
relatedNote.user = user;
|
|
|
|
}
|
|
|
|
return relatedNotes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findCascadingNotes(note: Note) {
|
|
|
|
const cascadingNotes: Note[] = [];
|
|
|
|
|
|
|
|
const recursive = async (noteId: string) => {
|
|
|
|
const query = Notes.createQueryBuilder('note')
|
|
|
|
.where('note.replyId = :noteId', { noteId })
|
|
|
|
.leftJoinAndSelect('note.user', 'user');
|
|
|
|
const replies = await query.getMany();
|
|
|
|
for (const reply of replies) {
|
|
|
|
cascadingNotes.push(reply);
|
|
|
|
await recursive(reply.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await recursive(note.id);
|
|
|
|
|
|
|
|
return cascadingNotes.filter(note => note.userHost === null); // filter out non-local users
|
|
|
|
}
|