Use the public GCS URL for local uploaded files

This commit is contained in:
Jackson Harper 2022-06-09 11:59:01 -07:00
parent ef072cb2c9
commit e23eada168
2 changed files with 13 additions and 4 deletions

View file

@ -11,6 +11,7 @@ import { WithDataSourcesContext } from '../types'
import {
generateUploadSignedUrl,
generateUploadFilePathName,
getFilePublicUrl,
} from '../../utils/uploads'
import path from 'path'
import normalizeUrl from 'normalize-url'
@ -99,11 +100,12 @@ export const uploadFileRequestResolver: ResolverFn<
input.contentType
)
// If this is a file URL, we swap in the GCS signed
// URL
const publicUrl = getFilePublicUrl(uploadFilePathName)
// If this is a file URL, we swap in the GCS public URL
if (isFileUrl(input.url)) {
await models.uploadFile.update(uploadFileData.id, {
url: uploadSignedUrl,
url: publicUrl,
status: UploadFileStatus.Initialized,
})
}
@ -135,7 +137,7 @@ export const uploadFileRequestResolver: ResolverFn<
} else {
const pageId = await createPage(
{
url: isFileUrl(input.url) ? uploadSignedUrl : input.url,
url: isFileUrl(input.url) ? publicUrl : input.url,
id: input.clientRequestId || '',
userId: claims.uid,
title: title,

View file

@ -15,6 +15,13 @@ const storage = env.fileUpload?.gcsUploadSAKeyFilePath
: new Storage()
const bucketName = env.fileUpload.gcsUploadBucket
export const getFilePublicUrl = (filePathName: string): string => {
return storage
.bucket(bucketName)
.file(filePathName)
.publicUrl()
}
export const generateUploadSignedUrl = async (
filePathName: string,
contentType: string,