2024-09-13 15:13:34 +00:00
|
|
|
import { USER_ID, IMAGE_PIXELS_MAX, UPLOAD_SIZE_MAX, UPLOAD_SIZE_MAX_AVATAR, UPLOAD_TYPES_ALLOW, AWS_S3_URL_REGEXP, AVATAR_TYPES_ALLOW } from '@/lib/constants'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { createPresignedPost } from '@/api/s3'
|
2024-09-10 16:35:25 +00:00
|
|
|
import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
|
2024-09-13 14:26:08 +00:00
|
|
|
import { msatsToSats } from '@/lib/format'
|
2022-05-12 18:44:21 +00:00
|
|
|
|
|
|
|
export default {
|
2024-09-13 14:26:08 +00:00
|
|
|
Query: {
|
|
|
|
uploadFees: async (parent, { s3Keys }, { models, me }) => {
|
|
|
|
return uploadFees(s3Keys, { models, me })
|
|
|
|
}
|
|
|
|
},
|
2022-05-12 18:44:21 +00:00
|
|
|
Mutation: {
|
2023-11-06 20:53:33 +00:00
|
|
|
getSignedPOST: async (parent, { type, size, width, height, avatar }, { models, me }) => {
|
2022-05-12 18:44:21 +00:00
|
|
|
if (UPLOAD_TYPES_ALLOW.indexOf(type) === -1) {
|
2024-09-13 15:13:34 +00:00
|
|
|
throw new GqlInputError(`upload must be ${UPLOAD_TYPES_ALLOW.map(t => t.replace(/^(image|video)\//, '')).join(', ')}`)
|
2022-05-12 18:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size > UPLOAD_SIZE_MAX) {
|
2024-09-13 15:13:34 +00:00
|
|
|
throw new GqlInputError(`upload must be less than ${UPLOAD_SIZE_MAX / (1024 ** 2)} megabytes`)
|
2023-11-06 20:53:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 15:13:34 +00:00
|
|
|
if (avatar) {
|
|
|
|
if (AVATAR_TYPES_ALLOW.indexOf(type) === -1) {
|
|
|
|
throw new GqlInputError(`avatar must be ${AVATAR_TYPES_ALLOW.map(t => t.replace('image/', '')).join(', ')}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > UPLOAD_SIZE_MAX_AVATAR) {
|
|
|
|
throw new GqlInputError(`avatar must be less than ${UPLOAD_SIZE_MAX_AVATAR / (1024 ** 2)} megabytes`)
|
|
|
|
}
|
2022-05-12 18:44:21 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 15:41:07 +00:00
|
|
|
// width and height is 0 for videos
|
2022-05-12 18:44:21 +00:00
|
|
|
if (width * height > IMAGE_PIXELS_MAX) {
|
2024-09-10 16:35:25 +00:00
|
|
|
throw new GqlInputError(`image must be less than ${IMAGE_PIXELS_MAX} pixels`)
|
2022-05-12 18:44:21 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 15:41:07 +00:00
|
|
|
const fileParams = {
|
2023-11-06 20:53:33 +00:00
|
|
|
type,
|
|
|
|
size,
|
|
|
|
width,
|
|
|
|
height,
|
2024-06-03 16:26:19 +00:00
|
|
|
userId: me?.id || USER_ID.anon,
|
2023-11-06 20:53:33 +00:00
|
|
|
paid: false
|
|
|
|
}
|
2022-05-12 18:44:21 +00:00
|
|
|
|
2023-11-06 20:53:33 +00:00
|
|
|
if (avatar) {
|
2024-09-10 16:35:25 +00:00
|
|
|
if (!me) throw new GqlAuthenticationError()
|
2024-09-13 15:41:07 +00:00
|
|
|
fileParams.paid = undefined
|
2023-11-06 20:53:33 +00:00
|
|
|
}
|
2022-05-12 18:44:21 +00:00
|
|
|
|
2024-09-13 15:41:07 +00:00
|
|
|
const upload = await models.upload.create({ data: { ...fileParams } })
|
2024-10-07 18:20:02 +00:00
|
|
|
return createPresignedPost({ key: String(upload.id), type, size })
|
2022-05-12 18:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 14:26:08 +00:00
|
|
|
|
|
|
|
export function uploadIdsFromText (text, { models }) {
|
|
|
|
if (!text) return []
|
|
|
|
return [...new Set([...text.matchAll(AWS_S3_URL_REGEXP)].map(m => Number(m[1])))]
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function uploadFees (s3Keys, { models, me }) {
|
|
|
|
// returns info object in this format:
|
|
|
|
// { bytes24h: int, bytesUnpaid: int, nUnpaid: int, uploadFeesMsats: BigInt }
|
|
|
|
const [info] = await models.$queryRawUnsafe('SELECT * FROM upload_fees($1::INTEGER, $2::INTEGER[])', me ? me.id : USER_ID.anon, s3Keys)
|
|
|
|
const uploadFees = msatsToSats(info.uploadFeesMsats)
|
|
|
|
const totalFeesMsats = info.nUnpaid * Number(info.uploadFeesMsats)
|
|
|
|
const totalFees = msatsToSats(totalFeesMsats)
|
|
|
|
return { ...info, uploadFees, totalFees, totalFeesMsats }
|
|
|
|
}
|