Overwrite old avatar
This commit is contained in:
parent
ef0506d7c6
commit
eb4700070f
@ -4,7 +4,7 @@ import { createPresignedPost } from '../s3'
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
Mutation: {
|
Mutation: {
|
||||||
getSignedPOST: async (parent, { type, size, width, height }, { models, me }) => {
|
getSignedPOST: async (parent, { type, size, width, height, avatar }, { models, me }) => {
|
||||||
if (!me) {
|
if (!me) {
|
||||||
throw new GraphQLError('you must be logged in to get a signed url', { extensions: { code: 'FORBIDDEN' } })
|
throw new GraphQLError('you must be logged in to get a signed url', { extensions: { code: 'FORBIDDEN' } })
|
||||||
}
|
}
|
||||||
@ -21,19 +21,30 @@ export default {
|
|||||||
throw new GraphQLError(`image must be less than ${IMAGE_PIXELS_MAX} pixels`, { extensions: { code: 'BAD_INPUT' } })
|
throw new GraphQLError(`image must be less than ${IMAGE_PIXELS_MAX} pixels`, { extensions: { code: 'BAD_INPUT' } })
|
||||||
}
|
}
|
||||||
|
|
||||||
// create upload record
|
const data = {
|
||||||
const upload = await models.upload.create({
|
|
||||||
data: {
|
|
||||||
type,
|
type,
|
||||||
size,
|
size,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
userId: me.id
|
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, where: { id: uploadId } })
|
||||||
|
} else {
|
||||||
|
// create upload record
|
||||||
|
const upload = await models.upload.create({ data })
|
||||||
|
uploadId = upload.id
|
||||||
|
}
|
||||||
|
|
||||||
// get presigned POST url
|
// get presigned POST url
|
||||||
return createPresignedPost({ key: String(upload.id), type, size })
|
return createPresignedPost({ key: String(uploadId), type, size })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { gql } from 'graphql-tag'
|
|||||||
|
|
||||||
export default gql`
|
export default gql`
|
||||||
extend type Mutation {
|
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 {
|
type SignedPost {
|
||||||
|
@ -5,8 +5,8 @@ import { UPLOAD_TYPES_ALLOW } from '../lib/constants'
|
|||||||
export default function Upload ({ as: Component, onSelect, onStarted, onError, onSuccess }) {
|
export default function Upload ({ as: Component, onSelect, onStarted, onError, onSuccess }) {
|
||||||
const [getSignedPOST] = useMutation(
|
const [getSignedPOST] = useMutation(
|
||||||
gql`
|
gql`
|
||||||
mutation getSignedPOST($type: String!, $size: Int!, $width: Int!, $height: Int!) {
|
mutation getSignedPOST($type: String!, $size: Int!, $width: Int!, $height: Int!, $avatar: Boolean!) {
|
||||||
getSignedPOST(type: $type, size: $size, width: $width, height: $height) {
|
getSignedPOST(type: $type, size: $size, width: $width, height: $height, avatar: $avatar) {
|
||||||
url
|
url
|
||||||
fields
|
fields
|
||||||
}
|
}
|
||||||
@ -23,6 +23,7 @@ export default function Upload ({ as: Component, onSelect, onStarted, onError, o
|
|||||||
try {
|
try {
|
||||||
({ data } = await getSignedPOST({
|
({ data } = await getSignedPOST({
|
||||||
variables: {
|
variables: {
|
||||||
|
avatar: true,
|
||||||
type: file.type,
|
type: file.type,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
width: img.width,
|
width: img.width,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user