Skip to content

Commit

Permalink
Overwrite old avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Oct 20, 2023
1 parent 8e3b5ce commit 8426680
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
45 changes: 33 additions & 12 deletions api/resolvers/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createPresignedPost } from '../s3'

export default {
Mutation: {
getSignedPOST: async (parent, { type, size, width, height }, { models, me }) => {
getSignedPOST: async (parent, { type, size, width, height, avatar }, { models, me }) => {
if (!me) {
throw new GraphQLError('you must be logged in to get a signed url', { extensions: { code: 'FORBIDDEN' } })
}
Expand All @@ -21,19 +21,40 @@ export default {
throw new GraphQLError(`image must be less than ${IMAGE_PIXELS_MAX} pixels`, { extensions: { code: 'BAD_INPUT' } })
}

// create upload record
const upload = await models.upload.create({
data: {
type,
size,
width,
height,
userId: me.id
}
})
let uploadId
if (avatar) {
const { photoId } = await models.user.findUnique({ where: { id: me.id } })
if (photoId) uploadId = photoId
}

if (uploadId) {
// update upload record
await models.upload.update({
data: {
type,
size,
width,
height,
userId: me.id
},
where: { id: uploadId }
})
} else {
// create upload record
const upload = await models.upload.create({
data: {
type,
size,
width,
height,
userId: me.id
}
})
uploadId = upload.id
}

// get presigned POST url
return createPresignedPost({ key: String(upload.id), type, size })
return createPresignedPost({ key: String(uploadId), type, size })
}
}
}
2 changes: 1 addition & 1 deletion api/typeDefs/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gql } from 'graphql-tag'

export default gql`
extend type Mutation {
getSignedPOST(type: String!, size: Int!, width: Int!, height: Int!): SignedPost!
getSignedPOST(type: String!, size: Int!, width: Int!, height: Int!, avatar: Boolean): SignedPost!
}
type SignedPost {
Expand Down
5 changes: 3 additions & 2 deletions components/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { UPLOAD_TYPES_ALLOW } from '../lib/constants'
export default function Upload ({ as: Component, onSelect, onStarted, onError, onSuccess }) {
const [getSignedPOST] = useMutation(
gql`
mutation getSignedPOST($type: String!, $size: Int!, $width: Int!, $height: Int!) {
getSignedPOST(type: $type, size: $size, width: $width, height: $height) {
mutation getSignedPOST($type: String!, $size: Int!, $width: Int!, $height: Int!, $avatar: Boolean!) {
getSignedPOST(type: $type, size: $size, width: $width, height: $height, avatar: $avatar) {
url
fields
}
Expand All @@ -23,6 +23,7 @@ export default function Upload ({ as: Component, onSelect, onStarted, onError, o
try {
({ data } = await getSignedPOST({
variables: {
avatar: true,
type: file.type,
size: file.size,
width: img.width,
Expand Down

0 comments on commit 8426680

Please sign in to comment.