|
| 1 | +/** |
| 2 | + * downloadUrl Computed Field Plugin |
| 3 | + * |
| 4 | + * Adds a `downloadUrl` computed field to File types in the GraphQL schema. |
| 5 | + * For public files, returns the public URL prefix + key. |
| 6 | + * For private files, generates a presigned GET URL. |
| 7 | + * |
| 8 | + * This plugin uses the GraphQLObjectType_fields hook to detect file tables |
| 9 | + * (by checking for the storage module's files table) and add the computed field. |
| 10 | + */ |
| 11 | + |
| 12 | +import type { GraphileConfig } from 'graphile-config'; |
| 13 | +import { Logger } from '@pgpmjs/logger'; |
| 14 | + |
| 15 | +import type { PresignedUrlPluginOptions } from './types'; |
| 16 | +import { generatePresignedGetUrl } from './s3-signer'; |
| 17 | + |
| 18 | +const log = new Logger('graphile-presigned-url:download-url'); |
| 19 | + |
| 20 | +/** |
| 21 | + * Creates the downloadUrl computed field plugin. |
| 22 | + * |
| 23 | + * This is a separate plugin from the main presigned URL plugin because it |
| 24 | + * uses the GraphQLObjectType_fields hook (low-level) rather than extendSchema. |
| 25 | + * The downloadUrl field needs to be added dynamically to whatever table is |
| 26 | + * the storage module's files table, which we discover at schema-build time. |
| 27 | + */ |
| 28 | +export function createDownloadUrlPlugin( |
| 29 | + options: PresignedUrlPluginOptions, |
| 30 | +): GraphileConfig.Plugin { |
| 31 | + const { s3 } = options; |
| 32 | + const downloadUrlExpirySeconds = 3600; // 1 hour for GET URLs |
| 33 | + |
| 34 | + return { |
| 35 | + name: 'PresignedUrlDownloadPlugin', |
| 36 | + version: '0.1.0', |
| 37 | + description: 'Adds downloadUrl computed field to File types', |
| 38 | + |
| 39 | + schema: { |
| 40 | + hooks: { |
| 41 | + GraphQLObjectType_fields(fields, build, context) { |
| 42 | + const { |
| 43 | + scope: { pgCodec, isPgClassType }, |
| 44 | + } = context as any; |
| 45 | + |
| 46 | + // Only process PG class types (table row types) |
| 47 | + if (!isPgClassType || !pgCodec || !pgCodec.attributes) { |
| 48 | + return fields; |
| 49 | + } |
| 50 | + |
| 51 | + const attrs = pgCodec.attributes as Record<string, any>; |
| 52 | + |
| 53 | + // Detect if this is a files table by checking for characteristic columns: |
| 54 | + // key, content_type, content_hash, status, bucket_id, is_public |
| 55 | + const hasKey = 'key' in attrs; |
| 56 | + const hasContentType = 'content_type' in attrs; |
| 57 | + const hasContentHash = 'content_hash' in attrs; |
| 58 | + const hasStatus = 'status' in attrs; |
| 59 | + const hasBucketId = 'bucket_id' in attrs; |
| 60 | + const hasIsPublic = 'is_public' in attrs; |
| 61 | + |
| 62 | + if (!hasKey || !hasContentType || !hasContentHash || !hasStatus || !hasBucketId || !hasIsPublic) { |
| 63 | + return fields; |
| 64 | + } |
| 65 | + |
| 66 | + log.debug(`Adding downloadUrl field to type: ${pgCodec.name}`); |
| 67 | + |
| 68 | + const { |
| 69 | + graphql: { GraphQLString }, |
| 70 | + } = build; |
| 71 | + |
| 72 | + return build.extend( |
| 73 | + fields, |
| 74 | + { |
| 75 | + downloadUrl: context.fieldWithHooks( |
| 76 | + { fieldName: 'downloadUrl' } as any, |
| 77 | + { |
| 78 | + description: |
| 79 | + 'URL to download this file. For public files, returns the public URL. ' + |
| 80 | + 'For private files, returns a time-limited presigned URL.', |
| 81 | + type: GraphQLString, |
| 82 | + resolve(parent: any) { |
| 83 | + const key = parent.key || parent.get?.('key'); |
| 84 | + const isPublic = parent.is_public ?? parent.get?.('is_public'); |
| 85 | + const filename = parent.filename || parent.get?.('filename'); |
| 86 | + const status = parent.status || parent.get?.('status'); |
| 87 | + |
| 88 | + if (!key) return null; |
| 89 | + |
| 90 | + // Only provide download URLs for ready/processed files |
| 91 | + if (status !== 'ready' && status !== 'processed') { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + if (isPublic && s3.publicUrlPrefix) { |
| 96 | + // Public file: return direct URL |
| 97 | + return `${s3.publicUrlPrefix}/${key}`; |
| 98 | + } |
| 99 | + |
| 100 | + // Private file: generate presigned GET URL |
| 101 | + return generatePresignedGetUrl( |
| 102 | + s3, |
| 103 | + key, |
| 104 | + downloadUrlExpirySeconds, |
| 105 | + filename || undefined, |
| 106 | + ); |
| 107 | + }, |
| 108 | + }, |
| 109 | + ), |
| 110 | + }, |
| 111 | + 'PresignedUrlDownloadPlugin adding downloadUrl field', |
| 112 | + ); |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +export default createDownloadUrlPlugin; |
0 commit comments