fix silly bug on upload

This commit is contained in:
Robert Schäfer 2025-05-30 23:19:48 +08:00
parent 4ea6e5ba0e
commit 0dd8f41808
3 changed files with 26 additions and 5 deletions

View File

@ -44,16 +44,14 @@ export interface Images {
resource: { id: string },
relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE',
opts?: DeleteImageOpts,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => Promise<any>
) => Promise<Image>
mergeImage: (
resource: { id: string },
relationshipType: 'HERO_IMAGE' | 'AVATAR_IMAGE',
imageInput: ImageInput | null | undefined,
opts?: MergeImageOpts,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => Promise<any>
) => Promise<Image>
}
export const images = (config: Context['config']) =>

View File

@ -324,6 +324,26 @@ describe('mergeImage', () => {
})
})
})
describe('edge cases: `Location` is not a valid URL (e.g. hetzner object storage)', () => {
beforeEach(async () => {
uploadCallback = jest.fn(
() =>
'fsn1.your-objectstorage.com/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg',
)
})
it('adds missing https:// protocol', async () => {
const result = await mergeImage(post, 'HERO_IMAGE', imageInput, {
uploadCallback,
deleteCallback,
})
expect(result).toMatchObject({
url: 'https://fsn1.your-objectstorage.com/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg',
})
expect(new URL(result.url)).toBeDefined()
})
})
})
})

View File

@ -104,7 +104,10 @@ export const images = (config: S3Configured) => {
const upload = await uploadPromise
const { name, ext } = path.parse(upload.filename)
const uniqueFilename = `${uuid()}-${slug(name)}${ext}`
const Location = await uploadCallback({ ...upload, uniqueFilename })
let Location = await uploadCallback({ ...upload, uniqueFilename })
if (!Location.startsWith('https://') && !Location.startsWith('http://')) {
Location = `https://${Location}` // assume https
}
return Location
}