Skip to content

Commit

Permalink
Also delete objects in S3
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis authored and ekzyis committed Oct 19, 2023
1 parent 323fae1 commit ab74ed6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
11 changes: 7 additions & 4 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { advSchema, amountSchema, bountySchema, commentSchema, discussionSchema,
import { sendUserNotification } from '../webPush'
import { defaultCommentSort } from '../../lib/item'
import { notifyItemParents, notifyUserSubscribers, notifyZapped } from '../../lib/push-notifications'
import { deleteObject } from '../s3'

export async function commentFilterClause (me, models) {
let clause = ` AND ("Item"."weightedVotes" - "Item"."weightedDownVotes" > -${ITEM_FILTER_THRESHOLD}`
Expand Down Expand Up @@ -757,13 +758,15 @@ export default {
throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } })
}

id = Number(id)

const img = await models.upload.findUnique({ where: { id } })
const img = await models.upload.findUnique({ where: { id: Number(id) } })
if (img.userId !== me.id) {
throw new GraphQLError('not your image', { extensions: { code: 'FORBIDDEN' } })
}
await models.upload.delete({ where: { id } })
if (img.itemId) {
throw new GraphQLError('image already included in an item', { extensions: { code: 'BAD_INPUT' } })
}
await models.upload.delete({ where: { id: Number(id) } })
await deleteObject(id)

return id
}
Expand Down
29 changes: 3 additions & 26 deletions api/resolvers/upload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { GraphQLError } from 'graphql'
import AWS from 'aws-sdk'
import { IMAGE_PIXELS_MAX, UPLOAD_SIZE_MAX, UPLOAD_TYPES_ALLOW } from '../../lib/constants'

const bucketRegion = 'us-east-1'

AWS.config.update({
region: bucketRegion
})
import { createPresignedPost } from '../s3'

export default {
Mutation: {
Expand Down Expand Up @@ -38,25 +32,8 @@ export default {
}
})

// get presigned POST ur
const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
const res = await new Promise((resolve, reject) => {
s3.createPresignedPost({
Bucket: process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET,
Fields: {
key: String(upload.id)
},
Expires: 300,
Conditions: [
{ 'Content-Type': type },
{ 'Cache-Control': 'max-age=31536000' },
{ acl: 'public-read' },
['content-length-range', size, size]
]
}, (err, preSigned) => { if (err) { reject(err) } else { resolve(preSigned) } })
})

return res
// get presigned POST url
return createPresignedPost({ key: String(upload.id), type, size })
}
}
}
35 changes: 35 additions & 0 deletions api/s3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import AWS from 'aws-sdk'

const bucketRegion = 'us-east-1'
const Bucket = process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET

AWS.config.update({
region: bucketRegion
})

export function createPresignedPost ({ key, type, size }) {
const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
return new Promise((resolve, reject) => {
s3.createPresignedPost({
Bucket,
Fields: { key },
Expires: 300,
Conditions: [
{ 'Content-Type': type },
{ 'Cache-Control': 'max-age=31536000' },
{ acl: 'public-read' },
['content-length-range', size, size]
]
}, (err, preSigned) => { err ? reject(err) : resolve(preSigned) })
})
}

export function deleteObject (key) {
const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
return new Promise((resolve, reject) => {
s3.deleteObject({
Bucket,
Key: key
}, (err, data) => { err ? reject(err) : resolve(key) })
})
}

0 comments on commit ab74ed6

Please sign in to comment.