chore: Refactor file storage (#5711)

This commit is contained in:
Tom Moor
2023-08-20 10:04:34 -04:00
committed by GitHub
parent 4354e1055e
commit 74722b80f2
68 changed files with 496 additions and 313 deletions

View File

@@ -10,13 +10,7 @@ import {
DataType,
IsNumeric,
} from "sequelize-typescript";
import {
publicS3Endpoint,
deleteFromS3,
getFileStream,
getSignedUrl,
getFileBuffer,
} from "@server/utils/s3";
import FileStorage from "@server/storage/files";
import Document from "./Document";
import Team from "./Team";
import User from "./User";
@@ -76,14 +70,14 @@ class Attachment extends IdModel {
* Get the contents of this attachment as a readable stream.
*/
get stream() {
return getFileStream(this.key);
return FileStorage.getFileStream(this.key);
}
/**
* Get the contents of this attachment as a buffer.
*/
get buffer() {
return getFileBuffer(this.key);
return FileStorage.getFileBuffer(this.key);
}
/**
@@ -105,21 +99,21 @@ class Attachment extends IdModel {
* a signed URL must be used.
*/
get canonicalUrl() {
return encodeURI(`${publicS3Endpoint()}/${this.key}`);
return encodeURI(`${FileStorage.getPublicEndpoint()}/${this.key}`);
}
/**
* Get a signed URL with the default expirt to download the attachment from storage.
*/
get signedUrl() {
return getSignedUrl(this.key);
return FileStorage.getSignedUrl(this.key);
}
// hooks
@BeforeDestroy
static async deleteAttachmentFromS3(model: Attachment) {
await deleteFromS3(model.key);
await FileStorage.deleteFile(model.key);
}
// associations

View File

@@ -13,7 +13,7 @@ import {
FileOperationState,
FileOperationType,
} from "@shared/types";
import { deleteFromS3, getFileStream } from "@server/utils/s3";
import FileStorage from "@server/storage/files";
import Collection from "./Collection";
import Team from "./Team";
import User from "./User";
@@ -67,7 +67,7 @@ class FileOperation extends IdModel {
expire = async function () {
this.state = FileOperationState.Expired;
try {
await deleteFromS3(this.key);
await FileStorage.deleteFile(this.key);
} catch (err) {
if (err.retryable) {
throw err;
@@ -80,14 +80,14 @@ class FileOperation extends IdModel {
* The file operation contents as a readable stream.
*/
get stream() {
return getFileStream(this.key);
return FileStorage.getFileStream(this.key);
}
// hooks
@BeforeDestroy
static async deleteFileFromS3(model: FileOperation) {
await deleteFromS3(model.key);
await FileStorage.deleteFile(model.key);
}
// associations

View File

@@ -1,6 +1,6 @@
import isNil from "lodash/isNil";
import vaults from "@server/database/vaults";
import Logger from "@server/logging/Logger";
import vaults from "@server/storage/vaults";
const key = "sequelize:vault";

View File

@@ -19,9 +19,9 @@ import { trace } from "@server/logging/tracing";
import type Document from "@server/models/Document";
import type Revision from "@server/models/Revision";
import User from "@server/models/User";
import FileStorage from "@server/storage/files";
import diff from "@server/utils/diff";
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
import { getSignedUrl } from "@server/utils/s3";
import Attachment from "../Attachment";
import ProsemirrorHelper from "./ProsemirrorHelper";
@@ -324,7 +324,10 @@ export default class DocumentHelper {
});
if (attachment) {
const signedUrl = await getSignedUrl(attachment.key, expiresIn);
const signedUrl = await FileStorage.getSignedUrl(
attachment.key,
expiresIn
);
text = text.replace(
new RegExp(escapeRegExp(attachment.redirectUrl), "g"),
signedUrl

View File

@@ -5,12 +5,12 @@ import map from "lodash/map";
import queryParser from "pg-tsquery";
import { Op, QueryTypes, WhereOptions } from "sequelize";
import { DateFilter } from "@shared/types";
import { sequelize } from "@server/database/sequelize";
import Collection from "@server/models/Collection";
import Document from "@server/models/Document";
import Share from "@server/models/Share";
import Team from "@server/models/Team";
import User from "@server/models/User";
import { sequelize } from "@server/storage/database";
type SearchResponse = {
results: {