fix: resize image if no aspect ratio change (#3859)
* fix: resize image if no aspect ratio change * test: adjust unawaited assertion
This commit is contained in:
@@ -205,7 +205,12 @@ export default async function resizeAndTransformImageSizes({
|
|||||||
const originalAspectRatio = dimensions.width / dimensions.height
|
const originalAspectRatio = dimensions.width / dimensions.height
|
||||||
const targetAspectRatio = imageResizeConfig.width / imageResizeConfig.height
|
const targetAspectRatio = imageResizeConfig.width / imageResizeConfig.height
|
||||||
|
|
||||||
if (originalAspectRatio !== targetAspectRatio) {
|
if (originalAspectRatio === targetAspectRatio) {
|
||||||
|
resized = resized.resize({
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
const prioritizeHeight = originalAspectRatio > targetAspectRatio
|
const prioritizeHeight = originalAspectRatio > targetAspectRatio
|
||||||
|
|
||||||
const { info } = await resized
|
const { info } = await resized
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import fs from 'fs'
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
|
|
||||||
|
import type { Enlarge, Media } from './payload-types'
|
||||||
|
|
||||||
import payload from '../../packages/payload/src'
|
import payload from '../../packages/payload/src'
|
||||||
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath'
|
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath'
|
||||||
import { initPayloadTest } from '../helpers/configHelpers'
|
import { initPayloadTest } from '../helpers/configHelpers'
|
||||||
@@ -133,7 +135,7 @@ describe('Collections - Uploads', () => {
|
|||||||
|
|
||||||
it('creates media without storing a file', async () => {
|
it('creates media without storing a file', async () => {
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
formData.append('file', fs.createReadStream(path.join(__dirname, './unstored.png')))
|
||||||
|
|
||||||
// unstored media
|
// unstored media
|
||||||
const { status, doc } = await client.create({
|
const { status, doc } = await client.create({
|
||||||
@@ -145,7 +147,7 @@ describe('Collections - Uploads', () => {
|
|||||||
expect(status).toBe(201)
|
expect(status).toBe(201)
|
||||||
|
|
||||||
// Check for files
|
// Check for files
|
||||||
expect(await !fileExists(path.join(__dirname, './media', doc.filename))).toBe(false)
|
expect(await fileExists(path.join(__dirname, './media', doc.filename))).toBe(false)
|
||||||
|
|
||||||
// Check api response
|
// Check api response
|
||||||
expect(doc.filename).toBeDefined()
|
expect(doc.filename).toBeDefined()
|
||||||
@@ -162,7 +164,7 @@ describe('Collections - Uploads', () => {
|
|||||||
|
|
||||||
expect(result).toBeTruthy()
|
expect(result).toBeTruthy()
|
||||||
|
|
||||||
const { sizes } = result
|
const { sizes } = result as unknown as Enlarge
|
||||||
const expectedPath = path.join(__dirname, './media/enlarge')
|
const expectedPath = path.join(__dirname, './media/enlarge')
|
||||||
|
|
||||||
// Check for files
|
// Check for files
|
||||||
@@ -199,11 +201,11 @@ describe('Collections - Uploads', () => {
|
|||||||
it('should resize images if one desired dimension is smaller and the other is larger', async () => {
|
it('should resize images if one desired dimension is smaller and the other is larger', async () => {
|
||||||
const small = await getFileByPath(path.resolve(__dirname, './small.png'))
|
const small = await getFileByPath(path.resolve(__dirname, './small.png'))
|
||||||
|
|
||||||
const result = await payload.create({
|
const result = (await payload.create({
|
||||||
collection: enlargeSlug,
|
collection: enlargeSlug,
|
||||||
data: {},
|
data: {},
|
||||||
file: small,
|
file: small,
|
||||||
})
|
})) as unknown as Enlarge
|
||||||
|
|
||||||
expect(result).toBeTruthy()
|
expect(result).toBeTruthy()
|
||||||
|
|
||||||
@@ -236,7 +238,7 @@ describe('Collections - Uploads', () => {
|
|||||||
|
|
||||||
expect(result).toBeTruthy()
|
expect(result).toBeTruthy()
|
||||||
|
|
||||||
const { sizes } = result
|
const { sizes } = result as unknown as Enlarge
|
||||||
const expectedPath = path.join(__dirname, './media/reduce')
|
const expectedPath = path.join(__dirname, './media/reduce')
|
||||||
|
|
||||||
// Check for files
|
// Check for files
|
||||||
@@ -271,11 +273,11 @@ describe('Collections - Uploads', () => {
|
|||||||
const file = await getFileByPath(filePath)
|
const file = await getFileByPath(filePath)
|
||||||
file.name = 'renamed.png'
|
file.name = 'renamed.png'
|
||||||
|
|
||||||
const mediaDoc = await payload.create({
|
const mediaDoc = (await payload.create({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
data: {},
|
data: {},
|
||||||
file,
|
file,
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
||||||
@@ -300,11 +302,11 @@ describe('Collections - Uploads', () => {
|
|||||||
const file = await getFileByPath(filePath)
|
const file = await getFileByPath(filePath)
|
||||||
file.name = 'renamed.png'
|
file.name = 'renamed.png'
|
||||||
|
|
||||||
const mediaDoc = await payload.create({
|
const mediaDoc = (await payload.create({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
data: {},
|
data: {},
|
||||||
file,
|
file,
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
||||||
@@ -332,11 +334,11 @@ describe('Collections - Uploads', () => {
|
|||||||
const file = await getFileByPath(filePath)
|
const file = await getFileByPath(filePath)
|
||||||
file.name = 'temp.png'
|
file.name = 'temp.png'
|
||||||
|
|
||||||
const mediaDoc = await payload.create({
|
const mediaDoc = (await payload.create({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
data: {},
|
data: {},
|
||||||
file,
|
file,
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
const expectedPath = path.join(__dirname, './media')
|
const expectedPath = path.join(__dirname, './media')
|
||||||
|
|
||||||
@@ -348,12 +350,12 @@ describe('Collections - Uploads', () => {
|
|||||||
const newFile = await getFileByPath(newFilePath)
|
const newFile = await getFileByPath(newFilePath)
|
||||||
newFile.name = 'temp-renamed.png'
|
newFile.name = 'temp-renamed.png'
|
||||||
|
|
||||||
const updatedMediaDoc = await payload.update({
|
const updatedMediaDoc = (await payload.update({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
id: mediaDoc.id,
|
id: mediaDoc.id,
|
||||||
file: newFile,
|
file: newFile,
|
||||||
data: {},
|
data: {},
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
// Check that the replacement file was created and the old one was removed
|
// Check that the replacement file was created and the old one was removed
|
||||||
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.filename))).toBe(true)
|
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.filename))).toBe(true)
|
||||||
@@ -366,11 +368,11 @@ describe('Collections - Uploads', () => {
|
|||||||
const file = await getFileByPath(filePath)
|
const file = await getFileByPath(filePath)
|
||||||
file.name = 'temp.png'
|
file.name = 'temp.png'
|
||||||
|
|
||||||
const mediaDoc = await payload.create({
|
const mediaDoc = (await payload.create({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
data: {},
|
data: {},
|
||||||
file,
|
file,
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
const expectedPath = path.join(__dirname, './media')
|
const expectedPath = path.join(__dirname, './media')
|
||||||
|
|
||||||
@@ -382,14 +384,14 @@ describe('Collections - Uploads', () => {
|
|||||||
const newFile = await getFileByPath(newFilePath)
|
const newFile = await getFileByPath(newFilePath)
|
||||||
newFile.name = 'temp-renamed.png'
|
newFile.name = 'temp-renamed.png'
|
||||||
|
|
||||||
const updatedMediaDoc = await payload.update({
|
const updatedMediaDoc = (await payload.update({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
where: {
|
where: {
|
||||||
id: { equals: mediaDoc.id },
|
id: { equals: mediaDoc.id },
|
||||||
},
|
},
|
||||||
file: newFile,
|
file: newFile,
|
||||||
data: {},
|
data: {},
|
||||||
})
|
})) as unknown as { docs: Media[] }
|
||||||
|
|
||||||
// Check that the replacement file was created and the old one was removed
|
// Check that the replacement file was created and the old one was removed
|
||||||
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.docs[0].filename))).toBe(true)
|
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.docs[0].filename))).toBe(true)
|
||||||
@@ -407,12 +409,12 @@ describe('Collections - Uploads', () => {
|
|||||||
file,
|
file,
|
||||||
})
|
})
|
||||||
|
|
||||||
const doc = await payload.update({
|
const doc = (await payload.update({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
id,
|
id,
|
||||||
data: {},
|
data: {},
|
||||||
file: small,
|
file: small,
|
||||||
})
|
})) as unknown as Media
|
||||||
|
|
||||||
expect(doc.sizes.icon).toBeDefined()
|
expect(doc.sizes.icon).toBeDefined()
|
||||||
expect(doc.sizes.tablet.width).toBeNull()
|
expect(doc.sizes.tablet.width).toBeNull()
|
||||||
@@ -429,14 +431,14 @@ describe('Collections - Uploads', () => {
|
|||||||
file,
|
file,
|
||||||
})
|
})
|
||||||
|
|
||||||
const doc = await payload.update({
|
const doc = (await payload.update({
|
||||||
collection: mediaSlug,
|
collection: mediaSlug,
|
||||||
where: {
|
where: {
|
||||||
id: { equals: id },
|
id: { equals: id },
|
||||||
},
|
},
|
||||||
data: {},
|
data: {},
|
||||||
file: small,
|
file: small,
|
||||||
})
|
})) as unknown as { docs: Media[] }
|
||||||
|
|
||||||
expect(doc.docs[0].sizes.icon).toBeDefined()
|
expect(doc.docs[0].sizes.icon).toBeDefined()
|
||||||
expect(doc.docs[0].sizes.tablet.width).toBeNull()
|
expect(doc.docs[0].sizes.tablet.width).toBeNull()
|
||||||
|
|||||||
BIN
test/uploads/unstored.png
Normal file
BIN
test/uploads/unstored.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 KiB |
Reference in New Issue
Block a user