Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions functions/src/api/routes/event/exportSchedulePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getFirebaseProjectId } from '../../../utils/getFirebaseProjectId'
import { getServiceAPIKey } from '../../../serviceApi/serviceApiKeyPreHandler'
import { getIndividualDays } from '../../../../../src/utils/dates/diffDays'
import { uploadBufferToStorage } from '../file/utils/uploadBufferToStorage'
import { addPdfFileToEvent, getFilesNames, getUploadFilePath } from '../deploy/updateWebsiteActions/getFilesNames'
import { addPdfFileToEvent } from '../deploy/updateWebsiteActions/getFilesNames'
import { getFileName } from '../../other/getFileName'

const ExportPdfReply = Type.Object({
Expand Down Expand Up @@ -130,17 +130,8 @@ export const exportSchedulePdfRoute = (fastify: FastifyInstance, options: any, d
return reply.status(400).send(publicFileUrlOrError)
}

if (!event.files?.pdf) {
const updatedEvent = await EventDao.getEvent(fastify.firebase, eventId)
const eventFiles = await getFilesNames(fastify.firebase, updatedEvent)
const uploadFilePath = getUploadFilePath(eventFiles)
return reply.status(200).send({
pdf: uploadFilePath.pdf,
})
}

reply.status(200).send({
pdf: getUploadFilePath(event.files).pdf,
pdf: publicFileUrlOrError,
})
} catch (err) {
const error = err as Error
Expand Down
14 changes: 12 additions & 2 deletions src/utils/images/uploadImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import { v4 as uuidv4 } from 'uuid'

export const uploadImage = async (imageFolder: string, image: Blob): Promise<string> => {
const fileName = uuidv4()
const storageBucket = storage.app.options.storageBucket!

const outputRef = ref(storage, `${imageFolder}${fileName}`)
// imageFolder may be a full public URL; extract just the storage path
const baseUrl = `https://storage.googleapis.com/${storageBucket}/`
const altUrl = `https://${storageBucket}.storage.googleapis.com/`
const folderPath = imageFolder.startsWith(baseUrl)
? imageFolder.slice(baseUrl.length)
: imageFolder.startsWith(altUrl)
? imageFolder.slice(altUrl.length)
: imageFolder

const outputRef = ref(storage, `${folderPath}${fileName}`)

await uploadBytes(outputRef, image, {})
return `${imageFolder}${fileName}`
return `https://${storageBucket}.storage.googleapis.com/${outputRef.fullPath}`
}