This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/server/models/Attachment.js
Tom Moor 09dea295a2 fix: Cleanup S3 Attachments (#1217)
* fix: Server error if attempting to load an unknown attachment

* fix: Migration should cascade delete to document attachments

* fix: Delete S3 attachments along with documents
2020-03-28 15:56:01 -07:00

65 lines
1.3 KiB
JavaScript

// @flow
import path from 'path';
import { DataTypes, sequelize } from '../sequelize';
import { deleteFromS3 } from '../utils/s3';
const Attachment = sequelize.define(
'attachment',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
key: {
type: DataTypes.STRING,
allowNull: false,
},
url: {
type: DataTypes.STRING,
allowNull: false,
},
contentType: {
type: DataTypes.STRING,
allowNull: false,
},
size: {
type: DataTypes.BIGINT,
allowNull: false,
},
acl: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'public-read',
validate: {
isIn: [['private', 'public-read']],
},
},
},
{
getterMethods: {
name: function() {
return path.parse(this.key).base;
},
redirectUrl: function() {
return `/api/attachments.redirect?id=${this.id}`;
},
isPrivate: function() {
return this.acl === 'private';
},
},
}
);
Attachment.beforeDestroy(async model => {
await deleteFromS3(model.key);
});
Attachment.associate = models => {
Attachment.belongsTo(models.Team);
Attachment.belongsTo(models.Document);
Attachment.belongsTo(models.User);
};
export default Attachment;