fix: Attachments should not always be deleted with their original document (#1715)

* fix: Attachments should not be deleted when their original document is deleted when referenced elsewhere

* fix: Attachments deleted prematurely when docs are placed in trash

* mock

* restore hook, cascading delete was the issue
This commit is contained in:
Tom Moor
2020-12-14 19:55:22 -08:00
committed by GitHub
parent 3dbe54ac1e
commit e2e66954b5
5 changed files with 207 additions and 57 deletions

View File

@ -1,6 +1,7 @@
// @flow
import { takeRight } from "lodash";
import { Attachment, Document, User } from "../models";
import parseAttachmentIds from "../utils/parseAttachmentIds";
import { getSignedImageUrl } from "../utils/s3";
import presentUser from "./user";
@ -8,13 +9,9 @@ type Options = {
isPublic?: boolean,
};
const attachmentRegex = /!\[.*?\]\(\/api\/attachments\.redirect\?id=(?<id>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\)/gi;
// replaces attachments.redirect urls with signed/authenticated url equivalents
async function replaceImageAttachments(text) {
const attachmentIds = [...text.matchAll(attachmentRegex)].map(
(match) => match.groups && match.groups.id
);
async function replaceImageAttachments(text: string) {
const attachmentIds = parseAttachmentIds(text);
for (const id of attachmentIds) {
const attachment = await Attachment.findByPk(id);