|
| 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 | + * Detection: Uses the `@storageFiles` smart tag on the codec (table). |
| 9 | + * The storage module generator in constructive-db sets this tag on the |
| 10 | + * generated files table via a smart comment: |
| 11 | + * COMMENT ON TABLE files IS E'@storageFiles\nStorage files table'; |
| 12 | + * |
| 13 | + * This is explicit and reliable — no duck-typing on column names. |
| 14 | + */ |
| 15 | + |
| 16 | +import type { GraphileConfig } from 'graphile-config'; |
| 17 | +import { Logger } from '@pgpmjs/logger'; |
| 18 | + |
| 19 | +import type { PresignedUrlPluginOptions } from './types'; |
| 20 | +import { generatePresignedGetUrl } from './s3-signer'; |
| 21 | +import { getStorageModuleConfig } from './storage-module-cache'; |
| 22 | + |
| 23 | +const log = new Logger('graphile-presigned-url:download-url'); |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates the downloadUrl computed field plugin. |
| 27 | + * |
| 28 | + * This is a separate plugin from the main presigned URL plugin because it |
| 29 | + * uses the GraphQLObjectType_fields hook (low-level) rather than extendSchema. |
| 30 | + * The downloadUrl field needs to be added dynamically to whatever table is |
| 31 | + * the storage module's files table, which we discover at schema-build time |
| 32 | + * via the `@storageFiles` smart tag. |
| 33 | + */ |
| 34 | +export function createDownloadUrlPlugin( |
| 35 | + options: PresignedUrlPluginOptions, |
| 36 | +): GraphileConfig.Plugin { |
| 37 | + const { s3 } = options; |
| 38 | + |
| 39 | + return { |
| 40 | + name: 'PresignedUrlDownloadPlugin', |
| 41 | + version: '0.1.0', |
| 42 | + description: 'Adds downloadUrl computed field to File types tagged with @storageFiles', |
| 43 | + |
| 44 | + schema: { |
| 45 | + hooks: { |
| 46 | + GraphQLObjectType_fields(fields, build, context) { |
| 47 | + const { |
| 48 | + scope: { pgCodec, isPgClassType }, |
| 49 | + } = context as any; |
| 50 | + |
| 51 | + // Only process PG class types (table row types) |
| 52 | + if (!isPgClassType || !pgCodec || !pgCodec.attributes) { |
| 53 | + return fields; |
| 54 | + } |
| 55 | + |
| 56 | + // Check for @storageFiles smart tag — set by the storage module generator |
| 57 | + const tags = (pgCodec.extensions as any)?.tags; |
| 58 | + if (!tags?.storageFiles) { |
| 59 | + return fields; |
| 60 | + } |
| 61 | + |
| 62 | + log.debug(`Adding downloadUrl field to type: ${pgCodec.name} (has @storageFiles tag)`); |
| 63 | + |
| 64 | + const { |
| 65 | + graphql: { GraphQLString }, |
| 66 | + } = build; |
| 67 | + |
| 68 | + return build.extend( |
| 69 | + fields, |
| 70 | + { |
| 71 | + downloadUrl: context.fieldWithHooks( |
| 72 | + { fieldName: 'downloadUrl' } as any, |
| 73 | + { |
| 74 | + description: |
| 75 | + 'URL to download this file. For public files, returns the public URL. ' + |
| 76 | + 'For private files, returns a time-limited presigned URL.', |
| 77 | + type: GraphQLString, |
| 78 | + async resolve(parent: any, _args: any, context: any) { |
| 79 | + const key = parent.key || parent.get?.('key'); |
| 80 | + const isPublic = parent.is_public ?? parent.get?.('is_public'); |
| 81 | + const filename = parent.filename || parent.get?.('filename'); |
| 82 | + const status = parent.status || parent.get?.('status'); |
| 83 | + |
| 84 | + if (!key) return null; |
| 85 | + |
| 86 | + // Only provide download URLs for ready/processed files |
| 87 | + if (status !== 'ready' && status !== 'processed') { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + if (isPublic && s3.publicUrlPrefix) { |
| 92 | + // Public file: return direct URL |
| 93 | + return `${s3.publicUrlPrefix}/${key}`; |
| 94 | + } |
| 95 | + |
| 96 | + // Resolve download URL expiry from storage module config (per-database) |
| 97 | + let downloadUrlExpirySeconds = 3600; // fallback default |
| 98 | + try { |
| 99 | + const withPgClient = context.pgSettings |
| 100 | + ? context.withPgClient |
| 101 | + : null; |
| 102 | + if (withPgClient) { |
| 103 | + const config = await withPgClient(null, async (pgClient: any) => { |
| 104 | + const dbResult = await pgClient.query( |
| 105 | + `SELECT jwt_private.current_database_id() AS id`, |
| 106 | + ); |
| 107 | + const databaseId = dbResult.rows[0]?.id; |
| 108 | + if (!databaseId) return null; |
| 109 | + return getStorageModuleConfig(pgClient, databaseId); |
| 110 | + }); |
| 111 | + if (config) { |
| 112 | + downloadUrlExpirySeconds = config.downloadUrlExpirySeconds; |
| 113 | + } |
| 114 | + } |
| 115 | + } catch { |
| 116 | + // Fall back to default if config lookup fails |
| 117 | + } |
| 118 | + |
| 119 | + // Private file: generate presigned GET URL |
| 120 | + return generatePresignedGetUrl( |
| 121 | + s3, |
| 122 | + key, |
| 123 | + downloadUrlExpirySeconds, |
| 124 | + filename || undefined, |
| 125 | + ); |
| 126 | + }, |
| 127 | + }, |
| 128 | + ), |
| 129 | + }, |
| 130 | + 'PresignedUrlDownloadPlugin adding downloadUrl field', |
| 131 | + ); |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +export default createDownloadUrlPlugin; |
0 commit comments