feat: rework image dimensions, use image-size
This commit is contained in:
@@ -52,6 +52,7 @@
|
||||
"find-up": "4.1.0",
|
||||
"get-tsconfig": "^4.7.2",
|
||||
"http-status": "1.6.2",
|
||||
"image-size": "^1.1.1",
|
||||
"joi": "^17.12.1",
|
||||
"json-schema-to-typescript": "11.0.3",
|
||||
"jsonwebtoken": "9.0.1",
|
||||
@@ -62,10 +63,10 @@
|
||||
"pino": "8.15.0",
|
||||
"pino-pretty": "10.2.0",
|
||||
"pluralize": "8.0.0",
|
||||
"probe-image-size": "^7.2.3",
|
||||
"sanitize-filename": "1.6.3",
|
||||
"scheduler": "0.23.0",
|
||||
"scmp": "2.1.0"
|
||||
"scmp": "2.1.0",
|
||||
"tempy": "^3.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@monaco-editor/react": "4.5.1",
|
||||
|
||||
@@ -133,7 +133,7 @@ export const generateFileData = async <T>({
|
||||
}
|
||||
|
||||
if (fileSupportsResize || isImage(file.mimetype)) {
|
||||
dimensions = getImageSize(file)
|
||||
dimensions = await getImageSize(file)
|
||||
fileData.width = dimensions.width
|
||||
fileData.height = dimensions.height
|
||||
}
|
||||
|
||||
@@ -1,14 +1,31 @@
|
||||
import fs from 'fs'
|
||||
import probeImageSize from 'probe-image-size'
|
||||
import sizeOfImport from 'image-size'
|
||||
import { temporaryFileTask } from 'tempy'
|
||||
import { promisify } from 'util'
|
||||
|
||||
import type { PayloadRequest } from '../types/index.js'
|
||||
import type { ProbedImageSize } from './types.js'
|
||||
|
||||
export function getImageSize(file: PayloadRequest['file']): ProbedImageSize {
|
||||
const { imageSize } = sizeOfImport
|
||||
const imageSizePromise = promisify(imageSize)
|
||||
|
||||
export async function getImageSize(file: PayloadRequest['file']): Promise<ProbedImageSize> {
|
||||
if (file.tempFilePath) {
|
||||
const data = fs.readFileSync(file.tempFilePath)
|
||||
return probeImageSize.sync(data)
|
||||
return imageSizePromise(file.tempFilePath)
|
||||
}
|
||||
|
||||
return probeImageSize.sync(file.data)
|
||||
// Tiff file do not support buffers or streams, so we must write to file first
|
||||
// then retrieve dimensions. https://github.com/image-size/image-size/issues/103
|
||||
if (file.mimetype === 'image/tiff') {
|
||||
const dimensions = await temporaryFileTask(
|
||||
async (filepath) => {
|
||||
fs.writeFileSync(filepath, file.data)
|
||||
return imageSizePromise(filepath)
|
||||
},
|
||||
{ extension: 'tiff' },
|
||||
)
|
||||
return dimensions
|
||||
}
|
||||
|
||||
return imageSize(file.data)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@payloadcms/plugin-cloud-storage",
|
||||
"description": "The official cloud storage plugin for Payload CMS",
|
||||
"version": "3.0.0-beta.6",
|
||||
"version": "3.0.0-beta.6-86adc6f",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"type": "module",
|
||||
|
||||
Reference in New Issue
Block a user