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 targetAspectRatio = imageResizeConfig.width / imageResizeConfig.height
|
||||
|
||||
if (originalAspectRatio !== targetAspectRatio) {
|
||||
if (originalAspectRatio === targetAspectRatio) {
|
||||
resized = resized.resize({
|
||||
height,
|
||||
width,
|
||||
})
|
||||
} else {
|
||||
const prioritizeHeight = originalAspectRatio > targetAspectRatio
|
||||
|
||||
const { info } = await resized
|
||||
|
||||
@@ -3,6 +3,8 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { promisify } from 'util'
|
||||
|
||||
import type { Enlarge, Media } from './payload-types'
|
||||
|
||||
import payload from '../../packages/payload/src'
|
||||
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath'
|
||||
import { initPayloadTest } from '../helpers/configHelpers'
|
||||
@@ -133,7 +135,7 @@ describe('Collections - Uploads', () => {
|
||||
|
||||
it('creates media without storing a file', async () => {
|
||||
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
|
||||
const { status, doc } = await client.create({
|
||||
@@ -145,7 +147,7 @@ describe('Collections - Uploads', () => {
|
||||
expect(status).toBe(201)
|
||||
|
||||
// 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
|
||||
expect(doc.filename).toBeDefined()
|
||||
@@ -162,7 +164,7 @@ describe('Collections - Uploads', () => {
|
||||
|
||||
expect(result).toBeTruthy()
|
||||
|
||||
const { sizes } = result
|
||||
const { sizes } = result as unknown as Enlarge
|
||||
const expectedPath = path.join(__dirname, './media/enlarge')
|
||||
|
||||
// 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 () => {
|
||||
const small = await getFileByPath(path.resolve(__dirname, './small.png'))
|
||||
|
||||
const result = await payload.create({
|
||||
const result = (await payload.create({
|
||||
collection: enlargeSlug,
|
||||
data: {},
|
||||
file: small,
|
||||
})
|
||||
})) as unknown as Enlarge
|
||||
|
||||
expect(result).toBeTruthy()
|
||||
|
||||
@@ -236,7 +238,7 @@ describe('Collections - Uploads', () => {
|
||||
|
||||
expect(result).toBeTruthy()
|
||||
|
||||
const { sizes } = result
|
||||
const { sizes } = result as unknown as Enlarge
|
||||
const expectedPath = path.join(__dirname, './media/reduce')
|
||||
|
||||
// Check for files
|
||||
@@ -271,11 +273,11 @@ describe('Collections - Uploads', () => {
|
||||
const file = await getFileByPath(filePath)
|
||||
file.name = 'renamed.png'
|
||||
|
||||
const mediaDoc = await payload.create({
|
||||
const mediaDoc = (await payload.create({
|
||||
collection: mediaSlug,
|
||||
data: {},
|
||||
file,
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
||||
@@ -300,11 +302,11 @@ describe('Collections - Uploads', () => {
|
||||
const file = await getFileByPath(filePath)
|
||||
file.name = 'renamed.png'
|
||||
|
||||
const mediaDoc = await payload.create({
|
||||
const mediaDoc = (await payload.create({
|
||||
collection: mediaSlug,
|
||||
data: {},
|
||||
file,
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', fs.createReadStream(path.join(__dirname, './small.png')))
|
||||
@@ -332,11 +334,11 @@ describe('Collections - Uploads', () => {
|
||||
const file = await getFileByPath(filePath)
|
||||
file.name = 'temp.png'
|
||||
|
||||
const mediaDoc = await payload.create({
|
||||
const mediaDoc = (await payload.create({
|
||||
collection: mediaSlug,
|
||||
data: {},
|
||||
file,
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
const expectedPath = path.join(__dirname, './media')
|
||||
|
||||
@@ -348,12 +350,12 @@ describe('Collections - Uploads', () => {
|
||||
const newFile = await getFileByPath(newFilePath)
|
||||
newFile.name = 'temp-renamed.png'
|
||||
|
||||
const updatedMediaDoc = await payload.update({
|
||||
const updatedMediaDoc = (await payload.update({
|
||||
collection: mediaSlug,
|
||||
id: mediaDoc.id,
|
||||
file: newFile,
|
||||
data: {},
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
// Check that the replacement file was created and the old one was removed
|
||||
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.filename))).toBe(true)
|
||||
@@ -366,11 +368,11 @@ describe('Collections - Uploads', () => {
|
||||
const file = await getFileByPath(filePath)
|
||||
file.name = 'temp.png'
|
||||
|
||||
const mediaDoc = await payload.create({
|
||||
const mediaDoc = (await payload.create({
|
||||
collection: mediaSlug,
|
||||
data: {},
|
||||
file,
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
const expectedPath = path.join(__dirname, './media')
|
||||
|
||||
@@ -382,14 +384,14 @@ describe('Collections - Uploads', () => {
|
||||
const newFile = await getFileByPath(newFilePath)
|
||||
newFile.name = 'temp-renamed.png'
|
||||
|
||||
const updatedMediaDoc = await payload.update({
|
||||
const updatedMediaDoc = (await payload.update({
|
||||
collection: mediaSlug,
|
||||
where: {
|
||||
id: { equals: mediaDoc.id },
|
||||
},
|
||||
file: newFile,
|
||||
data: {},
|
||||
})
|
||||
})) as unknown as { docs: Media[] }
|
||||
|
||||
// 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)
|
||||
@@ -407,12 +409,12 @@ describe('Collections - Uploads', () => {
|
||||
file,
|
||||
})
|
||||
|
||||
const doc = await payload.update({
|
||||
const doc = (await payload.update({
|
||||
collection: mediaSlug,
|
||||
id,
|
||||
data: {},
|
||||
file: small,
|
||||
})
|
||||
})) as unknown as Media
|
||||
|
||||
expect(doc.sizes.icon).toBeDefined()
|
||||
expect(doc.sizes.tablet.width).toBeNull()
|
||||
@@ -429,14 +431,14 @@ describe('Collections - Uploads', () => {
|
||||
file,
|
||||
})
|
||||
|
||||
const doc = await payload.update({
|
||||
const doc = (await payload.update({
|
||||
collection: mediaSlug,
|
||||
where: {
|
||||
id: { equals: id },
|
||||
},
|
||||
data: {},
|
||||
file: small,
|
||||
})
|
||||
})) as unknown as { docs: Media[] }
|
||||
|
||||
expect(doc.docs[0].sizes.icon).toBeDefined()
|
||||
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